QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#181161#7108. Couleurucup-team1069#RE 2ms9944kbC++203.7kb2023-09-16 16:06:432023-09-16 16:06:44

Judging History

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

  • [2023-09-16 16:06:44]
  • 评测
  • 测评结果:RE
  • 用时:2ms
  • 内存:9944kb
  • [2023-09-16 16:06:43]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

const int M = 70e5 + 200;
const int N = 1e5 + 200;

int n, a[N], rt[N];
long long now[N];

struct tree {
    int L[M], R[M];
    int t[M], sz = 0;

    void clear() {
        for (int i = 1; i <= sz; ++i) {
            L[i] = R[i] = t[i] = 0;
        }
        sz = 0;
    }
    
    inline int make() {
        return ++sz;
    }

    void build(int v, int tl = 1, int tr = n) {
        t[v] = 0;
        if (tl == tr) return;
        L[v] = make();
        R[v] = make();
        int tm = tl + tr >> 1;
        build(L[v], tl, tm);
        build(R[v], tm + 1, tr);
    }

    void upd(int pos, int v, int u, int tl = 1, int tr = n) {
        if (tl == tr) {
            t[v] = t[u] + 1;
            return;
        }
        int tm = tl + tr >> 1;
        L[v] = L[u];
        R[v] = R[u];
        if (pos <= tm) {
           L[v] = make();
           upd(pos, L[v], L[u], tl, tm);
        }
        else {
           R[v] = make();
           upd(pos, R[v], R[u], tm + 1, tr);
        }
        t[v] = t[L[v]] + t[R[v]];
    }

    int get(int l, int r, int lv, int rv, int tl = 1, int tr = n) {
        if (l <= tl && tr <= r)
            return t[rv] - t[lv];
        if (r < tl || tr < l)
            return 0;
        int tm = tl + tr >> 1;
        return get(l, r, L[lv], L[rv], tl, tm) + get(l, r, R[lv], R[rv], tm + 1, tr);
    }
} t;

void solve() {
    cin >> n;
    t.clear();
    rt[0] = t.make();
    t.build(rt[0]);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        rt[i] = t.make();
        t.upd(a[i], rt[i], rt[i - 1]);
    }
    long long ans = 0;
    for (int i = 1; i <= n; ++i) {
        ans += t.get(a[i] + 1, n, rt[0], rt[i - 1]);
    }
    set<pair<int, int>> s;
    multiset<long long> vals;
    s.insert({1, n});
    now[1] = ans;
    vals.insert(ans);
    vals.insert(0);
    for (int i = 1, p; i <= n; ++i) {
        cout << ans << " ";
        cin >> p;
        p ^= ans;
        if (i == n) break;
        auto it = prev(s.upper_bound({p, n + 1}));
        auto [l, r] = *it;
        s.erase(it);
        vals.erase(vals.find(now[l]));
        long long x = 0, y = 0, z = 0;
        if (p <= (l + r) / 2) {
            for (int j = l; j < p; ++j) {
                x += t.get(a[j] + 1, n, rt[l - 1], rt[j - 1]);
                y += t.get(1, a[j] - 1, rt[p], rt[r]);
            }
            z += t.get(1, a[p] - 1, rt[p], rt[r]);
            z += t.get(a[p] + 1, n, rt[l - 1], rt[p - 1]);
            if (p + 1 <= r) {
                now[p + 1] = now[l] - x - y - z;
                s.insert({p + 1, r});
                vals.insert(now[p + 1]);
            }
            if (p - 1 >= l) {
                s.insert({l, p - 1});
                now[l] = x;
                vals.insert(now[l]);
            }
        }
        else {
            for (int j = p + 1; j <= r; ++j) {
                x += t.get(a[j] + 1, n, rt[p], rt[j - 1]);
                y += t.get(a[j] + 1, n, rt[l - 1], rt[p - 1]);
            }
            z += t.get(1, a[p] - 1, rt[p], rt[r]);
            z += t.get(a[p] + 1, n, rt[l - 1], rt[p - 1]);
            if (p + 1 <= r) {
                s.insert({p + 1, r});
                now[p + 1] = x;
                vals.insert(now[p + 1]);
            }
            if (p - 1 >= l) {
                s.insert({l, p - 1});
                now[l] = now[l] - x - y - z;
                vals.insert(now[l]);
            }
        }
        ans = *vals.rbegin();
    }
    cout << "\n";
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int T;
    cin >> T;
    while (T--) solve();
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 9944kb

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: -100
Runtime Error

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 ...

result: