QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#18626 | #2327. Egma Game | DoorKickers | RE | 3ms | 3564kb | C++20 | 2.4kb | 2022-01-22 15:21:51 | 2022-05-06 01:55:26 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
// #define int long long
struct info {
int lson, rson, num;
info() : lson(0), rson(0), num(0) {}
info(int lson, int rson, int num) : lson(lson), rson(rson), num(num) {}
};
struct segment_tree {
int tot; vector<int> root;
vector<info> tr;
segment_tree(int n) : tr(n * 30 + 5), tot(0), root(n + 5) {}
int insert(int p, int l, int r, int x, int idx) {
int q = ++tot;
tr[q] = tr[p];
if (l == r) {
tr[q].num = idx;
return q;
}
int mid = (l + r) >> 1;
if (x <= mid) tr[q].lson = insert(tr[p].lson, l, mid, x, idx);
else tr[q].rson = insert(tr[p].rson, mid + 1, r, x, idx);
tr[q].num = min(tr[tr[q].lson].num, tr[tr[q].rson].num);
return q;
}
int query(int q, int p, int l, int r) {
if (l == r) return l;
int mid = (l + r) >> 1;
if (tr[tr[p].lson].num < q) return query(q, tr[p].lson, l, mid);
else return query(q, tr[p].rson, mid + 1, r);
}
void debug(int p, int l, int r, int idx) {
cout << "tree " << idx << "'s " << l << ' ' << r << "has num = " << tr[p].num << '\n';
// cout << "tree " << idx << "'s " << tr[p].lson << ' ' << tr[p].rson << "is " << tr[tr[p].lson].num << ' ' << tr[tr[p].rson].num << '\n';
if (l == r) return;
int mid = (l + r) >> 1;
debug(tr[p].lson, l, mid, idx);
debug(tr[p].rson, mid + 1, r, idx);
}
};
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
map<int, bool> vis;
vector<int> a(n + 5);
int mx(0), piv(-1);
for (int i = 1; i <= n; i++) {cin >> a[i]; vis[a[i]] = 1; mx = max(mx, a[i]);}
for (int i = 0; i <= mx + 1; i++) {
if (!vis[i]) {piv = i; break;}
}
segment_tree tree(piv + 2);
// cout << "piv = " << piv << '\n';
for (int i = 1; i <= n; i++) {
if (a[i] > piv) {tree.root[i] = tree.root[i - 1]; continue;}
tree.root[i] = tree.insert(tree.root[i - 1], 0, piv, a[i], i);
// tree.debug(tree.root[i], 0, piv, i);
}
int q; cin >> q;
for (int i = 1; i <= q; i++) {
int l, r; cin >> l >> r;
if (piv == 0) cout << 0 << '\n';
else {
cout << tree.query(l, tree.root[r], 0, piv) << '\n';
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 3564kb
input:
5 1 2 3 0 5 2 1 3 1 4
output:
0 4
result:
ok 2 lines
Test #2:
score: -100
Dangerous Syscalls
input:
20 2 6 8 3 2 1 0 6 3 9 5 7 4 5 1 5 6 6 8 1 6 3 11 3 19 8 8 17 17 18 20 19 20