Submission #773032

#TimeUsernameProblemLanguageResultExecution timeMemory
773032Valters07Abracadabra (CEOI22_abracadabra)C++14
0 / 100
3 ms4948 KiB
#include <bits/stdc++.h> #pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt") #define fio ios_base::sync_with_stdio(0);cin.tie(0); #define ll long long #define en cin.close();return 0; #define pb push_back #define fi first//printf("%lli\n",cur); #define se second//scanf("%lli",&n); using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int N = 2e5+5; struct node { int prior, val, mx, sz, l, r; }; vector<node> treap; vector<pair<int,int> > qu[N]; int nw(int val) { treap.pb({rand(),val,val,1,0,0}); return treap.size()-1; } int merg(int t1, int t2) { if(t1==0||t2==0) return (t1|t2); if(treap[t1].prior<treap[t2].prior) { treap[t1].r=merg(treap[t1].r,t2); treap[t1].mx=max({treap[t1].val,treap[treap[t1].l].mx,treap[treap[t1].r].mx}); treap[t1].sz=treap[treap[t1].l].sz+treap[treap[t1].r].sz+1; return t1; } else { treap[t2].l=merg(t1,treap[t2].l); treap[t2].mx=max({treap[t2].val,treap[treap[t2].l].mx,treap[treap[t2].r].mx}); treap[t2].sz=treap[treap[t2].l].sz+treap[treap[t2].r].sz+1; return t2; } } pair<int,int> splitbysize(int t, int sz) { if(treap[t].sz==sz) return {t,0}; int l = treap[t].l, r = treap[t].r; // cout << t << " : " << l << " " << r << " " << sz << endl; if(treap[l].sz>sz) { pair<int,int> tt = splitbysize(l,sz); treap[t].l=tt.se; treap[t].mx=max({treap[t].val,treap[treap[t].l].mx,treap[treap[t].r].mx}); treap[t].sz=treap[treap[t].l].sz+treap[treap[t].r].sz+1; return {tt.fi,t}; } else { pair<int,int> tt = splitbysize(r,sz-treap[l].sz); treap[t].r=tt.fi; treap[t].mx=max({treap[t].val,treap[treap[t].l].mx,treap[treap[t].r].mx}); treap[t].sz=treap[treap[t].l].sz+treap[treap[t].r].sz+1; return {t,tt.se}; } } pair<int,int> splitbymax(int t, int val) { if(treap[t].mx<=val) return {t,0}; int l = treap[t].l, r = treap[t].r; if(treap[l].mx>val) { pair<int,int> tt = splitbymax(l,val); treap[t].l=tt.se; treap[t].mx=max({treap[t].val,treap[treap[t].l].mx,treap[treap[t].r].mx}); treap[t].sz=treap[treap[t].l].sz+treap[treap[t].r].sz+1; return {tt.fi,t}; } else { pair<int,int> tt = splitbymax(r,val); treap[t].r=tt.fi; treap[t].mx=max({treap[t].val,treap[treap[t].l].mx,treap[treap[t].r].mx}); treap[t].sz=treap[treap[t].l].sz+treap[treap[t].r].sz+1; return {t,tt.se}; } } vector<int> make_op(vector<int> &a) { int n = a.size(); vector<int> ret; int it = 0, it2 = n/2; while(it<n/2||it2<n) { if(it==n/2) ret.pb(a[it2++]); else if(it2==n) ret.pb(a[it++]); else if(a[it]<a[it2]) ret.pb(a[it++]); else ret.pb(a[it2++]); } return ret; } int main() { fio ifstream cin("in.in"); srand(6954); treap.pb({0,0,0,0,0,0}); int n, q, r = 1; cin >> n >> q; vector<int> a(n), ans(q); for(auto &x:a) cin >> x; for(int qi = 0;qi<q;qi++) { int t, x; cin >> t >> x; qu[min(t,N-1)].pb({x,qi}); } for(int i = 0;i<N;i++) { if(i==0) { for(auto x:qu[i]) ans[x.se]=a[x.fi-1]; a=make_op(a); for(int i = 0;i<n;i++) r=merg(r,nw(a[i])); } else { for(auto x:qu[i]) { auto t = splitbysize(r,x.fi); cout << t.fi << " " << treap[t.fi].l;en cout << treap[treap[t.fi].l].val << " " << t.se;en auto t2 = splitbysize(t.fi,x.fi-1); ans[x.se]=treap[t2.se].val; r=merg(t2.fi,merg(t2.se,t.se)); } auto t = splitbysize(r,n/2); while(treap[t.se].sz>1) { auto t1 = splitbysize(t.se,1); int val = treap[t1.fi].val; auto t2 = splitbymax(merg(t1.fi,t1.se),val); auto t3 = splitbymax(t.fi,val); t.fi=merg(merg(t3.fi,t2.fi),t3.se); t.se=t2.se; } r=merg(t.fi,t.se); } } for(auto x:ans) cout << x << "\n"; cin.close(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...