QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#231058#7108. CouleurUFRJ#AC ✓2034ms23348kbC++204.0kb2023-10-29 00:07:532023-10-29 00:07:54

Judging History

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

  • [2023-10-29 00:07:54]
  • 评测
  • 测评结果:AC
  • 用时:2034ms
  • 内存:23348kb
  • [2023-10-29 00:07:53]
  • 提交

answer

#include<bits/stdc++.h>
#include<bits/extc++.h>

using namespace std;

using lint = int64_t;


template<typename K, typename V, typename Comp = less<K>>
using ordered_map = __gnu_pbds::tree<K, V, Comp, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;

template<typename K, typename Comp = less<K>>
using ordered_set = ordered_map<K, __gnu_pbds::null_type, Comp>;

template<typename T> struct FT {
    vector<T> s;
    FT(int n=0) : s(n) {}
    FT(const vector<T> &A) : s(int(A.size())) {
        const int N = int(A.size());
        for(int pos = 0; pos < N; ++pos) {
            s[pos] += A[pos];
            int nxt = (pos | (pos + 1));
            if(nxt < N) s[nxt] += s[pos];
        }
    }

    void update(int pos, T dif) {
        for(; pos < (int) s.size(); pos |= pos + 1) s[pos] += dif;
    }

    T query(int pos) {
        T res = 0;
        for(; pos > 0; pos &= pos - 1) res += s[pos-1];        
        return res;
    }
};

FT<lint> ft(100005);
vector<lint> a;

lint count_inv(int l, int r){
    if(l >= r) return 0;
    lint t = 0;
    for(int i = r; i>=l; i--){
        t += ft.query(a[i]);
        ft.update(a[i], 1);
    }
    for(int i = r; i>=l; i--){
        ft.update(a[i], -1);
    }
    return t;
}

using pii = pair<int, int>;

const int inf = 1e7;

void solve(){
    int n;
    cin>>n;
    a.resize(n);
    for(auto &x : a) cin>>x;
    map<pair<int, int>, lint> inv;
    map<lint, int> freq;
    inv[{0, n-1}] = count_inv(0, n-1);
    freq[inv[{0, n-1}]]++;
    lint z = 0;
    vector<ordered_set<pii>> S(n+1);
    for(int i = 0; i < n; i++) S[0].insert({a[i], i});
    for(int _ = 0; _ < n; _++){
        assert(!freq.empty());
        z = freq.rbegin()->first;
        cout<<z;
        if(_ != n-1) cout<<" ";
        lint p; cin>>p;
        p = p ^ z;
        p--;
        auto it = inv.upper_bound({p+1, -1});
        assert(it != inv.begin());
        it--;
        auto [interval, T] = *it;
        auto [l, r] = interval;
        freq[T]--;
        if(freq[T] == 0) freq.erase(T);
        inv.erase(it);
        int sz_left = p - l;
        int sz_right = r - (p+1) + 1;

        if(l == r){}
        else if(sz_left < sz_right){
            lint inv_lef = count_inv(l, p-1);
            lint inv_right = T - count_inv(l, p);
            ordered_set<pii> small;
            ordered_set<pii> &big = S[l];
            for(int i = l; i<=p; i++){
                small.insert({a[i], i});
                big.erase({a[i], i});
            }
            
            for(int i = l; i<=p; i++){
                inv_right -= big.order_of_key({a[i], -1});
            }
            small.erase({a[p], p});
            if(p+1 <= r){
                S[p+1].swap(big);
                inv[{p+1, r}] = inv_right;
                freq[inv_right]++;
            }
            if(l <= p-1){
                S[l] = small;
                inv[{l, p-1}] = inv_lef;
                freq[inv_lef]++;
            }

        } else {
            lint inv_right = count_inv(p+1, r);
            lint inv_lef = T - count_inv(p, r);
            ordered_set<pii> small;
            ordered_set<pii> &big = S[l];
            for(int i = p; i<=r; i++){
                small.insert({a[i], i});
                big.erase({a[i], i});
            }
          
            for(int i = p; i<=r; i++){
                inv_lef -= sz_left - big.order_of_key({a[i], inf});
            }
          
            small.erase({a[p], p});
            //S[l].swap(big);cout
            if(l <= p-1){
                inv[{l, p-1}] = inv_lef;
                freq[inv_lef]++;
            }
            if(p+1 <= r){
                S[p+1] = small;
                inv[{p+1, r}] = inv_right;
                freq[inv_right]++;
            }
        }
     
    } 
    cout<<"\n";
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int t; 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: 1ms
memory: 3936kb

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: 2034ms
memory: 23348kb

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