QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#172503#7108. CouleursupepapupuAC ✓3052ms43720kbC++174.0kb2023-09-09 19:31:592023-09-09 19:31:59

Judging History

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

  • [2023-09-09 19:31:59]
  • 评测
  • 测评结果:AC
  • 用时:3052ms
  • 内存:43720kb
  • [2023-09-09 19:31:59]
  • 提交

answer

#include <bits/stdc++.h>

#define el '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 10, MAXP = 20;

class Plane {
    vector<int> q[N];
    struct Node {
        int lc, rc;
        ll v;
    }tr[N * (4 + MAXP)];
    int n, idx, root[N];
    void pushup(int u) {
        tr[u].v = tr[tr[u].lc].v + tr[tr[u].rc].v;
    }
    int build_tree(int l, int r) {
        int p = ++idx;
        if (l == r) return p;
        int mid = l + r >> 1;
        tr[p].lc = build_tree(l, mid), tr[p].rc = build_tree(mid + 1, r);
        return p;
    }
    int update(int p, int l, int r, int x, int v) {
        int q = ++idx;
        tr[q] = tr[p];
        if (l == r) {
            tr[q].v += v;
            return q;
        }
        int mid = l + r >> 1;
        if (x <= mid) tr[q].lc = update(tr[p].lc, l, mid, x, v);
        else tr[q].rc = update(tr[p].rc, mid + 1, r, x, v);
        pushup(q);
        return q;
    }
    ll query(int p, int l, int r, int ql, int qr) {
        if (l >= ql && r <= qr) return tr[p].v;
        int mid = l + r >> 1;
        ll res = 0;
        if (ql <= mid) res = query(tr[p].lc, l, mid, ql, qr);
        if (qr > mid) res += query(tr[p].rc, mid + 1, r, ql, qr);
        return res;
    }
  public:
    void clear(int n) {
        memset(tr, 0, (idx + 5) * sizeof(Node));
        memset(root, 0, (n + 5) * sizeof(int));
        idx = 0;
        this->n = n;
        for (int i = 1; i <= n; ++i) q[i].clear();
    }
    void add_point(int x, int y) {
        q[y].push_back(x);
    }
    void build() {
        root[0] = build_tree(1, n);
        for (int i = 1; i <= n; ++i) {
            root[i] = root[i - 1];
            for (auto x: q[i]) root[i] = update(root[i], 1, n, x, 1);
        }
    }
    ll get_sum(int L, int R, int D, int U) {
        return query(root[U], 1, n, L, R) - query(root[D - 1], 1, n, L, R);
    }
}plane;

struct Node {
    int l, r;
    mutable ll v;
    bool operator<(const Node &t) const{
        return l < t.l;
    }
};
set<Node> odt;

void solve() {
    int n;
    cin >> n;
    plane.clear(n + 1);
    vector<int> a(n + 1);

    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        plane.add_point(i, a[i]);
    }
    ll ans = 0;
    plane.build();
    for (int i = 1; i <= n; ++i) 
        ans += plane.get_sum(1, i, a[i] + 1, n + 1);
    cout << ans << ' ';
    multiset<ll> vals;
    odt.clear();
    odt.insert({1, n, ans});
    vals.insert(ans);

    auto wk = [&](int k) {
        ll res = 0;
        auto it = --odt.upper_bound({k});
        ll l = it->l, r = it->r, inv = it->v;
        odt.erase(it), vals.erase(vals.find(inv));
        inv -= plane.get_sum(l, k, a[k] + 1, n + 1);
        inv -= plane.get_sum(k, r, 0, a[k] - 1);
        if (l == r) return;
        if (k - l <= r - k) {
            ll invl = 0, invlr = 0, invr = 0;
            for (int i = l; i < k; ++i) {
                invl += plane.get_sum(l, i, a[i] + 1, n + 1);
                invlr += plane.get_sum(k + 1, r, 0, a[i] - 1);
            }
            invr = inv - invl - invlr;
            if (k > l) odt.insert({l, k - 1, invl}), vals.insert(invl);
            odt.insert({k + 1, r, invr}), vals.insert(invr);
        } else {
            ll invl = 0, invlr = 0, invr = 0;
            for (int i = k + 1; i <= r; ++i) {
                invr += plane.get_sum(k + 1, i, a[i] + 1, n + 1);
                invlr += plane.get_sum(l, k - 1, a[i] + 1, n + 1);
            }
            invl = inv - invr - invlr;
            if (k < r) odt.insert({k + 1, r, invr}), vals.insert(invr);
            odt.insert({l, k - 1, invl}), vals.insert(invl);
        }
    };

    for (int i = 1; i <= n; ++i) {
        ll k; cin >> k;
        if (i == n) break;
        k = k ^ ans;
        wk(k);
        ans = *vals.rbegin();
        cout << ans << ' ';
    }
    cout << el;
}

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

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 3052ms
memory: 43720kb

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:

ok 11116 lines

Extra Test:

score: 0
Extra Test Passed