RMQ problem again time limit: 10000ms single point time limit: 1000ms memory limit: 256MB description
Finally, little Hi and Little Ho embarked on the journey back home. On the plane, looking at the sourcing of the specialty--Little hi was in a deep thought: remember last week when they went to the supermarket, and back and forth so many things, fortunately no other people (Sales/other customers) to disturb their procurement process. But if such a thing happens, how will their purchases become?
So little hi then raised the question to small ho: Assume that the entire shelf from left to right placed N products, and sequentially labeled 1 to N, each small hi gave a section of the interval [L, R], small ho to do is to select the label in this range of all the goods the lightest weight, and tell the small hi this product weight. However, in this process, it may be because of other people's behavior, the weight of the goods in some places change (such as the replacement of other kinds of goods), in the face of such a problem, how can small ho solve it?
Tip: Balance is the reason for harmony
Input
Each test point (input file) has and has only one set of test data.
The 1th behavior of each set of test data is an integer n, meaning as described earlier.
The 2nd behavior of each group of test data n integers, respectively, describes the weight of each commodity, where the I integer represents the weight of the commodity labeled I weight_i.
The 3rd behavior of each set of test data is an integer q, which indicates the sum of the number of times that a small hi has been queried and the number of times the weight of the product has been changed.
Each group of test data n+4~n+q+3 line, each line describes an operation, each line begins with a number belonging to 0 or 1, respectively, indicating that the line describes a query and describe the weight of a product change two cases. For line n+i+3, if the line describes a query, then the next two integers are Li, RI, which represents a range of small hi queries [Li, Ri]; If the line describes a change in the weight of a product, then two integer pi,wi, indicating that the weight of the product with the position number pi is changed to Wi
For 100% of data, meet N<=10^4,q<=10^4, 1<=li<=ri<=n,1<=pi<=n, 0<weight_i, wi<=10^4.
Output
For each set of test data, for each small hi query, according to the order in which they appear in the input, each output line represents the result: the weight of the lightest item in all the items in the interval [Li, Ri].
-
Sample input
-
10618 5122 1923 8934 2518 6024 5406 1020 8291 2647 60 3 61 2 20090 2 20 2 101 1 52840 2 5
-
Sample output
1923 2009 1020 1923
- Analysis: Try to write it again with RMQ and line tree;
- Code:
- RMQ:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<climits>#include<cstring>#include<string>#include<Set>#include<map>#include<queue>#include<stack>#include<vector>#include<list>#include<bitset>#defineRep (I,m,n) for (i=m;i<=n;i++)#defineRSP (It,s) for (Set<int>::iterator It=s.begin (); It!=s.end (); it++)#defineVI vector<int>#definePII pair<int,int>#defineMoD 1000000007#defineINF 0x3f3f3f3f#definePB Push_back#defineMP Make_pair#defineFi first#defineSe Second#definell Long Long#definePi ACOs (-1.0)Const intmaxn=1e6+Ten;Const intdis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};using namespacestd;ll gcd (ll p,ll q) {returnq==0? P:GCD (q,p%q);} ll Qpow (ll p,ll q) {ll F=1; while(q) {if(q&1) f=f*p;p=p*p;q>>=1;}returnF;}intn,m,p[maxn],a[ +][MAXN];voidinit () { for(intI=2; i<=n;i++) p[i]=1+p[i/2]; for(intI=1;i< -; i++) for(intj=1; j+ (1<<i)-1<=n;j++) A[i][j]=min (a[i-1][j],a[i-1][j+ (1<< (I-1))]); return;}intGETMI (intLintR) { intx=p[r-l+1]; returnMin (a[x][l],a[x][r-(1<<X) +1]);}intMain () {inti,j,k,t; scanf ("%d",&N); Rep (I,1, N) scanf ("%d", &a[0][i]); Init (); scanf ("%d",&m); while(m--) { intOk,l,r; scanf ("%d%d%d",&ok,&l,&R); if(!ok) printf ("%d\n", GETMI (l,r)); Else{a[0][l]=R; for(i=1;i< -; i++) { for(J=max (1, L +1-(1<<i)); j<=l&&j+ (1<<i)-1<=n;j++) A[i][j]=min (a[i-1][j],a[i-1][j+ (1<< (I-1))]); } } } //System ("pause"); return 0;}
Segment Tree:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<climits>#include<cstring>#include<string>#include<Set>#include<map>#include<queue>#include<stack>#include<vector>#include<list>#include<bitset>#defineRep (I,m,n) for (i=m;i<=n;i++)#defineRSP (It,s) for (Set<int>::iterator It=s.begin (); It!=s.end (); it++)#defineVI vector<int>#definePII pair<int,int>#defineMoD 1000000007#defineINF 0x3f3f3f3f#definePB Push_back#defineMP Make_pair#defineFi first#defineSe Second#definell Long Long#definePi ACOs (-1.0)Const intmaxn=1e6+Ten;Const intdis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};using namespacestd;ll gcd (ll p,ll q) {returnq==0? P:GCD (q,p%q);} ll Qpow (ll p,ll q) {ll F=1; while(q) {if(q&1) f=f*p;p=p*p;q>>=1;}returnF;}intN,M,A[MAXN];voidUpdateintPintq) {P+=n-1; A[P]=Q; while(p) {p= (P-1)/2; A[P]=min (a[2*p+1],a[2*p+2]); }}intQueryintQlintQrintPintLintR) { if(r<=ql| | QR<=L)returninf; Else if(QL<=L&&R<=QR)returnA[p]; Else returnMin (Query (ql,qr,p*2+1, L, (l+r)/2), Query (ql,qr,p*2+2, (L+R)/2, R));}intMain () {inti,j,k,t; memset (A,inf,sizeof(a)); scanf ("%d",&t); N=1; while(n<t) n<<=1; Rep (I,0, T1) scanf ("%d",&j), Update (I,J); scanf ("%d",&m); while(m--) { intP,b,c; scanf ("%d%d%d",&p,&b,&c); if(p==0) b--, printf ("%d\n", Query (B,c,0,0, N)); Elseb--, update (B,C); } //System ("pause"); return 0;}
RMQ Problem again