QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#240768#7108. Couleurpandapythoner#AC ✓1804ms35968kbC++233.4kb2023-11-05 18:36:452023-11-05 18:36:45

Judging History

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

  • [2023-11-05 18:36:45]
  • 评测
  • 测评结果:AC
  • 用时:1804ms
  • 内存:35968kb
  • [2023-11-05 18:36:45]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define flt double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(s) (int)(s).size()

vector<int> l, r, t;
int new_node(int sm, int l1, int r1) {
    int rs = sz(t);
    t.emplace_back(sm);
    l.emplace_back(l1);
    r.emplace_back(r1);
    return rs;
}

int add(int v, int tl, int tr, int pos) {
    if (tl > pos || tr <= pos) return v;
    int nv = new_node(t[v] + 1, l[v], r[v]);
    if (tl + 1 == tr) return nv;
    int tm = (tr - tl) / 2 + tl;
    if (l[nv] == -1) l[nv] = new_node(0, -1, -1);
    if (r[nv] == -1) r[nv] = new_node(0, -1, -1);
    l[nv] = add(l[nv], tl, tm, pos);
    r[nv] = add(r[nv], tm, tr, pos);
    return nv;
}

ll get(int v, int tl, int tr, int ql, int qr) {
    if (v == -1 || tl >= qr || tr <= ql) return 0;
    if (tl >= ql && tr <= qr) return t[v];
    int tm = (tr - tl) / 2 + tl;
    return get(l[v], tl, tm, ql, qr) + get(r[v], tm, tr, ql, qr);
}

struct seg {
    int l, r;
    ll rs;
    bool operator<(const seg& o) const {
        return r < o.r;
    }
    seg(int l, int r, ll rs) : l(l), r(r), rs(rs) {}
};

void solve() {
    t.clear(), l.clear(), r.clear();
    int n;
    cin >> n;
    vector<int> a(n);
    vector<ll> p(n);
    for (int& el : a) {
        cin >> el;
        --el;
    }
    for (ll& el : p) cin >> el;
    vector<int> root(n + 1);
    root[0] = new_node(0, -1, -1);
    vector<vector<int>> b(n);
    for (int i = 0; i < n; ++i) {
        b[a[i]].emplace_back(i);
    }
    ll sm0 = 0;
    for (int i = 0; i < n; ++i) {
        root[i + 1] = root[i];
        for (auto j : b[i]) {
            root[i + 1] = add(root[i + 1], 0, n, j);
            sm0 += get(root[i], 0, n, j + 1, n);
        }
    }
    set<seg> s;
    s.emplace(0, n, sm0);
    multiset<ll> cur_rs{sm0};
    ll lst = sm0;
    for (int i = 0; i < n; ++i) {
        lst = *cur_rs.rbegin();
        cout << lst << " \n"[i == n - 1];
        int pos = (int)(p[i] ^ lst);
        --pos;
        auto it = s.upper_bound(seg{-1, pos, -1});
        auto [ql, qr, rs] = *it;
        s.erase(it);
        cur_rs.erase(cur_rs.find(rs));
        ll lres = 0, rres = 0;
        if (pos - ql <= qr - pos - 1) {
            rres = rs;
            for (int j = ql; j <= pos; ++j) {
                if (j < pos) {
                    lres += get(root[a[j]], 0, n, j + 1, pos);
                }
                rres -= get(root[a[j]], 0, n, j + 1, qr);
            }
        } else {
            lres = rs;
            for (int j = qr - 1; j >= pos; --j) {
                if (j > pos) {
                    rres += (j - pos - 1) - get(root[a[j] + 1], 0, n, pos + 1, j);
                }
                lres -= (j - ql) - get(root[a[j] + 1], 0, n, ql, j);
            }
        }
        if (ql < pos) {
            s.emplace(ql, pos, lres);
            cur_rs.insert(lres);
        }
        if (pos + 1 < qr) {
            s.emplace(pos + 1, qr, rres);
            cur_rs.insert(rres);
        }
    }
}

int32_t main() {
    if (1) {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    }
#ifdef LOCAL
    freopen("../input.txt", "r", stdin);
    freopen("../output.txt", "w", stdout);
#endif
    t.reserve(1e6);
    l.reserve(1e6);
    r.reserve(1e6);
    int tt;
    cin >> tt;
    while (tt--) solve();
    return 0;
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3616kb

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: 1804ms
memory: 35968kb

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