QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#161108#7108. Couleurucup-team004#AC ✓1499ms162712kbC++203.6kb2023-09-02 22:30:092023-09-02 22:30:09

Judging History

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

  • [2023-09-02 22:30:09]
  • 评测
  • 测评结果:AC
  • 用时:1499ms
  • 内存:162712kb
  • [2023-09-02 22:30:09]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

constexpr int N = 4E6;

struct Node {
    int l = 0;
    int r = 0;
    int cnt = 0;
    i64 sum = 0;
    int max = -1E9;
    int min = 1E9;
    int tag = 0;
};

int tot = 0;
std::vector<Node> t(N);

void pull(int p) {
    t[p].cnt = t[t[p].l].cnt + t[t[p].r].cnt;
    t[p].sum = t[t[p].l].sum + t[t[p].r].sum;
    t[p].max = std::max(t[t[p].l].max, t[t[p].r].max);
    t[p].min = std::min(t[t[p].l].min, t[t[p].r].min);
}

void add(int p, int v) {
    if (!p) {
        return;
    }
    t[p].sum += 1LL * t[p].cnt * v;
    t[p].tag += v;
}

void push(int p) {
    if (t[p].tag) {
        add(t[p].l, t[p].tag);
        add(t[p].r, t[p].tag);
    }
    t[p].tag = 0;
}

void add(int &p, int l, int r, int x, int y, int i) {
    if (!p) {
        p = ++tot;
        t[p] = {};
    }
    if (r - l == 1) {
        t[p].cnt += 1;
        t[p].sum += y;
        t[p].min = t[p].max = i;
        return;
    }
    int m = (l + r) / 2;
    push(p);
    if (x < m) {
        add(t[p].l, l, m, x, y, i);
    } else {
        add(t[p].r, m, r, x, y, i);
    }
    pull(p);
}

int query(int p, int l, int r, int x, int y) {
    if (!p || l >= y || r <= x) {
        return 0;
    }
    if (l >= x && r <= y) {
        return t[p].cnt;
    }
    int m = (l + r) / 2;
    push(p);
    return query(t[p].l, l, m, x, y) + query(t[p].r, m, r, x, y);
}

std::pair<int, int> split(int p, int v) {
    if (t[p].min >= v) {
        return {0, p};
    }
    if (t[p].max < v) {
        return {p, 0};
    }
    push(p);
    int q = ++tot;
    t[q] = {};
    auto [a, b] = split(t[p].l, v);
    auto [c, d] = split(t[p].r, v);
    t[p].l = a;
    t[p].r = c;
    t[q].l = b;
    t[q].r = d;
    add(b, -t[c].cnt);
    pull(p);
    pull(q);
    return {p, q};
}

void solve() {
    int n;
    std::cin >> n;
    
    tot = 0;
    
    std::vector<int> a(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
        a[i]--;
    }
    std::vector<int> o(n);
    std::iota(o.begin(), o.end(), 0);
    std::sort(o.begin(), o.end(),
        [&](int i, int j) {
            return a[i] < a[j] || (a[i] == a[j] && i < j);
        });
    for (int i = 0; i < n; i++) {
        a[o[i]] = i;
    }
    
    int rt = 0;
    for (int i = 0; i < n; i++) {
        int v = query(rt, 0, n, a[i] + 1, n);
        add(rt, 0, n, a[i], v, i);
    }
    
    std::map<int, int> seg;
    seg[0] = rt;
    seg[n] = -1;
    std::multiset<i64> s{t[rt].sum};
    
    i64 ans;
    for (int i = 0; i < n; i++) {
        ans = *s.rbegin();
        std::cout << ans << " \n"[i == n - 1];
        
        i64 p;
        std::cin >> p;
        p ^= ans;
        p--;
        
        if (p < 0 || p >= n) {
            continue;
        }
        
        auto it = std::prev(seg.upper_bound(p));
        int l = it->first, r = l + t[it->second].cnt;
        int nd = it->second;
        seg.erase(it);
        // std::cerr << "(" << l << ", " << r << ")\n";
        
        s.extract(t[nd].sum);
        auto [L, MR] = split(nd, p);
        auto [M, R] = split(MR, p + 1);
        // std::cerr << p << " " << t[L].sum << " " << t[R].sum << "\n";
        if (L) {
            s.insert(t[L].sum);
            seg[l] = L;
        }
        if (R) {
            s.insert(t[R].sum);
            seg[p + 1] = R;
        }
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int t;
    std::cin >> t;
    
    while (t--) {
        solve();
    }
    
    return 0;
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 8ms
memory: 159372kb

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: 1499ms
memory: 162712kb

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