QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#486720#7108. CouleurlwenjAC ✓2851ms39556kbC++143.0kb2024-07-21 23:32:522024-07-21 23:32:53

Judging History

你现在查看的是最新测评结果

  • [2024-07-21 23:32:53]
  • 评测
  • 测评结果:AC
  • 用时:2851ms
  • 内存:39556kb
  • [2024-07-21 23:32:52]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, idx;
int arr[N], root[N];
struct node {
    int l, r;
    int cnt;
}tr[N * 4 + 20 * N];

map<int, long long> mp;
multiset<long long> ms;

int query(int cur, int l, int r, int ql, int qr) {
    if (ql > qr) return 0;
    if (l >= ql && r <= qr) return tr[cur].cnt;
    int mid = (l + r) >> 1, sum = 0;

    if (ql <= mid) sum += query(tr[cur].l, l, mid, ql, qr);
    if (qr > mid) sum += query(tr[cur].r, mid + 1, r, ql, qr);
    return sum;
}

int build(int l, int r) {
    int cur = ++idx;
    if (l == r) 
        return cur;
    
    int mid = (l + r) >> 1;
    tr[cur].l = build(l, mid), tr[cur].r = build(mid + 1, r);
    return cur;
}

int add(int p, int l, int r, int x) {
    int cur = ++idx;
    tr[cur] = tr[p];
    if (l == r) {
        tr[cur].cnt++;
        return cur;
    }

    int mid = (l + r) / 2;
    if (x <= mid) tr[cur].l = add(tr[p].l, l, mid, x);
    else tr[cur].r = add(tr[p].r, mid + 1, r, x);
    tr[cur].cnt = tr[tr[cur].l].cnt + tr[tr[cur].r].cnt;

    return cur;
}

void split(int L, int R, int X) {
    long long old = mp[L - 1]; ms.erase(ms.find(old));
    long long base = query(root[R], 1, n, 1, arr[X] - 1) - query(root[X], 1, n, 1, arr[X] - 1);
    base += query(root[X - 1], 1, n, arr[X] + 1, n) - query(root[L - 1], 1, n, arr[X] + 1, n);
    if (X - L < R - X) {
        long long a = 0, b = base;
        for (int i = L; i < X; i++) {
            a += query(root[i - 1], 1, n, arr[i] + 1, n) - query(root[L - 1], 1, n, arr[i] + 1, n);
            b += query(root[R], 1, n, 1, arr[i] - 1) - query(root[X], 1, n, 1, arr[i] - 1);
        }
        mp[L - 1] = a; ms.insert(mp[L - 1]);
        mp[X] = old - a - b; ms.insert(mp[X]);
    } else {
        long long a = 0, b = base;
        for (int i = X + 1; i <= R; i++) {
            a += query(root[i - 1], 1, n, arr[i] + 1, n) - query(root[X], 1, n, arr[i] + 1, n);
            b += query(root[X - 1], 1, n, arr[i] + 1, n) - query(root[L - 1], 1, n, arr[i] + 1, n);
        }
        mp[L - 1] = old - a - b; ms.insert(mp[L - 1]);
        mp[X] = a; ms.insert(mp[X]);
    }
}

void solve() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &arr[i]);

    for (int i = 1; i <= idx; i++)
        tr[i] = {0, 0, 0};
    idx = 0;
    root[0] = build(1, n);
    for (int i = 1; i <= n; i++) root[i] = add(root[i - 1], 1, n, arr[i]);

    long long tmp = 0;
    for (int i = 1; i <= n; i++) tmp += query(root[i - 1], 1, n, arr[i] + 1, n);

    mp.clear(); ms.clear();
    mp[0] = tmp; ms.insert(tmp);
    mp[n + 1] = 0; ms.insert(0);

    long long ans = *prev(ms.end());
    for (int i = 1; i <= n; i++) {
        printf("%lld%c", ans, "\n "[i < n]);
        long long x; scanf("%lld", &x);
        x ^= ans;
        auto it = prev(mp.lower_bound(x));
        split(it->first + 1, next(it)->first - 1, x);
        ans = *prev(ms.end());
    }
}

int main() 
{
    int t; scanf("%d", &t);
    while (t--) solve();
    return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5884kb

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

output:

7 0 0 0 0
20 11 7 2 0 0 0 0 0 0
42 31 21 14 14 4 1 1 1 0 0 0 0 0 0

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 2851ms
memory: 39556kb

input:

11116
10
10 5 10 3 6 4 8 5 9 8
31 27 24 11 12 3 0 2 3 1
10
8 2 7 2 8 10 1 10 9 10
6 5 2 13 2 1 0 1 3 1
10
7 10 7 6 1 3 10 6 7 9
21 18 10 1 6 5 4 8 9 10
10
2 10 4 8 8 5 7 2 6 7
20 10 9 1 15 0 4 2 9 7
10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
10
1 2 3 4 5 6 7 8 9 10
6 3 5 2 7 10 9 1 4 8
10
1 10 1 3...

output:

21 18 16 12 10 6 4 1 1 0
12 12 10 10 4 4 4 2 1 0
20 16 9 5 3 3 3 0 0 0
22 14 8 8 5 5 2 1 1 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
19 12 7 4 4 2 2 1 0 0
20 18 8 3 1 1 0 0 0 0
45 21 21 10 3 3 3 0 0 0
17 11 8 2 1 1 1 0 0 0
13 4 1 0 0 0 0 0 0 0
29 27 22 15 9 7 4 3 1 0
26 16 9 2 1 1 1 1 1 0
0 0 0 0 0 ...

result:

ok 11116 lines

Extra Test:

score: 0
Extra Test Passed