QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#163134#7108. Couleurucup-team296AC ✓1181ms24084kbC++204.7kb2023-09-03 21:14:222023-09-03 21:14:23

Judging History

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

  • [2023-09-03 21:14:23]
  • 评测
  • 测评结果:AC
  • 用时:1181ms
  • 内存:24084kb
  • [2023-09-03 21:14:22]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "../../debug.h"
#else
#define dbg(...) 787788
#endif

struct Fenwick {
    vector<int> a;

    Fenwick(int n) {
        a.resize(n);
    }

    Fenwick() = default;

    int sum(int pos) {
        int r = 0;
        while (pos >= 0) {
            r += a[pos];
            pos = (pos & (pos + 1)) - 1;
        }
        return r;
    }

    void inc(int pos, int val) {
        while (pos < (int)a.size()) {
            a[pos] += val;
            pos |= pos + 1;
        }
    }
};

struct Solver {
    vector<int> sorted;
    vector<int> vals;
    int fr, to;
    long long inv{};
    Fenwick f;

    Solver(vector<int> vals_) : vals(vals_) {
        sorted = vals;
        sort(sorted.begin(), sorted.end());
        sorted.erase(unique(sorted.begin(), sorted.end()), sorted.end());
        f = Fenwick(sorted.size());
        fr = 0;
        to = vals.size() - 1;
        for (int i = 0; i < (int)vals.size(); i++) {
            int pos = lower_bound(sorted.begin(), sorted.end(), vals[i]) - sorted.begin();
            inv += i - f.sum(pos);
            f.inc(pos, 1);
        }
    }

    void inc_fr() {
        int pos = lower_bound(sorted.begin(), sorted.end(), vals[fr]) - sorted.begin();
        inv -= f.sum(pos - 1);
        f.inc(pos, -1);
        fr++;
    }

    void dec_to() {
        int pos = lower_bound(sorted.begin(), sorted.end(), vals[to]) - sorted.begin();
        int tot = to - fr + 1;
        inv -= tot - f.sum(pos);
        f.inc(pos, -1);
        to--;
    }
};

void solve() {
    int tc;
    cin >> tc;
    for (int t = 0; t < tc; t++) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        vector<Solver> solvers;
        vector<int> solver_ids(n, -1);
        Solver base(a);
        multiset<long long> all_inv;
        all_inv.insert(base.inv);
        solvers.push_back(std::move(base));
        solver_ids[0] = 0;
        set<int> solvers_pos;
        solvers_pos.insert(0);
        solvers_pos.insert(n);
        for (int i = 0; i < n; i++) {
            long long mid_p;
            cin >> mid_p;
            long long max_inv = *all_inv.rbegin();
            cout << max_inv;
            if (i + 1 < n) {
                cout << ' ';
            } else {
                cout << '\n';
            }
            mid_p ^= max_inv;
            mid_p -= 1;

            assert(mid_p >= 0);
            assert(mid_p < n);
            auto it = solvers_pos.upper_bound(mid_p);
            it--;
            int pos = *it;
            int id = solver_ids[pos];
            assert(id != -1);
            int len = solvers[id].to - solvers[id].fr + 1;

            all_inv.erase(all_inv.find(solvers[id].inv));

            int next_pos = pos + len;
            int left = mid_p - pos;
            int right = len - left - 1;

            if (left > right) {
                if (right > 0) {
                    vector<int> new_group(a.begin() + mid_p + 1, a.begin() + next_pos);
                    Solver new_solver(std::move(new_group));
                    all_inv.insert(new_solver.inv);
                    solvers.push_back(std::move(new_solver));
                    int new_id = solvers.size() - 1;
                    solver_ids[mid_p + 1] = new_id;
                    solvers_pos.insert(mid_p + 1);
                }
                if (left > 0) {
                    for (int ii = 0; ii < right + 1; ii++) {
                        solvers[id].dec_to();
                    }
                    all_inv.insert(solvers[id].inv);
                }
            } else {
                if (left > 0) {
                    vector<int> new_group(a.begin() + pos, a.begin() + mid_p);
                    Solver new_solver(new_group);
                    all_inv.insert(new_solver.inv);
                    solvers.push_back(std::move(new_solver));
                    int new_id = solvers.size() - 1;
                    solver_ids[pos] = new_id;
                }
                if (right > 0) {
                    for (int ii = 0; ii < left + 1; ii++) {
                        solvers[id].inc_fr();
                    }
                    all_inv.insert(solvers[id].inv);
                    solver_ids[mid_p + 1] = id;
                    solvers_pos.insert(mid_p + 1);
                }
            }
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

#ifdef LOCAL
    run_all_tests(solve);
    // run_single_test(solve, 1);
    // solve();
#else
    solve();
#endif
}

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

詳細信息

Test #1:

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

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: 1181ms
memory: 24084kb

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