#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
#include <algorithm>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
const int bSize = 11;
const int bits = 17;
const int NMAX = 200001;
const int LIMIT = NMAX * bits;
vector <pii> qq[LIMIT + 1];
int sol[NMAX * 5];
int v[NMAX];
int poz[NMAX];
int stiva[NMAX];
int vf;
int nxt[NMAX];
pii interval[NMAX];
int aib[NMAX];
void update(int node, int x){
for(; node < NMAX; node += node&(-node))
aib[node] += x;
}
int query(int node){
int x = 0;
for(; node > 0; node -= node&(-node))
x += aib[node];
return x;
}
int cb(int k){
int r = 0, pas = (1 << bits), sum = 0;
while(pas){
if(r + pas < NMAX && query(r + pas) < k){
r += pas;
}
pas /= 2;
}
r++;
return r;
}
int sz(pii x){
if(x.second == 0)
return 0;
return x.second - x.first + 1;
}
int n;
void mShuffle(){
int cine = cb(n / 2);
if(query(cine) == n / 2){
return;
}
update(cine, -sz(interval[cine]));
int pana = query(cine - 1);
int node = n / 2;
node -= pana;
interval[cine] = {poz[cine], poz[cine] + node - 1};
update(cine, sz(interval[cine]));
int i = v[poz[cine] + node];
while(sz(interval[i]) == 0){
int urm = nxt[poz[i]];
if(urm == 0)
urm = n + 1;
interval[i] = {poz[i], urm - 1};
update(i, sz(interval[i]));
if(urm != n + 1)
i = v[urm];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int q, i;
cin >> n >> q;
for(i = 1; i <= n; i++){
cin >> v[i];
poz[v[i]] = i;
while(vf && v[stiva[vf]] < v[i]){
nxt[stiva[vf]] = i;
vf--;
}
stiva[++vf] = i;
}
i = 1;
while(nxt[i] != 0){
interval[v[i]] = {i, nxt[i] - 1};
i = nxt[i];
}
interval[v[i]] = {i, n};
for(i = 1; i <= n; i++){
update(i, sz(interval[i]));
}
for(i = 1; i <= q; i++){
int x, y;
cin >> x >> y;
x = min(x, LIMIT);
qq[x].push_back({y, i});
}
for(i = 0; i <= LIMIT; i++){
for(auto x : qq[i]){
int node = x.first;
int idx = x.second;
int cine = cb(node);
int pana = query(cine - 1);
node -= pana;
sol[idx] = v[poz[cine] + node - 1];
}
mShuffle();
}
for(i = 1; i <= q; i++){
cout << sol[i] << "\n";
}
return 0;
}
Compilation message
Main.cpp: In function 'int cb(int)':
Main.cpp:44:35: warning: unused variable 'sum' [-Wunused-variable]
44 | int r = 0, pas = (1 << bits), sum = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
671 ms |
98164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
799 ms |
101404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
544 ms |
85296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
671 ms |
98164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |