This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
vector<int> haha(200001);
vector<int> p(200001,1e7);
vector<pair<int,int>> idk[200001];
vector<pair<int,int>> bruh[200001];
vector<int> pos(200001);
int n;
int calc(int a, int t) {
int l = 0,r = idk[a].size()-1;
while(l < r) {
int mid = (l+r+1)/2;
if(idk[a][mid].second <= t) {
l = mid;
}
else {
r = mid-1;
}
}
return idk[a][l].first;
}
void upd(int a, int x, int t) {
while(a < 200001) {
idk[a].push_back({idk[a][idk[a].size()-1].first+x,t});
a+=((a)&(-a));
}
}
bool dude(int t) {
int c = 0,sb = 0;
for(int i = 18; i >= 0; i--) {
if(c+(1 << i) <= n && sb+calc(c+(1 << i),t) < n/2) {
c+=(1 << i);
sb+=calc(c,t);
}
}
c++;
int w = n/2-sb;
sb+=bruh[c][bruh[c].size()-1].first;
if(sb == n/2) {
return false;
}
int y = pos[c]+bruh[c][bruh[c].size()-1].first-(sb-n/2);
int z = bruh[c][bruh[c].size()-1].first;
while(true) {
if(p[y] < pos[c]+z) {
bruh[haha[y]].push_back({p[y]-y,t});
upd(haha[y],bruh[haha[y]][bruh[haha[y]].size()-1].first,t);
}
else {
bruh[haha[y]].push_back({pos[c]+z-y,t});
upd(haha[y],bruh[haha[y]][bruh[haha[y]].size()-1].first,t);
break;
}
y = p[y];
}
upd(c,n/2-sb,t);
bruh[c].push_back({w,t});
return true;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int q;
cin >> n >> q;
for(int i = 0; i < 200001; i++) {
idk[i].push_back({0,0});
bruh[i].push_back({0,0});
}
for(int i = 1; i <= n; i++) {
cin >> haha[i];
pos[haha[i]] = i;
}
stack<pair<int,int>> troll;
for(int i = 1; i <= n; i++) {
while(!troll.empty() && haha[i] > troll.top().first) {
p[troll.top().second] = i;
troll.pop();
}
troll.push({haha[i],i});
}
int y = 1,big = haha[1];
for(int i = 2; i <= n/2; i++) {
if(haha[i] > big) {
bruh[haha[y]].push_back({i-y,0});
y = i;
big = haha[i];
}
}
bruh[haha[y]].push_back({n/2-y+1,0});
big = haha[n/2+1],y = n/2+1;
for(int i = n/2+1; i <= n; i++) {
if(haha[i] > big) {
bruh[haha[y]].push_back({i-y,0});
y = i;
big = haha[i];
}
}
bruh[haha[y]].push_back({n-y+1,0});
for(int i = 1; i <= n; i++) {
upd(i,bruh[i][bruh[i].size()-1].first,0);
}
for(int i = 1; i <= n; i++) {
dude(i);
}
for(int i = 0; i < q; i++) {
int t,a;
cin >> t >> a;
if(t == 0) {
cout << haha[a] << "\n";
}
else {
t--;
int c = 0,sb = 0;
for(int j = 18; j >= 0; j--) {
if(c+(1 << j) <= n && sb+calc(c+(1 << j),t) < a) {
c+=(1 << j);
sb+=calc(c,t);
}
}
c++;
cout << haha[pos[c]-1-(sb-a)] << "\n";
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |