QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#163543 | #7108. Couleur | luanmenglei | RE | 0ms | 0kb | C++17 | 3.0kb | 2023-09-04 10:29:47 | 2023-09-04 10:29:48 |
answer
#include <bits/stdc++.h>
using namespace std;
void debug(const char *msg, ...) {
#ifdef CLESIP
va_list args;
va_start(args, msg);
static char res[512];
vsprintf(res, msg, args);
cerr << "[DEBUG] " << res << "\n";
va_end(args);
#endif
}
using i64 = long long;
const int N = 1e5 + 10;
const int SEG_SIZE = N * 120;
int n, a[N], cnt, rt[N], p[N];
map<int, int> seg;
multiset<i64> ans;
struct Node {
int cnt, tag, lc, rc, min, max;
i64 sum;
} t[SEG_SIZE];
void pull(int x) {
t[x].sum = 0, t[x].cnt = 0, t[x].min = 1e9, t[x].max = -1e9;
if (t[x].lc) t[x].sum += t[t[x].lc].sum, t[x].cnt += t[t[x].lc].cnt, t[x].min = min(t[t[x].lc].min, t[x].min), t[x].max = max(t[t[x].lc].max, t[x].max);
if (t[x].rc) t[x].sum += t[t[x].rc].sum, t[x].cnt += t[t[x].rc].cnt, t[x].min = min(t[t[x].rc].min, t[x].min), t[x].max = max(t[t[x].rc].max, t[x].max);
}
void apply(int x, int k) { t[x].sum += 1LL * t[x].cnt * k, t[x].tag += k; }
void push(int x) {
if (t[x].tag) {
if (t[x].lc) apply(t[x].lc, t[x].tag);
if (t[x].rc) apply(t[x].rc, t[x].tag);
t[x].tag = 0;
}
}
void add(int &x, int l, int r, int v, int p, int sum) {
if (!x) x = ++ cnt;
if (l == r) {
t[x] = { 1, 0, 0, 0, p, p, sum };
return;
}
int mid = (l + r) >> 1;
if (v <= mid) add(t[x].lc, l, mid, v, p, (t[x].rc ? t[t[x].rc].cnt : 0) + sum);
else add(t[x].rc, mid + 1, r, v, p, sum);
pull(x);
}
void extract(int &x, int &y, int k) {
if (!y) {
x = 0;
return;
}
if (t[y].max <= k) {
x = y;
y = 0;
return;
}
if (t[y].min > k) {
x = 0;
return;
}
push(y);
x = ++ cnt;
extract(t[x].lc, t[y].lc, k);
extract(t[x].rc, t[y].rc, k);
apply(t[y].lc, -t[t[x].rc].cnt);
pull(x), pull(y);
}
void solve() {
// debug("new test case");
seg.clear(), ans.clear(), cnt = 0;
cin >> n;
for (int i = 1; i <= n; i ++) rt[i] = 0;
for (int i = 1; i <= n; i ++) cin >> a[i], p[i] = i;
sort(p + 1, p + 1 + n, [&](int x, int y) { return a[x] == a[y] ? x < y : a[x] < a[y]; });
for (int i = 1; i <= n; i ++) a[p[i]] = i;
for (int i = 1; i <= n; i ++)
add(rt[1], 1, n, a[i], i, 0);
seg[1] = n, ans.insert(t[rt[1]].sum);
int q = n, lst = 0;
assert(false);
for (int i = 1; i <= q; i ++) {
assert(!ans.empty());
cout << (lst = *prev(ans.end())) << " \n"[i == q];
int p; cin >> p;
p ^= lst;
if (p < 0 || p > n) continue;
auto iter = prev(seg.upper_bound(p));
auto [l, r] = *iter;
seg.erase(iter);
int rtl = 0, rtr = rt[l], tmp = 0;
ans.extract(t[rt[l]].sum);
rt[l] = 0;
extract(rtl, rtr, p - 1);
extract(tmp, rtr, p);
if (rtl) {
rt[l] = rtl;
seg[l] = p - 1;
ans.insert(t[rt[l]].sum);
}
if (rtr) {
rt[p + 1] = rtr;
seg[p + 1] = r;
ans.insert(t[rt[p + 1]].sum);
}
}
for (int i = 1; i <= cnt; i ++) {
t[i] = { 0, 0, 0, 0, (int) 1e9, (int) -1e9, 0 };
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int tt; cin >> tt;
while (tt --) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
3 5 4 3 1 1 1 5 4 5 3 1 10 9 7 1 4 7 8 5 7 4 8 21 8 15 5 9 2 4 5 10 6 15 4 8 8 1 12 1 10 14 7 14 2 9 13 10 3 37 19 23 15 7 2 10 15 2 13 4 5 8 7 10