Line Segment tree problem, line segment tree
Input
Each test point (input file) has only one set of test data.
The 1st behavior of each group of test data is an integer N, meaning as described above.
The 2nd Act N Integers of each group of test data respectively describe the weight of each item. the I-th integer indicates the weight of the item labeled I weight_ I.
The 3rd behavior of each group of test data is an integer Q, indicating the sum of the total number of inquiries by the small Hi and the number of changes to the weight of the product.
N + 4 ~ N + Q + 3 rows. Each row describes an operation. Each row starts with a number 0 or 1, this line describes one query and one change to the item's weight. For row N + I + 3, if this row describes a query, the next two integers are Li, Ri, indicating a range of the small Hi Query [Li, Ri]; if this row describes a change in the weight of a product, the next two integers are Pi, Wi, indicating that the weight of the product with the position number Pi is changed to Wi
For 100% of the data, N <= 10 ^ 6, Q <= 10 ^ 6, 1 <= Li <= Ri <= N, 1 <= Pi <= N, 0 <weight_ I, Wi <= 10 ^ 4.
Output
For each group of test data, for each small Hi query, according to the order in which the input appears, each output a row, indicating the query result: number in the range [Li, ri] the lightest weight of all items.
-
Sample Input
-
10
-
3655 5246 8991 5933 7474 7603 6098 6654 2414 884
-
6
-
0 4 9
-
0 2 10
-
1 4 7009
-
0 5 6
-
1 3 7949
-
1 3 1227
-
Sample output
-
2414
-
884
-
7474
1 #include <cstdio> 2 3 const int NM = 1<<21; 4 int a[NM] = {0}; 5 int _min(int x,int y) {return (x<y)?x:y;} 6 7 int aa;char ch; 8 9 int _getNumber()10 {11 while(ch=getchar(),ch<48||ch>57);12 aa=ch-48;13 while(ch=getchar(),ch>47&&ch<58)14 aa=(aa<<1)*5+ch-48;15 return aa;16 }17 18 19 20 int _query(int l,int r)21 {22 int res = NM;23 while(1)24 {25 if(l>r) break;26 if(l==r) {res = _min(res,a[l]); break;}27 if((l&1)==1) {res = _min(res,a[l]); l++;}28 if((r&1)==0) {res = _min(res,a[r]); r--;}29 l>>=1; r>>=1;30 }31 return res;32 }33 34 void _update(int p,int v)35 {36 a[p] = v;37 while(1)38 {39 p>>=1;40 if(p==0)41 break;42 a[p] = _min(a[p<<1],a[(p<<1)|1]);43 }44 }45 46 int main()47 {48 int N,Q,A,B,C;49 N = _getNumber();50 for(A=1;A<N;A<<=1); B = A+N; C = A<<1;51 for(int i = A; i < B; i++) a[i] = _getNumber();52 for(int i = B; i < C; i++) a[i] = NM;53 for(int i = A-1; i; i--) a[i] = _min(a[i<<1],a[(i<<1)|1]);54 55 Q = _getNumber();56 for(int i = 0; i < Q; i++)57 {58 int x,y,z;59 x = _getNumber(); y = _getNumber(); z = _getNumber();60 if(x==0)61 printf("%d\n",_query(--y+A,--z+A));62 else63 _update(--y+A,z);64 }65 return 0;66 }