2112 動態單點修改主席樹
阿新 • • 發佈:2018-12-18
題意:n個數,q個詢問 (n<=50000, q<=10000)
Q x y z 代表詢問[x, y]區間裡的第z小的數
C x y 代表將(從左往右數)第x個數變成y
對於更新, 我們不改變這些已經建好的樹, 而是另建一批樹S,用來記錄更新,而這批線段樹,我們用樹狀陣列來維護
也就是樹狀陣列的每個節點都是一顆線段樹
一開始,S[0]、S[1]、S[2]、S[3]、S[4]、S[5](樹狀陣列的每個節點)這些都與T[0]相同(也就是每個節點建了一棵空樹)
對於C 2 6 這個操作, 我們只需要減去一個2,加上一個6即可
對於減去2
(樹狀陣列i+lowbit(i)為i的父親節點, 修改i,就要把i的所有父親節點都修改了)
2在樹狀陣列中出現的位置是 2、2+lowbit(2)=4 這兩個位置,
因此要更新的是S[2]和S[4]這兩個節點中的樹
加上一個6 (同樣是對於2號位置, 因此需要更新的仍是S[2]和S[4])
當查詢的時候, 對樹T的操作與靜態的一致,另外再加上S樹的值就好了
/// .-~~~~~~~~~-._ _.-~~~~~~~~~-. /// __.' ~. .~ `.__ /// .'// \./ \\`. /// .'// | \\`. /// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`. /// .'//.-" `-. | .-' "-.\\`. /// .'//______.============-.. \ | / ..-============.______\\`. /// .'______________________________\|/______________________________`. #pragma GCC optimize("Ofast") #pragma comment(linker, "/STACK:102400000,102400000") #pragma GCC target(sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx) #include<bits/stdc++.h> using namespace std; #define pi acos(-1) #define s_1(x) scanf("%d",&x) #define s_2(x,y) scanf("%d%d",&x,&y) #define s_3(x,y,z) scanf("%d%d%d",&x,&y,&z) #define s_4(x,y,z,X) scanf("%d%d%d%d",&x,&y,&z,&X) #define S_1(x) scan_d(x) #define S_2(x,y) scan_d(x),scan_d(y) #define S_3(x,y,z) scan_d(x),scan_d(y),scan_d(z) #define PI acos(-1) #define endl '\n' #define srand() srand(time(0)); #define me(x,y) memset(x,y,sizeof(x)); #define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++) #define close() ios::sync_with_stdio(0); cin.tie(0); #define FOR(x,n,i) for(int i=x;i<=n;i++) #define FOr(x,n,i) for(int i=x;i<n;i++) #define fOR(n,x,i) for(int i=n;i>=x;i--) #define fOr(n,x,i) for(int i=n;i>x;i--) #define W while #define sgn(x) ((x) < 0 ? -1 : (x) > 0) #define bug printf("***********\n"); #define db double #define ll long long #define mp make_pair #define pb push_back #define sz size #define fi first #define se second #define pf printf typedef long long LL; typedef pair <int, int> ii; const int INF=0x3f3f3f3f; const LL LINF=0x3f3f3f3f3f3f3f3fLL; const int dx[]={-1,0,1,0,1,-1,-1,1}; const int dy[]={0,1,0,-1,-1,1,-1,1}; const int maxn=1e2+7; const int _=1e5+10; const double EPS=1e-8; const double eps=1e-8; const LL mod=1e9+7; template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);} template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);} template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));} template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));} template <class T> inline bool scan_d(T &ret){char c;int sgn;if (c = getchar(), c == EOF){return 0;} while (c != '-' && (c < '0' || c > '9')){c = getchar();}sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9'){ret = ret * 10 + (c - '0');}ret *= sgn;return 1;} inline bool scan_lf(double &num){char in;double Dec=0.1;bool IsN=false,IsD=false;in=getchar();if(in==EOF) return false; while(in!='-'&&in!='.'&&(in<'0'||in>'9'))in=getchar();if(in=='-'){IsN=true;num=0;}else if(in=='.'){IsD=true;num=0;} else num=in-'0';if(!IsD){while(in=getchar(),in>='0'&&in<='9'){num*=10;num+=in-'0';}} if(in!='.'){if(IsN) num=-num;return true;}else{while(in=getchar(),in>='0'&&in<='9'){num+=Dec*(in-'0');Dec*=0.1;}} if(IsN) num=-num;return true;} void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');} void print(LL a){ Out(a),puts("");} //cerr << "run time is " << clock() << endl; int n,q; int m,tot; int a[_],T[_],L[_<<5],R[_<<5],sum[_<<5]; int S[_],Hash[_]; struct node { int l,r,k; bool Q; }op[_]; void build(int &rt,int l,int r) { rt=++tot; sum[rt]=0; if(l>=r) return ; int mid=(l+r)>>1; build(L[rt],l,mid); build(R[rt],mid+1,r); } void update(int &rt,int l,int r,int last,int pos,int val) { rt=++tot; L[rt]=L[last]; R[rt]=R[last]; sum[rt]=sum[last]+val; if(l>=r) return ; int mid=(l+r)>>1; if(pos<=mid) update(L[rt],l,mid,L[last],pos,val); else update(R[rt],mid+1,r,R[last],pos,val); } int lowbit(int x) { return x&(-x); } int use[_]; void add(int x,int pos,int val) { W(x<=n) { update(S[x],1,m,S[x],pos,val); x+=lowbit(x); } } int Sum(int x) { int ans=0; W(x) { ans+=sum[L[use[x]]]; x-=lowbit(x); } return ans; } int query(int u,int v,int lr,int rr,int l,int r,int k) { if(l>=r) return l; int mid=(l+r)>>1; int tmp=Sum(v)-Sum(u)+sum[L[rr]]-sum[L[lr]]; if(tmp>=k) { for(int i=u;i;i-=lowbit(i)) use[i]=L[use[i]]; for(int i=v;i;i-=lowbit(i)) use[i]=L[use[i]]; return query(u,v,L[lr],L[rr],l,mid,k); } else { for(int i=u;i;i-=lowbit(i)) use[i]=R[use[i]]; for(int i=v;i;i-=lowbit(i)) use[i]=R[use[i]]; return query(u,v,R[lr],R[rr],mid+1,r,k-tmp); } } void init() { sort(Hash+1,Hash+1+m); int mm=unique(Hash+1,Hash+1+m)-Hash-1; m=mm; } void solve() { s_2(n,q); m=0; tot=0; FOR(1,n,i) { s_1(a[i]); Hash[++m]=a[i]; } FOr(0,q,i) { char s[10]; scanf("%s",s); if(s[0]=='Q') {//查詢 s_3(op[i].l,op[i].r,op[i].k); op[i].Q=1; } else { s_2(op[i].l,op[i].r); op[i].Q=0; Hash[++m]=op[i].r; } } init(); build(T[0],1,m); FOR(1,n,i) update(T[i],1,m,T[i-1],lower_bound(Hash+1,Hash+1+m,a[i])-Hash,1); FOR(1,n,i) S[i]=T[0]; FOr(0,q,i) { if(op[i].Q) { for(int j=op[i].l-1;j;j-=lowbit(j)) use[j]=S[j]; for(int j=op[i].r;j;j-=lowbit(j)) use[j]=S[j]; print(Hash[query(op[i].l-1,op[i].r,T[op[i].l-1],T[op[i].r],1,m,op[i].k)]); } else { add(op[i].l,lower_bound(Hash+1,Hash+1+m,a[op[i].l])-Hash,-1); add(op[i].l,lower_bound(Hash+1,Hash+1+m,op[i].r)-Hash,1); a[op[i].l]=op[i].r; } } } int main() { //freopen( "1.in" , "r" , stdin ); //freopen( "1.out" , "w" , stdout ); int t=1; //init(); s_1(t); for(int cas=1;cas<=t;cas++) { //printf("Case #%d: ",cas); solve(); } }