CC

自然万物都趋向从有序变得无序

0%

Circular RMQ CodeForces - 52C(线段树)

题目链接:点我

题目


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).

Example
Input

4
1 2 3 4
4
3 0
3 0 -1
0 1
2 1

Output

1
0
0

题意:

给你一个循环序列,有两种操作:

  1. 区间(lg, rg) 的所有元素加上V;
  2. 查询区间(lg, rg) 的最小值.

思路:

线段树的区间查询与更新,我们用线段树维护区间的最小值,并且设置一个懒惰标记来延迟更新.

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;

typedef long long LL;
const LL INF = 0x3fffffff;
struct ss{
LL v,tag;//tag是懒惰标记
}a[200000 << 2];
LL Min(LL a,LL b){
return a < b ? a:b;
}

void pushup(int cur){//向上更新
a[cur].v = Min(a[cur<<1].v,a[cur<<1|1].v);
}

void built(int l,int r,int cur){
a[cur].v = 0;
a[cur].tag = 0;//0 表示没有被标记
if(l == r){
scanf("%I64d", &a[cur].v);
return ;
}
int mid = (l + r) >> 1;
built(l, mid,cur << 1);
built(mid + 1,r,cur << 1|1);
pushup(cur);
}

void pushdown(int cur){
if(a[cur].tag != 0){//向下调整,将标记传给儿子
a[cur<<1].tag += a[cur].tag;
a[cur<<1|1].tag += a[cur].tag;
a[cur<<1].v += a[cur].tag;
a[cur<<1|1].v += a[cur].tag;
a[cur].tag = 0;
}
}

void update(int l,int r,int ll,int rr,int cur,LL v){
if(l<=ll&&r>=rr){
a[cur].v += v;
a[cur].tag += v;
return ;
}pushdown(cur);
int mid = (ll +rr)>>1;
if(mid < r) update(l,r,mid + 1,rr,cur<<1|1,v);
if(mid >= l) update(l,r,ll,mid,cur<<1,v);
pushup(cur);
}

LL quiry(int l,int r,int ll,int rr,int cur){
if(l<=ll&&r>=rr){
return a[cur].v;
} pushdown(cur);
int mid = (ll + rr) >> 1;
LL t1 =INF;
LL t2 = INF;
if(mid < r) t1 = quiry(l,r,mid + 1,rr,cur<<1|1);
if(mid >= l) t2 = quiry(l,r,ll,mid,cur<<1);
return Min(t1,t2);
}

int main(){
int n;
scanf("%d", &n);
built(1,n,1);
int m;
scanf("%d", &m);
while(m--){
int a,b,c;
char ch;
scanf("%d%d%c", &a, &b, &ch);
a++,b++;
if(ch != ' '){
if(a>b){
LL t1=quiry(a,n,1,n,1);
LL t2 = quiry(1,b,1,n,1);
printf("%I64d\n",Min(t1,t2));
}else
printf("%I64d\n",quiry(a,b,1,n,1));
}else {scanf("%d", &c);
if(a>b){
update(a,n,1,n,1,c);
update(1,b,1,n,1,c);
}else update(a,b,1,n,1,c);
}
}
return 0;
}