#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937 mrand(233333); int rnd(int x) { return mrand() % x;} const ll mod=1000000007; ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head typedef pair<ll,int> keyt; int n,q,ty,id; ll x,y; const int N=401000; struct node { int wt; ll sm; keyt key; int sz; node *s[2]; void upd() { sm=key.fi; sz=1; rep(i,0,2) if (s[i]) sm+=s[i]->sm,sz+=s[i]->sz; } }pool[N],*cur=pool,tpool[101000],*pcur=tpool,*rt; node *newnode(keyt key) { node *q=cur++; q->key=key; q->s[0]=q->s[1]=0; q->upd(); q->wt=mrand(); return q; } node *onewnode(node *p=NULL) { node *q=pcur++; if (p) *q=*p; return q; } void merge(node *&p,node *l,node *r) { if (!l||!r) p=l?l:r; else if (l->wt<r->wt) { merge(l->s[1],l->s[1],r); (p=l)->upd(); } else { merge(r->s[0],l,r->s[0]); (p=r)->upd(); } } node* omerge(node *l,node *r) { if (!l||!r) return l?onewnode(l):onewnode(r); else if (l->wt<r->wt) { node *p=onewnode(l); p->s[1]=omerge(l->s[1],r); p->upd(); return p; } else { node *p=onewnode(r); p->s[0]=omerge(l,r->s[0]); p->upd(); return p; } } void split(node *p,node *&l,node *&r,keyt x) { if (p==0) l=0,r=0; else { if (p->key>=x) r=p,split(p->s[0],l,r->s[0],x),r->upd(); else l=p,split(p->s[1],l->s[1],r,x),l->upd(); } } pair<node*,node*> osplit(node *p,keyt x) { if (p==0) return {NULL,NULL}; else { if (p->key>=x) { auto z=osplit(p->s[0],x); node *r=onewnode(p); r->s[0]=z.se; r->upd(); return mp(z.fi,r); } else { auto z=osplit(p->s[1],x); node *l=onewnode(p); l->s[1]=z.fi; l->upd(); return mp(l,z.se); } } } // 右边<x pair<node*,node*> osplits(node *p,ll x) { if (p==0) return {NULL,NULL}; else { ll oval=p->key.fi+(p->s[1]?p->s[1]->sm:0); if (oval<x) { auto z=osplits(p->s[0],x-oval); node *r=onewnode(p); r->s[0]=z.se; r->upd(); return mp(z.fi,r); } else { auto z=osplits(p->s[1],x); node *l=onewnode(p); l->s[1]=z.fi; l->upd(); return mp(l,z.se); } } } int main() { scanf("%d",&n); node *rt=newnode(mp(1ll<<60,-1)); rep(i,0,n) { scanf("%lld",&x); node *p,*q; keyt v(x,++id); node *md=newnode(v); split(rt,p,q,v); merge(p,p,md); merge(rt,p,q); } scanf("%d",&q); rep(i,0,q) { scanf("%d%lld",&ty,&x); if (ty==2) { node *p,*q; keyt v(x,++id); node *md=newnode(v); split(rt,p,q,v); merge(p,p,md); merge(rt,p,q); } else if (ty==3) { node *p,*q,*r; split(rt,p,q,mp(x,0)); r=q; while (r->s[0]) r=r->s[0]; keyt v(x,r->key.se+1); r=0; split(q,q,r,v); merge(rt,p,r); } else { scanf("%lld",&y); int ans=0; pcur=tpool; node *ort=onewnode(rt); while (x<y) { auto z=osplit(ort,mp(x,0)); node *r=z.se; while (r->s[0]) r=r->s[0]; ll targ=min(r->key.fi+1,y)-x; node *l=z.fi; if (!l||l->sm<targ) { ans=-1; break; } auto z2=osplits(l,targ); assert(z2.fi); node *l2=z2.fi; while (l2->s[1]) l2=l2->s[1]; auto z3=osplit(z2.fi,l2->key); z3.se=omerge(z3.se,z2.se); x+=z3.se->sm; ans+=z3.se->sz; ort=omerge(z3.fi,z.se); } printf("%d\n",ans); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937 mrand(233333); int rnd(int x) { return mrand() % x;} const ll mod=1000000007; ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head typedef pair<ll,int> keyt; int n,q,ty,id; ll x,y; const int N=401000; struct node { int wt; ll sm; keyt key; int sz; node *s[2]; void upd() { sm=key.fi; sz=1; rep(i,0,2) if (s[i]) sm+=s[i]->sm,sz+=s[i]->sz; } }pool[N],*cur=pool,tpool[101000],*pcur=tpool,*rt; node *newnode(keyt key) { node *q=cur++; q->key=key; q->s[0]=q->s[1]=0; q->upd(); q->wt=mrand(); return q; } node *onewnode(node *p=NULL) { node *q=pcur++; if (p) *q=*p; return q; } void merge(node *&p,node *l,node *r) { if (!l||!r) p=l?l:r; else if (l->wt<r->wt) { merge(l->s[1],l->s[1],r); (p=l)->upd(); } else { merge(r->s[0],l,r->s[0]); (p=r)->upd(); } } node* omerge(node *l,node *r) { if (!l||!r) return l?onewnode(l):onewnode(r); else if (l->wt<r->wt) { node *p=onewnode(l); p->s[1]=omerge(l->s[1],r); p->upd(); return p; } else { node *p=onewnode(r); p->s[0]=omerge(l,r->s[0]); p->upd(); return p; } } void split(node *p,node *&l,node *&r,keyt x) { if (p==0) l=0,r=0; else { if (p->key>=x) r=p,split(p->s[0],l,r->s[0],x),r->upd(); else l=p,split(p->s[1],l->s[1],r,x),l->upd(); } } pair<node*,node*> osplit(node *p,keyt x) { if (p==0) return {NULL,NULL}; else { if (p->key>=x) { auto z=osplit(p->s[0],x); node *r=onewnode(p); r->s[0]=z.se; r->upd(); return mp(z.fi,r); } else { auto z=osplit(p->s[1],x); node *l=onewnode(p); l->s[1]=z.fi; l->upd(); return mp(l,z.se); } } } // 右边<x pair<node*,node*> osplits(node *p,ll x) { if (p==0) return {NULL,NULL}; else { ll oval=p->key.fi+(p->s[1]?p->s[1]->sm:0); if (oval<x) { auto z=osplits(p->s[0],x-oval); node *r=onewnode(p); r->s[0]=z.se; r->upd(); return mp(z.fi,r); } else { auto z=osplits(p->s[1],x); node *l=onewnode(p); l->s[1]=z.fi; l->upd(); return mp(l,z.se); } } } int main() { scanf("%d",&n); node *rt=newnode(mp(1ll<<60,-1)); rep(i,0,n) { scanf("%lld",&x); node *p,*q; keyt v(x,++id); node *md=newnode(v); split(rt,p,q,v); merge(p,p,md); merge(rt,p,q); } scanf("%d",&q); rep(i,0,q) { scanf("%d%lld",&ty,&x); if (ty==2) { node *p,*q; keyt v(x,++id); node *md=newnode(v); split(rt,p,q,v); merge(p,p,md); merge(rt,p,q); } else if (ty==3) { node *p,*q,*r; split(rt,p,q,mp(x,0)); r=q; while (r->s[0]) r=r->s[0]; keyt v(x,r->key.se+1); r=0; split(q,q,r,v); merge(rt,p,r); } else { scanf("%lld",&y); int ans=0; pcur=tpool; node *ort=onewnode(rt); while (x<y) { auto z=osplit(ort,mp(x,0)); node *r=z.se; while (r->s[0]) r=r->s[0]; ll targ=min(r->key.fi+1,y)-x; node *l=z.fi; if (!l||l->sm<targ) { ans=-1; break; } auto z2=osplits(l,targ); assert(z2.fi); node *l2=z2.fi; while (l2->s[1]) l2=l2->s[1]; auto z3=osplit(z2.fi,l2->key); z3.se=omerge(z3.se,z2.se); x+=z3.se->sm; ans+=z3.se->sz; ort=omerge(z3.fi,z.se); } printf("%d\n",ans); } } } |