QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#183961#7108. CouleurBUET_POISSONAC ✓2321ms23300kbC++234.5kb2023-09-20 04:09:252023-09-20 04:09:25

Judging History

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

  • [2023-09-20 04:09:25]
  • 评测
  • 测评结果:AC
  • 用时:2321ms
  • 内存:23300kb
  • [2023-09-20 04:09:25]
  • 提交

answer

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define getbit(n, i) (((n) & (1LL << (i))) != 0) 
#define setbit0(n, i) ((n) & (~(1LL << (i)))) 
#define setbit1(n, i) ((n) | (1LL << (i))) 
#define togglebit(n, i) ((n) ^ (1LL << (i))) 
#define lastone(n) ((n) & (-(n))) 
char gap = 32;
 
#define int long long 
#define ll long long 
#define lll __int128_t
#define pb push_back
typedef tree<
pair<int, int>,
null_type,
less<pair<int, int>>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll hashPrime = 1610612741;
void solve() {
    int n; cin >> n;
    int a[n + 1]; 
    for(int i = 1; i <= n; i++) cin >> a[i];
    int contribution[n + 1];
    memset(contribution, 0, sizeof(contribution));

    vector<ordered_set> st(n + 1);
    for(int i = n; i >= 1; i--) {
        
        contribution[i] = st[1].order_of_key({a[i], -1});
        st[1].insert({a[i], i});
    
    }
    multiset<int, greater<int>> maxi;
    vector<int> ans(n + 1);
    for(int i = 1; i <= n; i++) {
        ans[1] += contribution[i];
    }
    
    maxi.insert(ans[1]);
    vector<int> p(n + 1); 
    multiset<pair<int, int>> ms;
    ms.insert({1, n});

    for(int i = 1; i <= n; i++) {
        cin >> p[i];
        int maxans = 0;
        if(!maxi.empty()) maxans = *maxi.begin();
        int idx = (p[i] ^ maxans);
        if(i > 1) cout << " ";
        cout << maxans;
        
        if(i == n) {
            break;
        }

        auto it = ms.upper_bound({idx, INT_MAX});
        it--;
        
        int l = it->first;  
        int r = it->second;
        
        ms.erase(it);
    
        int segment_ans = ans[l];
        maxi.erase(maxi.find(ans[l]));

        if(l == r) {
            ans[l] = 0;
            st[l].clear();
            continue;
        }


        if(idx - l < r - idx) {
            st[l].swap(st[idx + 1]);
            vector<pair<int, int>> temp;
            for(int i = l; i < idx; i++) {
                st[idx + 1].erase({a[i], i});
                temp.push_back({a[i], i});
            }
            int reduced = 0;
            for(int i = l; i <= idx; i++) {
                reduced += st[idx + 1].order_of_key({a[i], -1});
            }
            st[idx + 1].erase({a[idx], idx});
            segment_ans -= reduced;
            for(int i = idx - 1; i >= l; i--) {
                contribution[i] = st[l].order_of_key({a[i], -1});
                st[l].insert({a[i], i});
            }
            int left_ans = 0;
            for(int i = l; i < idx; i++) {
                left_ans += contribution[i];
            }
            if(idx > l) {
                ms.insert({l, idx - 1});
                ans[l] = left_ans;
                maxi.insert(ans[l]);
            }
            if(idx + 1 <= r) {
                ms.insert({idx + 1, r});
                ans[idx + 1] = segment_ans - left_ans;
                maxi.insert(ans[idx + 1]);
            }
            
        }
        else {
            vector<pair<int, int>> temp;
            for(int i = idx + 1; i <= r; i++) {
                st[l].erase({a[i], i});
                temp.push_back({a[i], i});
            }
            
            int reduced = 0;
            for(int i = idx; i <= r; i++) {
                reduced += ((int)st[l].size() - st[l].order_of_key({a[i], INT_MAX}));
            }
            st[l].erase({a[idx], idx});
            segment_ans -= reduced;
            for(int i = r; i > idx; i--) {
                contribution[i] = st[idx + 1].order_of_key({a[i], -1});
                st[idx + 1].insert({a[i], i});
            }
            int right_ans = 0;
            for(int i = idx + 1; i <= r; i++) {
                right_ans += contribution[i];
            }
            if(idx > l) {
                ms.insert({l, idx - 1});
                ans[l] = segment_ans - right_ans;
                maxi.insert(ans[l]);
            }
            if(idx + 1 <= r) {
                ms.insert({idx + 1, r});
                ans[idx + 1] = right_ans;
                maxi.insert(ans[idx + 1]);
            }
        } 

    }
    cout << "\n";
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t; cin >> t;
    while(t--) {
        solve();
    }
}

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

详细

Test #1:

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

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: 2321ms
memory: 23300kb

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