QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#159934#7108. Couleurucup-team1469#AC ✓1719ms30540kbC++205.1kb2023-09-02 19:07:322023-09-02 19:07:32

Judging History

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

  • [2023-09-02 19:07:32]
  • 评测
  • 测评结果:AC
  • 用时:1719ms
  • 内存:30540kb
  • [2023-09-02 19:07:32]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

#define rep(i, l, r) for (int i = (l); i < (r); ++i)

struct BIT {
    int n;
    vector<int> data;

    BIT(int n_) : n(n_), data(n + 1, 0) {}

    void add(int i, int v) {
        ++i;
        while (i <= n) {
            data[i] += v;
            i += i & -i;
        }
    }

    int fold(int l, int r) {
        int ret = 0;
        while (l < r) {
            ret += data[r];
            r -= r & -r;
        }
        while (r < l) {
            ret -= data[l];
            l -= l & -l;
        }
        return ret;
    }
};

template <class C, class T>
int lwb(const C &con, const T &v) {
    return lower_bound(con.begin(), con.end(), v) - con.begin();
}
template <class C, class T>
int upb(const C &con, const T &v) {
    return upper_bound(con.begin(), con.end(), v) - con.begin();
}

void solve() {
    int N;
    cin >> N;
    vector<int> A(N);
    for (auto &e : A) cin >> e;
    set<tuple<int, int, int, i64>> s;
    vector<BIT> fens;
    vector<vector<int>> ps;
    multiset<i64> answers;
    {
        vector<int> vec;
        rep (i, 0, N) {
            vec.push_back(A[i]);
        }
        ranges::sort(vec);
        vec.erase(ranges::unique(vec).begin(), vec.end());
        const int m = (int)vec.size();
        BIT tree(m);
        i64 inv = 0;
        rep (i, 0, N) {
            const int p = lwb(vec, A[i]);
            inv += tree.fold(p + 1, m);
            tree.add(p, 1);
        }
        ps.push_back(vec);
        fens.push_back(tree);
        s.insert({0, N, 0, inv});
        answers.insert(inv);
    }

    int nowMaxId = 1;
    i64 nowAns = *answers.rbegin();

    auto divide = [&](int l, int r, int m) {
        assert(l <= m and m < r);
        const auto itr = s.lower_bound({l, 0, 0, 0ll});
        const int id = std::get<2>(*itr);
        if (r - l == 1) return;
        if (m - l >= r - (m + 1)) {
            // base : L
            vector<int> vs;
            rep (i, m + 1, r) {
                vs.push_back(A[i]);
            }
            ranges::sort(vs);
            vs.erase(ranges::unique(vs).begin(), vs.end());
            ps.push_back(vs);

            const int x = vs.size();
            BIT tree(x);
            i64 inv = 0, binv = std::get<3>(*itr);
            rep (i, m + 1, r) {
                const int p = lwb(vs, A[i]);
                inv += tree.fold(p + 1, x);
                tree.add(p, 1);
                const int p2 = lwb(ps[id], A[i]);
                fens[id].add(p2, -1);
            }
            const int p = lwb(ps[id], A[m]);
            fens[id].add(p, -1);
            binv -= fens[id].fold(p + 1, ps[id].size());
            binv -= tree.fold(0, lwb(vs, A[m]));
            fens.push_back(tree);

            rep (i, m + 1, r) {
                const int p2 = lwb(ps[id], A[i]);
                binv -= fens[id].fold(p2 + 1, (int)ps[id].size());
            }
            binv -= inv;

            answers.erase(answers.find(std::get<3>(*itr)));
            answers.insert(inv);
            answers.insert(binv);
            s.erase(itr);
            s.insert({l, m, id, binv});
            s.insert({m + 1, r, nowMaxId++, inv});
        } else {
            // base : R
            vector<int> vs;
            rep (i, l, m) {
                vs.push_back(A[i]);
            }
            ranges::sort(vs);
            vs.erase(ranges::unique(vs).begin(), vs.end());
            ps.push_back(vs);

            const int x = vs.size();
            BIT tree(x);
            i64 inv = 0, binv = std::get<3>(*itr);
            rep (i, l, m) {
                const int p = lwb(vs, A[i]);
                inv += tree.fold(p + 1, x);
                tree.add(p, 1);
                const int p2 = lwb(ps[id], A[i]);
                fens[id].add(p2, -1);
            }
            const int p = lwb(ps[id], A[m]);
            fens[id].add(p, -1);
            binv -= fens[id].fold(0, p);
            binv -= tree.fold(upb(vs, A[m]), x);
            fens.push_back(tree);

            rep (i, l, m) {
                const int p2 = lwb(ps[id], A[i]);
                binv -= fens[id].fold(0, p2);
            }
            binv -= inv;

            answers.erase(answers.find(std::get<3>(*itr)));
            answers.insert(inv);
            answers.insert(binv);
            s.erase(itr);
            s.insert({l, m, nowMaxId++, inv});
            s.insert({m + 1, r, id, binv});
        }
    };

    std::vector<i64> P(N);
    for (auto &e : P) cin >> e;
    rep (q, 0, N) {
        nowAns = *answers.rbegin();
        cout << nowAns << (q == N - 1 ? '\n' : ' ');

        P[q] ^= nowAns;
        --P[q];
        assert(0 <= P[q] and P[q] < N);

        auto itr = s.upper_bound({P[q], N + 1, N + 1, N + 1});
        --itr;
        const auto &[l, r, id, score] = *itr;
        divide(l, r, P[q]);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T;
    cin >> T;
    while (T--) {
        solve();
    }
}

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

详细

Test #1:

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

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: 1719ms
memory: 30540kb

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