QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#231027#7108. CouleurUFRJ#TL 4ms18636kbC++203.9kb2023-10-28 23:23:382023-10-28 23:23:39

Judging History

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

  • [2023-10-28 23:23:39]
  • 评测
  • 测评结果:TL
  • 用时:4ms
  • 内存:18636kb
  • [2023-10-28 23:23:38]
  • 提交

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(2000005);
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 = inv[{0, n-1}];
    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({l, r});
        for(int i = l; i<p; i++) if(a[i] > a[p]) T--;
        for(int i = p+1;i<=r; i++) if(a[i] < a[p]) T--;
        if(l == r) continue;
        if(p == l){
            lint inv_right = T;
            inv[{l+1, r}] = inv_right;
            freq[inv_right]++;
            continue;
        } 
        if(p == r){
            lint inv_lef = T;
            inv[{l, r-1}] = inv_lef;
            freq[inv_lef]++;
            continue;
        } 
        int sz_left = p - l;
        int sz_right = r - (p+1) + 1;
        
        if(sz_left < sz_right){
            lint inv_lef = count_inv(l, p-1);
            lint inv_right = T - inv_lef;
            ordered_set<pii> S;
            for(int i = l; i<p; i++){
                S.insert({a[i], i});
            }
            int L = S.size();
            for(int i = p+1; i<=r; i++){
                inv_right -= L - S.order_of_key({a[i], inf});
            }
            inv[{l, p-1}] = inv_lef;
            inv[{p+1, r}] = inv_right;
            freq[inv_lef]++;
            freq[inv_right]++;
        } else {
            lint inv_right = count_inv(p+1, r);
            lint inv_lef = T - inv_right;
            ordered_set<pii> S;
            for(int i = p+1; i<=r; i++){
                S.insert({a[i], i});
            }
            for(int i = l; i<p; i++){
                inv_lef -= S.order_of_key({a[i], -1});
            }
            inv[{l, p-1}] = inv_lef;
            inv[{p+1, r}] = inv_right;
            freq[inv_lef]++;
            freq[inv_right]++;
        }
        // for(auto [interval, T] : inv){
        //     //cout<<"( " <<interval.first<<", "<<interval.second<<") "<<T<<endl;
        // }
        //cout<<endl;
    } 
    cout<<"\n";
    //cout<<"-----"<<endl;
}

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

详细

Test #1:

score: 100
Accepted
time: 4ms
memory: 18636kb

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: -100
Time Limit Exceeded

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: