Title Description
Given a sequence comprising n number of a [1], a [2], [3] ...... a [n] a, must answer the inquiry procedure: for a given i, j, k, in a [ i], a [i + 1], a [i + 2] ...... a [j] in the k-th smaller number is the number (1≤k≤j-i + 1), and you can change the number of a [ i] value, after the change, the program can continue to answer the above questions for a later change. You need to compile such a program, a sequence of reads from the input file, and reads the series of instructions, including instructions and a modification instruction interrogation.
For each query command, you must output the correct answer.
Input Format
The first line has two positive integers n (1≤n≤100000), m (1≤m≤100000). Respectively represent the number and length of the instruction sequence.
The second row has the number n, represents a [1], a [2] ...... a [n], these numbers are less than 10 ^ 9. The following description of each instruction m rows, each row format is one of two formats. Q ijk or C it
-
Q ijk (i, j, k is a number, 1≤i≤j≤n, 1≤k≤j-i + 1) represents inquiry command to inquire about a [i], a [i + 1] ...... a [j the number of small k] in.
-
C (1≤i≤n, 0≤t≤10 ^ 9) represents the a [i] becomes changed t.
Output Format
For each prompt, you need to answer his output, each output on separate lines.
Sample input and output
5 3 3 2 1 4 7 Q 1 4 3 C 2 6 Q 2 5 3
3 6
Description / Tips
10% of the data, m, n≤100;
20% of the data, m, n≤1000;
50% of the data, m, n≤10000.
For all data, m, n≤100000
Note that the constant optimization, but overall the normal writing binary tree and the tree can be set over a time around 1000ms for each point.
Source: bzoj1901
Meaning of the questions: Chairman tree with repair
Chairman common tree maintenance is a sequence of updates prefix and prefix and is the most simple 1-i and
If you want to make changes to the i-th then followed by the Chairman of the tree should be modified if the words of the complexity of the violence to be more a n
Can maintain the prefix Fenwick tree log and
#include<bits/stdc++.h> using namespace std; //input by bxd #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define repp(i,a,b) for(int i=(a);i>=(b);--i) #define ll long long #define see(x) (cerr<<(#x)<<'='<<(x)<<endl) #define pb push_back #define inf 0x3f3f3f3f #define CLR(A,v) memset(A,v,sizeof A) typedef pair<int,int>pii; ////////////////////////////////// const int N=2e5+10; int T[N],t[N<<5],lson[N<<5],rson[N<<5],ncnt,cntpre,cntpos,xx[N],yy[N]; void up(int x,int l,int r,int pre,int &pos,int v) { pos=++ncnt; lson[pos]=lson[pre];rson[pos]=rson[pre];t[pos]=t[pre]+v; if(l==r)return ;int m=(l+r)>>1; if(x<=m)up(x,l,m,lson[pre],lson[pos],v); else up(x,m+1,r,rson[pre],rson[pos],v); } int qsum(int k,int l,int r) { if(l==r)return l; int x=0; rep(i,1,cntpre)x-=t[lson[xx[i]]]; rep(i,1,cntpos)x+=t[lson[yy[i]]]; int m=(l+r)>>1; if(k<=x) { rep(i,1,cntpre)xx[i]=lson[xx[i]]; rep(i,1,cntpos)yy[i]=lson[yy[i]]; qsum(k,l,m); } else { rep(i,1,cntpre)xx[i]=rson[xx[i]]; rep(i,1,cntpos)yy[i]=rson[yy[i]]; qsum(k-x,m+1,r); } } int a[N],b[N],nn,n,m,ca[N],cb[N],cc[N]; char s[2]; int add(int x,int v) { int k=lower_bound(b+1,b+1+nn,a[x])-b; for(int i=x;i<=n;i+=i&-i)up(k,1,nn,T[i],T[i],v); } int main() { scanf("%d%d",&n,&m); rep(i,1,n)scanf("%d",&a[i]),b[i]=a[i];nn=n; rep(i,1,m) { scanf("%s",s);scanf("%d%d",&ca[i],&cb[i]); if(s[0]=='Q')scanf("%d",&cc[i]); else b[++nn]=cb[i]; } sort(b+1,b+1+nn); nn=unique(b+1,b+1+nn)-b-1; rep(i,1,n)add(i,1); rep(i,1,m) { if(cc[i]) { cntpre=cntpos=0; for(int j=ca[i]-1;j;j-=j&-j)xx[++cntpre]=T[j]; for(int j=cb[i];j;j-=j&-j) yy[++cntpos]=T[j]; printf("%d\n",b[qsum(cc[i],1,nn)]); } else add(ca[i],-1),a[ca[i]]=cb[i],add(ca[i],1); } return 0; }