QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#732036#7108. CouleurIUT_BhalocheleraAC ✓2197ms57508kbC++234.7kb2024-11-10 12:48:362024-11-10 12:48:37

Judging History

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

  • [2024-11-10 12:48:37]
  • 评测
  • 测评结果:AC
  • 用时:2197ms
  • 内存:57508kb
  • [2024-11-10 12:48:36]
  • 提交

answer

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define PLL pair<long long, long long>
#define LL long long

#define faster {ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}
#define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define all(v) v.begin(),v.end()

const LL mod = 998244353;
const int N = 1e6 + 10;
LL inf = 1e17 + 1;


class PST{
    #define lc(u) tree[u].left
    #define rc(u) tree[u].right;
    struct node{
        int left = 0, right = 0, val = 0;
    };
    node *tree;
    int N, LG, time = 0, I = 0;

    node create(int l, int r){
        return {l, r, merge(tree[l].val, tree[r].val)};
    }
    int merge(LL a, LL b){ 
        return a + b;
    }
    int build(int le, int ri){
        int id = ++time;
        if(le == ri) return tree[id] = node(), id;
        int m = (le + ri) / 2;
        return tree[id] = create(build(le, m), build(m + 1, ri)), id;
    }
    int update(int id, int le, int ri, int pos, int val){
        int nid = ++time;
        if(le == ri) return tree[nid] = {0, 0, (tree[id].val + val)}, nid; // change here
        int m = (le + ri) / 2;
        if(pos <= m){
            tree[nid] = create(update(tree[id].left, le, m, pos, val), tree[id].right);
        }else{
            tree[nid] = create(tree[id].left, update(tree[id].right, m + 1, ri, pos, val));
        }
        return nid;
    }
    int query(int id, int le, int ri, int l, int r){
        if (l <= le && r >= ri) {
            return tree[id].val;
        }
        if (r < le || l > ri) {
            return I;
        }
        int m = (le + ri) >> 1;      
        return merge(query(tree[id].left, le, m, l, r), query(tree[id].right, m + 1, ri, l, r));
    }
    
public:
    PST(int N, int U){ // U --> number of expected updates
        this->N = N;
        LG = 33 - __builtin_clz(N);
        tree = new node[N * 4 + U * LG];
        build(0, N - 1); 
    }
    int update(int id, int pos, int val){
        return update(id, 0, N - 1, pos, val);
    }
    int query(int id, int l, int r){
        if(l > r) return I;
        return query(id, 0, N - 1, l, r);
    }
    ~PST(){
        delete[] tree;
    }
};

void solve (int tc) {
    int n; cin>>n; 

    multiset<LL> allInv;
    set<pair<int, int>> ranges; ranges.insert({1, n});
    vector<int> a(n + 1), root(n + 1);
    vector<LL> ansInRange(n + 1);
    LL ans = 0;
    PST tree(n + 10, n);
    for(int i = 1; i <= n; i++){
        cin>>a[i];
        int large = tree.query(root[i - 1], a[i] + 1, n);
        ans += large;
        root[i] = tree.update(root[i - 1], a[i], 1);
    }
    allInv.insert(ans);
    ansInRange[1] = ans;
    for(int i = 0; i < n; i++){
        LL p; cin>>p; 
        p ^= ans; 
        cout<<ans<<' ';
        
        auto [l, r] = *prev (ranges.upper_bound ({p, n}));
        allInv.erase(allInv.find(ansInRange[l]));
        ranges.erase({l, r});
        if(l == r) continue;
        if(r - p > p - l){
            LL left = 0, right = ansInRange[l];
            for(int j = l; j < p; j++){
                right -= (tree.query(root[r], 0, a[j] - 1) - tree.query(root[p - 1], 0, a[j] - 1));
                left += (tree.query(root[j], a[j] + 1, n) - tree.query(root[l - 1], a[j] + 1, n));
            }
            right -= (tree.query(root[r], 0, a[p] - 1) - tree.query(root[p - 1], 0, a[p] - 1));

            right -= left;

            ansInRange[l] = left;
            allInv.insert(ansInRange[l]); 
            if(p != r) ansInRange[p + 1] = right;
            if(p != r) allInv.insert(ansInRange[p + 1]);

        }else{
            LL left = ansInRange[l], right = 0;
            for(int j = p + 1; j <= r; j++){
                left -= (tree.query(root[p], a[j] + 1, n) - tree.query(root[l - 1], a[j] + 1, n));
                right += (tree.query(root[j], a[j] + 1, n) - tree.query(root[p], a[j] + 1, n));
            }
            left -= (tree.query(root[p], a[p] + 1, n) - tree.query(root[l - 1], a[p] + 1, n));

            left -= right;
            
            ansInRange[l] = left;
            allInv.insert(ansInRange[l]); 
            if(p != r) ansInRange[p + 1] = right;
            if(p != r) allInv.insert(ansInRange[p + 1]);
        }
        if(p > l)
            ranges.insert({l, p - 1});
        if(r > p)
            ranges.insert({p + 1, r});

        auto it = allInv.end(); it--;
        ans = *it;
    }
    cout<<'\n';
}

signed main() {  
    faster
    int t = 1;
    cin >> t;
    for (int tc = 1; tc <= t; tc++) {
        solve(tc);
    }
    return 0;
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 2197ms
memory: 57508kb

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