Description

You are given circular array a0, a1, ..., an - 1.
There are two types of operations with it:

  • inc(lf, rg, v) — this operation increases each element on the segment
    [lf, rg] (inclusively) by
    v;
  • rmq(lf, rg) — this operation returns minimal value on the segment
    [lf, rg] (inclusively).

Assume segments to be circular, so if n = 5 and
lf = 3, rg = 1, it means the index sequence:
3, 4, 0, 1.

Write program to process given sequence of operations.

Input

The first line contains integer n (1 ≤ n ≤ 200000). The next line contains initial state of the array:
a0, a1, ..., an - 1
( - 106 ≤ ai ≤ 106),
ai are integer. The third line contains integer
m (0 ≤ m ≤ 200000),
m — the number of operartons. Next
m lines contain one operation each. If line contains two integer
lf, rg (0 ≤ lf, rg ≤ n - 1) it means
rmq operation, it contains three integers
lf, rg, v (0 ≤ lf, rg ≤ n - 1; - 106 ≤ v ≤ 106)
inc operation.

Output

For each rmq operation write result for it. Please, do not use
%lld specificator to read or write 64-bit integers in C++. It is preffered to use
cout (also you may use
%I64d).

Sample Input

Input
4
1 2 3 4
4
3 0
3 0 -1
0 1
2 1
Output
1
0
0

题意非常好理解。

假设a>b的话,就查0~b和a~n-1,其余的就是线段树区间更新模板,推断m个询问里是否存在c,详见代码,非常好理解。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <ctype.h>
#include <iostream>
#define lson o << 1, l, m
#define rson o << 1|1, m+1, r
using namespace std;
typedef __int64 LL;
const __int64 MAX = 9223372036854775807;
const int maxn = 200010;
int n, a, q, c, b;
char str[1200];
LL mi[maxn<<2], add[maxn<<2];
void up(int o) {
mi[o] = min(mi[o<<1], mi[o<<1|1]);
}
void down(int o) {
if(add[o]) {
add[o<<1] += add[o];
add[o<<1|1] += add[o];
mi[o<<1] += add[o];
mi[o<<1|1] += add[o];
add[o] = 0;
}
}
void build(int o, int l, int r) {
if(l == r) {
scanf("%I64d", &mi[o]);
return;
}
int m = (l+r) >> 1;
build(lson);
build(rson);
up(o);
}
void update(int o, int l, int r) {
if(a <= l && r <= b) {
add[o] += c;
mi[o] += c;
return ;
}
down(o);
int m = (l+r) >> 1;
if(a <= m) update(lson);
if(m < b ) update(rson);
up(o);
}
LL query(int o, int l, int r) {
if(a <= l && r <= b) return mi[o];
down(o);
int m = (l+r) >> 1;
LL res = MAX;
if(a <= m) res = query(lson);
if(m < b ) res = min(res, query(rson));
return res;
}
int main()
{
scanf("%d", &n);
build(1, 0, n-1);
scanf("%d", &q); getchar(); while(q--) {
gets(str);
if(sscanf(str,"%d %d %d", &a, &b, &c) == 3) {
if(a <= b) update(1, 0, n-1);
else {
int tmp = b;
b = n-1; update(1, 0, n-1);
a = 0, b = tmp; update(1, 0, n-1);
}
} else {
LL ans ;
if(a <= b) ans = query(1, 0, n-1);
else {
int tmp = b;
b = n-1; ans = query(1, 0, n-1);
a = 0, b = tmp; ans = min(ans, query(1, 0, n-1));
}
printf("%I64d\n", ans);
}
}
return 0;
}



最新文章

  1. jq focus 在火狐(Firefox)下无效
  2. C语言SOCKET编程指南
  3. 修改PE文件的入口函数OEP
  4. JS模块化工具requirejs教程(一):初识requirejs
  5. POJ2761---Feed the dogs (Treap求区间第k大)
  6. python实现词法分析
  7. ASP.NET MVC 中使用 UEditor 富文本编辑器
  8. uva 11992 为矩阵更新查询段树
  9. linux: 几个常用makefile模板
  10. [Bullet3]三种碰撞检测及实现
  11. 【HTTP协议】---TCP三次握手和四次挥手
  12. gitlab的搭建及问题的解决
  13. 《.NET最佳实践》与Ext JS/Touch的团队开发
  14. bzoj3435 [Wc2014]紫荆花之恋
  15. 第28月第22天 iOS动态库
  16. Ubuntu安装搜狗sougou输入法
  17. 下拉列表 通过option 改变div的内容
  18. Lintcode449-Char to Integer-Naive
  19. Python——pandas读取JSON数据,xml,html数据(python programming)
  20. IDEA中的替换功能(替换代码中的变量名很好用哦)

热门文章

  1. firefox 自写底层扩展,源码简介
  2. HTML5学习笔记之Input类型
  3. Oracle EBS-SQL (GL-3):从总帐追溯到发票
  4. saiku安装方法总结
  5. JavaScript之insertBefore()和自定义insertAfter()的用法。
  6. Putty以及adb网络调试
  7. 在OS X 10.10系统上安装Navicat Premium中文破解版11.0.16教程
  8. NXT项目准备资料
  9. 对于System.Net.Http的学习(一)——System.Net.Http 简介(转)
  10. BZOJ 2879: [Noi2012]美食节( 费用流 + 动态加边 )