QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#157425#7108. Couleurucup-team520#AC ✓2481ms36552kbC++204.7kb2023-09-02 15:19:562023-09-02 15:19:58

Judging History

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

  • [2023-09-02 15:19:58]
  • 评测
  • 测评结果:AC
  • 用时:2481ms
  • 内存:36552kb
  • [2023-09-02 15:19:56]
  • 提交

answer

#include <bits/stdc++.h>     
using namespace std;  

int arr[100005];
std::vector<int> tree[400005]; // Merge Sort Tree

void build_tree(const int node, 
                const int a, 
                const int b)
{
    if (a == b) // Leaf node
    {
        tree[node].push_back(arr[a]);
    }
    else
    {
        int mid = (a + b) / 2;
        
        build_tree(node*2, a, mid);
        build_tree(node*2+1, mid+1, b);
        
        tree[node].reserve(tree[node*2].size() + tree[node*2+1].size());
        std::merge(std::begin(tree[node*2]), 
                   std::end(tree[node*2]), 
                   std::begin(tree[node*2+1]), 
                   std::end(tree[node*2+1]), 
                   std::back_inserter(tree[node]));
    }
}

std::size_t query_tree(const int node, 
                       const int a, 
                       const int b, 
                       const int i, 
                       const int j,
                       const int k)
{
    if(a > b || a > j || b < i) // Out of range
        return 0;
    else if (a >= i && b <= j) // Total Overlap
    {
        return std::distance(std::upper_bound(std::begin(tree[node]), 
                                              std::end(tree[node]), 
                                              k),
                             std::end(tree[node]));
    }
    else
    {
        // Partial overlap
        int mid = (a + b) / 2;
        
        auto left  = query_tree(node*2, a, mid, i, j, k);
        auto right = query_tree(node*2+1, mid+1, b, i, j, k);
            
        return left + right;
    }   
}
#define ll long long
struct FenwickTree{
    vector<ll> bit; 
    ll n;
    FenwickTree(ll n){
        this->n = n;
        bit.assign(n, 0);
    }
    FenwickTree(vector<ll> a):FenwickTree(a.size()){
        ll x=a.size();
        for(size_t i=0;i<x;i++)
            add(i,a[i]);
    }
    ll sum(ll r) {
        ll ret=0;
        for(;r>=0;r=(r&(r+1))-1)
            ret+=bit[r];
        return ret;
    }
    ll sum(ll l,ll r) {
        if(l>r)
            return 0;
        return sum(r)-sum(l-1);
    }
    void add(ll idx,ll delta) {
        for(;idx<n;idx=idx|(idx+1))
            bit[idx]+=delta;
    }
};
int main()
{    
    
    int test; std::scanf("%d", &test);
    while(test--){
        int n; std::scanf("%d", &n);
        for (int i = 0; i < n; ++i)
        std::scanf("%d", &arr[i]);
        FenwickTree get_sum(n+5);
        auto getv=[&](ll l,ll r){
            ll now=0;
            for(ll pos=l;pos<=r;pos++){
                now+=get_sum.sum(arr[pos]+1,n);
                get_sum.add(arr[pos],1);
            }
            for(ll pos=l;pos<=r;pos++){
                get_sum.add(arr[pos],-1);
            }
            return now;
        };
    
        build_tree(1, 0, n-1);
        int queries=n;
        set<ll> bad;
        bad.insert(0); bad.insert(n+1);
        multiset<ll> store_ans;
        map<pair<ll,ll>,ll> store_val;
        store_val[{1,n}]=getv(0,n-1);
        store_ans.insert(getv(0,n-1));
        store_ans.insert(0);
        while(queries--){  
            ll ans=*store_ans.rbegin();
            printf("%lld ", ans);
            ll p; scanf("%lld",&p);
            p^=ans;
            ll l=*(--bad.lower_bound(p));
            ll r=*(bad.lower_bound(p));    
            l++,r--;
            ans=store_val[{l,r}];
            store_ans.erase(store_ans.find(ans));  
            if(abs(p-l)<abs(r-p)){
                ll new_seg_val=getv(l-1,p-2);
                ans-=getv(l-1,p-1);
                for(ll i=l-1;i<=p-1;i++){
                    ans-= r-p-query_tree(1, 0, n-1, p, r-1, arr[i]-1);
                }
                if(p!=l){
                    store_val[{l,p-1}]=new_seg_val;
                    store_ans.insert(new_seg_val);
                }
                if(p!=r){
                    store_val[{p+1,r}]=ans;
                    store_ans.insert(ans);
                }
            }
            else{
                ll new_seg_val=getv(p,r-1);
                ans-=getv(p-1,r-1);  
                for(ll i=p-1;i<=r-1;i++){  
                    ans-= query_tree(1, 0, n-1, l-1, p-2, arr[i]);
                }
                if(p!=r){
                    store_val[{p+1,r}]=new_seg_val;
                    store_ans.insert(new_seg_val);
                }
                if(p!=l){
                    store_val[{l,p-1}]=ans;
                    store_ans.insert(ans);
                }  
            }
            bad.insert(p); 
        }
        for(int i=0;i<(4*n);i++){
            tree[i].clear();
        }
        printf("\n");

    }
    return 0;
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 13468kb

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: 2481ms
memory: 36552kb

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