Submission #923114

#TimeUsernameProblemLanguageResultExecution timeMemory
923114GrindMachineAbracadabra (CEOI22_abracadabra)C++17
100 / 100
694 ms66332 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef long long int ll; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,ll> pll; #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL) #define pb push_back #define endl '\n' #define sz(a) (int)a.size() #define setbits(x) __builtin_popcountll(x) #define ff first #define ss second #define conts continue #define ceil2(x,y) ((x+y-1)/(y)) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define yes cout << "Yes" << endl #define no cout << "No" << endl #define rep(i,n) for(int i = 0; i < n; ++i) #define rep1(i,n) for(int i = 1; i <= n; ++i) #define rev(i,s,e) for(int i = s; i >= e; --i) #define trav(i,a) for(auto &i : a) template<typename T> void amin(T &a, T b) { a = min(a,b); } template<typename T> void amax(T &a, T b) { a = max(a,b); } #ifdef LOCAL #include "debug.h" #else #define debug(x) 42 #endif /* read the edi a long time ago and remember some ideas from there */ const int MOD = 1e9 + 7; const int N = 1e5 + 5; const int inf1 = int(1e9) + 5; const ll inf2 = ll(1e18) + 5; template<typename T> struct fenwick { int siz; vector<T> tree; fenwick() { } fenwick(int n) { siz = n; tree = vector<T>(n + 1); } int lsb(int x) { return x & -x; } void build(vector<T> &a, int n) { for (int i = 1; i <= n; ++i) { int par = i + lsb(i); tree[i] += a[i]; if (par <= siz) { tree[par] += tree[i]; } } } void pupd(int i, T v) { while (i <= siz) { tree[i] += v; i += lsb(i); } } T sum(int i) { T res = 0; while (i) { res += tree[i]; i -= lsb(i); } return res; } T query(int l, int r) { if (l > r) return 0; T res = sum(r) - sum(l - 1); return res; } }; void solve(int test_case) { ll n,q; cin >> n >> q; vector<ll> a(n+5); rep1(i,n) cin >> a[i]; vector<ll> rx(n+5,n); stack<ll> stk; rep1(i,n){ while(!stk.empty() and a[i] > a[stk.top()]){ rx[stk.top()] = i-1; stk.pop(); } stk.push(i); } vector<ll> pos(n+5); rep1(i,n) pos[a[i]] = i; fenwick<ll> fenw(n+5); ll curr_mx = 0; rep1(i,n){ if(a[i] > curr_mx){ fenw.pupd(a[i],rx[i]-i+1); curr_mx = a[i]; } } vector<pll> queries[n+5]; rep1(id,q){ ll t,i; cin >> t >> i; amin(t,n); queries[t].pb({i,id}); } vector<ll> ans(q+5); rep(t,n+1){ if(t){ ll l = 1, r = n; ll mid_block = -1; while(l <= r){ ll mid = (l+r) >> 1; if(fenw.query(1,mid) > n/2){ mid_block = mid; r = mid-1; } else{ l = mid+1; } } assert(mid_block != -1); ll prev_sum = fenw.query(1,mid_block-1); ll ignore = n/2-prev_sum; if(ignore){ ll siz = fenw.query(mid_block,mid_block); fenw.pupd(mid_block,-siz+ignore); ll ptr = pos[mid_block]+ignore; ll mxr = pos[mid_block]+siz-1; while(ptr <= mxr){ if(rx[ptr] <= mxr){ fenw.pupd(a[ptr],rx[ptr]-ptr+1); ptr = rx[ptr]+1; } else{ fenw.pupd(a[ptr],mxr-ptr+1); ptr = mxr+1; } } } } for(auto [i,id] : queries[t]){ ll l = 1, r = n; ll block = -1; while(l <= r){ ll mid = (l+r) >> 1; if(fenw.query(1,mid) >= i){ block = mid; r = mid-1; } else{ l = mid+1; } } ll prev_sum = fenw.query(1,block-1); ans[id] = a[pos[block]+i-prev_sum-1]; } } rep1(i,q) cout << ans[i] << endl; } int main() { fastio; int t = 1; // cin >> t; rep1(i, t) { solve(i); } 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...