#include<bits/stdc++.h>#define int long long#define pb push_backusingnamespacestd;constintMAXN=2e5+5;structNode{Node*lc=nullptr;Node*rc=nullptr;intl,r;longlongsum;Node(intl,intr):l(l),r(r){sum=0;}voidpull(){sum=lc->sum+rc->sum;}};intn,q;inta[MAXN];Node*build(intl,intr){Node*root=newNode(l,r);if(l==r){root->sum=a[l];returnroot;}intmid=(l+r)/2;root->lc=build(l,mid);root->rc=build(mid+1,r);root->pull();returnroot;}voidupdate(Node*root,intpos,intval){if(root->l==root->r){root->sum=val;return;}if(pos<=root->lc->r){update(root->lc,pos,val);}else{update(root->rc,pos,val);}root->pull();}intwalk(Node*root,intk){if(root->l==root->r){returnroot->l;}if(k<=root->lc->sum){returnwalk(root->lc,k);}else{returnwalk(root->rc,k-root->lc->sum);}}signedmain(){cin>>n>>q;for(inti=0;i<n;i++){cin>>a[i];}Node*root=build(0,n-1);while(q--){intop;cin>>op;if(op==1){intpos;cin>>pos;a[pos]^=1;update(root,pos,a[pos]);}elseif(op==2){intk;cin>>k;k++;cout<<walk(root,k)<<'\n';}}}
CSES - List Removals
給一個長度為 n 的陣列 a[1], a[2], … , a[n],有 q 筆操作,給 k,將從左數過去第 k 個數移除,並輸出被移除的數字是多少
\(1\le n, q\le 2\times 10^5\)
思路
問在哪個 index 的 prefix sum 恰好超過 k。也就是在線段樹上「二分搜」。具體來說,看左子樹的 sum 是否足夠 k,是的話就往左走,不是的話就往右走,並將 k -= lc.sum
#include<bits/stdc++.h>#define int long longusingnamespacestd;constintN=5e5+5;intn,ans;vector<pair<int,int>>G[N];structGraph{intn,cnt;vector<int>sz;vector<int>par;stack<pair<int,int>>stk;intfind(intx){if(par[x]==x)returnx;elsereturnfind(par[x]);}voidinit(int_n){n=_n;sz=vector<int>(n,1);par=vector<int>(n);for(inti=0;i<n;i++){par[i]=i;}}voidmerge(intu,intv){intx=find(u),y=find(v);if(x==y){stk.push({x,x});return;}if(sz[x]<sz[y])swap(x,y);sz[x]+=sz[y];par[y]=x;stk.push({x,y});}voidundo(){auto[x,y]=stk.top();stk.pop();if(x==y)return;sz[x]-=sz[y];par[y]=y;}}dsu;voiddivide(intl,intr){if(l==r){for(auto[a,b]:G[l]){a=dsu.find(a),b=dsu.find(b);ans+=dsu.sz[a]*dsu.sz[b];}return;}intmid=(l+r)/2;for(inti=l;i<=mid;i++){for(auto[u,v]:G[i]){dsu.merge(u,v);}}divide(mid+1,r);for(inti=l;i<=mid;i++){for(auto[u,v]:G[i]){dsu.undo();}}for(inti=mid+1;i<=r;i++){for(auto[u,v]:G[i]){dsu.merge(u,v);}}divide(l,mid);for(inti=mid+1;i<=r;i++){for(auto[u,v]:G[i]){dsu.undo();}}}signedmain(){cin>>n;for(inti=1;i<n;i++){intu,v,w;cin>>u>>v>>w;G[w].push_back({u,v});}dsu.init(n+1);divide(1,n);cout<<ans<<'\n';}
#include<bits/stdc++.h>#include<bits/extc++.h>#define int long long#define pii pair<int, int>#define pb push_back#define mk make_pair#define F first#define S second#define ALL(x) x.begin(), x.end()usingnamespacestd;usingnamespace__gnu_pbds;tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update>T;constintINF=2e18;constintmaxn=5e5+5;constintM=1e9+7;structNode{Node*lc=nullptr;Node*rc=nullptr;intl,r;intid=-1,cnt=0;Node(intl,intr):l(l),r(r){}voidpull(){if(lc->cnt==0){id=rc->id;cnt=rc->cnt;return;}if(rc->cnt==0){id=lc->id;cnt=lc->cnt;return;}if(lc->id==rc->id){id=lc->id;cnt=lc->cnt+rc->cnt;}else{if(lc->cnt>rc->cnt){id=lc->id;cnt=lc->cnt-rc->cnt;}else{id=rc->id;cnt=rc->cnt-lc->cnt;}}}};intn,q;inta[maxn];Node*build(intl,intr){Node*root=newNode(l,r);if(l==r){root->id=a[l];root->cnt=1;returnroot;}intmid=(l+r)/2;root->lc=build(l,mid);root->rc=build(mid+1,r);root->pull();returnroot;}piiquery(constNode*root,intql,intqr){if(qr<root->l||root->r<ql)return{-1,0};if(ql<=root->l&&root->r<=qr){return{root->id,root->cnt};}piitmp={-1,0};if(ql<=root->lc->r){piiret=query(root->lc,ql,qr);if(tmp.S==0){tmp=ret;}elseif(ret.S!=0){if(ret.F==tmp.F){tmp.S+=ret.S;}else{if(ret.S>tmp.S){tmp.F=ret.F;tmp.S=ret.S-tmp.S;}else{tmp.S=tmp.S-ret.S;}}}}if(root->rc->l<=qr){piiret=query(root->rc,ql,qr);if(tmp.S==0){tmp=ret;}elseif(ret.S!=0){if(ret.F==tmp.F){tmp.S+=ret.S;}else{if(ret.S>tmp.S){tmp.F=ret.F;tmp.S=ret.S-tmp.S;}else{tmp.S=tmp.S-ret.S;}}}}returntmp;}voidinit(){cin>>n>>q;for(inti=0;i<n;i++){cin>>a[i];T.insert({a[i],i});}}voidsolve(){Node*root=build(0,n-1);while(q--){intl,r;cin>>l>>r;l--,r--;auto[id,c]=query(root,l,r);if(c==0){cout<<0<<'\n';continue;}intcnt=T.order_of_key(mk(id,r+1))-T.order_of_key(mk(id,l));if(cnt>(r-l+1)/2){cout<<id<<'\n';}else{cout<<0<<'\n';}}}signedmain(){intt=1;while(t--){init();solve();}}
#include<bits/stdc++.h>#include<bits/extc++.h>#define int long long#define pii pair<int, int>#define pb push_back#define mk make_pair#define F first#define S second#define ALL(x) x.begin(), x.end()usingnamespacestd;usingnamespace__gnu_pbds;tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update>T;constintINF=2e18;constintmaxn=3e5+5;constintM=1e9+7;structNode{intl,r;Node*lc=nullptr;Node*rc=nullptr;intid=-1,cnt=0;intmx;Node(intl,intr):l(l),r(r){}voidpull(){if(lc->cnt==0){id=rc->id;cnt=rc->cnt;}elseif(rc->cnt==0){id=lc->id;cnt=lc->cnt;}else{if(lc->id==rc->id){id=lc->id;cnt=lc->cnt+rc->cnt;}else{if(lc->cnt>rc->cnt){id=lc->id;cnt=lc->cnt-rc->cnt;}else{id=rc->id;cnt=rc->cnt-lc->cnt;}}}mx=max(lc->mx,rc->mx);}};intn,q;inta[maxn];Node*build(intl,intr){Node*root=newNode(l,r);if(l==r){root->id=a[l];root->cnt=1;root->mx=a[l];returnroot;}intmid=(l+r)/2;root->lc=build(l,mid);root->rc=build(mid+1,r);root->pull();returnroot;}voidupdate(Node*root,intpos,intval){if(root->l==root->r){T.erase({root->id,root->l});root->id=val;root->cnt=1;root->mx=val;T.insert({root->id,root->l});return;}if(pos<=root->lc->r){update(root->lc,pos,val);}else{update(root->rc,pos,val);}root->pull();}voiddivision(Node*root,intmL,intmR,intval){if(root->r<mL||mR<root->l)return;if(root->l==root->r){T.erase({root->id,root->l});root->id/=val;root->mx=root->id;T.insert({root->id,root->l});return;}if(mL<=root->lc->r&&root->lc->mx){division(root->lc,mL,mR,val);}if(root->rc->l<=mR&&root->rc->mx){division(root->rc,mL,mR,val);}root->pull();}piiquery(constNode*root,intql,intqr){if(qr<root->l||root->r<ql)return{-1,0};if(ql<=root->l&&root->r<=qr){return{root->id,root->cnt};}piitmp={-1,0};if(ql<=root->lc->r){piiret=query(root->lc,ql,qr);if(tmp.S==0){tmp=ret;}elseif(ret.S!=0){if(ret.F==tmp.F){tmp.S+=ret.S;}else{if(ret.S>tmp.S){tmp.F=ret.F;tmp.S=ret.S-tmp.S;}else{tmp.S=tmp.S-ret.S;}}}}if(root->rc->l<=qr){piiret=query(root->rc,ql,qr);if(tmp.S==0){tmp=ret;}elseif(ret.S!=0){if(ret.F==tmp.F){tmp.S+=ret.S;}else{if(ret.S>tmp.S){tmp.F=ret.F;tmp.S=ret.S-tmp.S;}else{tmp.S=tmp.S-ret.S;}}}}returntmp;}voidinit(){cin>>n>>q;for(inti=0;i<n;i++){cin>>a[i];T.insert({a[i],i});}}voidsolve(){Node*root=build(0,n-1);intop;while(q--){cin>>op;if(op==1){inti,x;cin>>i>>x;i--;update(root,i,x);}elseif(op==2){intl,r,k;cin>>l>>r>>k;l--,r--;if(k>1)division(root,l,r,k);}else{intl,r;cin>>l>>r;l--,r--;auto[id,c]=query(root,l,r);if(c==0){cout<<"-1"<<'\n';continue;}intcnt=T.order_of_key(mk(id,r+1))-T.order_of_key(mk(id,l));if(cnt>(r-l+1)/2){cout<<id<<'\n';}else{cout<<"-1"<<'\n';}}}}signedmain(){ios::sync_with_stdio(0);cin.tie(0);intt=1;//cin >> t;while(t--){init();solve();}}
#include<bits/stdc++.h>#define int long long#define ALL(x) x.begin(), x.end()usingnamespacestd;constintINF=2e18;structQuery{intl,r,id;booloperator<(constQuery&rhs)const{returnl>rhs.l;}};structNode{Node*lc=nullptr;Node*rc=nullptr;intl,r;intchg,sum;Node(intl,intr):l(l),r(r){chg=0;}voidpull(){sum=lc->sum+rc->sum;}voidpush(){if(chg){lc->sum=chg*(lc->r-lc->l+1);rc->sum=chg*(lc->r-lc->l+1);lc->chg=chg;rc->chg=chg;chg=0;}}};Node*build(intl,intr){Node*root=newNode(l,r);if(l==r){root->sum=0;returnroot;}intmid=(l+r)/2;root->lc=build(l,mid);root->rc=build(mid+1,r);root->pull();returnroot;}voidupdate(Node*root,intml,intmr,intval){if(mr<root->l||root->r<ml){return;}if(ml<=root->l&&root->r<=mr){root->sum=val*(root->r-root->l+1);root->chg=val;return;}root->push();update(root->lc,ml,mr,val);update(root->rc,ml,mr,val);root->pull();}intask(Node*root,intql,intqr){if(qr<root->l||root->r<ql){return0;}if(ql<=root->l&&root->r<=qr){returnroot->sum;}root->push();returnask(root->lc,ql,qr)+ask(root->rc,ql,qr);}constintN=2e5+5;intn,q;inta[N],pre[N],ans[N];signedmain(){cin>>n>>q;for(inti=1;i<=n;i++){cin>>a[i];pre[i]=pre[i-1]+a[i];}vector<Query>query;for(inti=1;i<=q;i++){intl,r;cin>>l>>r;query.push_back({l,r,i});}sort(ALL(query));Node*root=build(1,n);stack<int>stk;intl=n+1;for(auto&i:query){while(i.l<l){l--;while(stk.size()&&a[stk.top()]<=a[l]){intml=stk.top();stk.pop();intmr;if(stk.size()){mr=stk.top()-1;}else{mr=n;}update(root,ml,mr,a[l]);}update(root,l,l,a[l]);stk.push(l);}ans[i.id]=ask(root,i.l,i.r)-(pre[i.r]-pre[i.l-1]);}for(inti=1;i<=q;i++){cout<<ans[i]<<'\n';}}