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;
using ll = long long;
using pi32 = pair<int, int>;
using pi64 = pair<int64_t, int64_t>;
const int maxN = 2e5 + 5;
const int maxC = 10000000;
const ll INF = 2e18 + 7;
const int MOD = 998244353;
int n, nTest;
int idx;
struct Segment {
int left, right, val;
} st[maxC];
void init (int id, int l, int r){
if (l == r) return;
int mid = (l + r) / 2;
st[id].left = ++idx;
st[id].right = ++idx;
init(st[id].left, l, mid);
init(st[id].right, mid + 1, r);
}
void update (int id, int pos, int l, int r, int h) {
if (l == r) {
st[id].val = st[pos].val + 1;
return;
}
int mid = (l + r) / 2;
st[id].left = st[pos].left;
st[id].right = st[pos].right;
if (h <= mid) {
st[id].left = ++idx;
update(st[id].left, st[pos].left, l, mid, h);
}
else {
st[id].right = ++idx;
update(st[id].right, st[pos].right, mid + 1, r, h);
}
st[id].val = st[st[id].left].val + st[st[id].right].val;
}
int root[maxN];
int query (int id, int pos, int l, int r, int add) {
if (l == r) return l;
int mid = (l + r) / 2;
int tmp = st[st[id].right].val - st[st[pos].right].val + add;
if (tmp >= mid + 1)
return query(st[id].right, st[pos].right, mid + 1, r, add);
else return query(st[id].left, st[pos].left, l, mid, tmp);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> nTest;
idx = 1;
root[0] = 1;
init(1, 1, n);
for (int i = 1; i <= n; ++i) {
int h; cin >> h;
root[i] = root[i - 1];
int old = root[i];
root[i] = ++idx;
update(root[i], old, 1, n, h);
}
while (nTest--) {
int l, r; cin >> l >> r;
cout << query(root[r], root[l-1], 1, n, 0) << '\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... |