QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#157489#7108. Couleurucup-team198#AC ✓1343ms33992kbC++203.5kb2023-09-02 15:25:192023-09-02 15:25:20

Judging History

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

  • [2023-09-02 15:25:20]
  • 评测
  • 测评结果:AC
  • 用时:1343ms
  • 内存:33992kb
  • [2023-09-02 15:25:19]
  • 提交

answer

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
using namespace std;

#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
#define debug(...)
#include<debug>
#else
#define debug(...)
#endif

constexpr int N = 1e5 + 10,  M = N<<5;

int t[N], tot;
int lch[M], rch[M], cnt[M];

void cpy(int from,int to) {
    lch[from]=lch[to];
    rch[from]=rch[to];
    cnt[from]=cnt[to];
}

void change(int &u,int old,int l,int r,int pos){
    u=++tot;
    cpy(u,old);
    if(l==r) {
        cnt[u]++;
        return;
    }
    int mid=(l+r)>>1;
    if(pos<=mid)
        change(lch[u],lch[u],l,mid,pos);
    else {
        change(rch[u], rch[u],mid+1,r,pos);
    }
    cnt[u]=cnt[lch[u]]+cnt[rch[u]];
}

int query(int s,int t,int l,int r,int ql,int qr) {
    // cerr<<l<<" "<<r<<endl;
    if(l==ql && r==qr) {
        return cnt[t]-cnt[s];
    }
    int mid=(l+r)>>1;
    if(qr<=mid)
        return query(lch[s],lch[t],l,mid,ql,qr);
    if(ql>mid)
        return query(rch[s],rch[t],mid+1,r,ql,qr);
    return query(lch[s],lch[t],l,mid,ql,mid)
        +query(rch[s], rch[t], mid+1, r, mid+1, qr);
}

int n,a[N];
int64_t p[N];
int64_t num[N];
int query(int s,int t0,int l,int r) {
    if(s>t0 || l>r) return 0;
    return query(t[s-1],t[t0],1,n,l,r);
}

void solve() {
    cin>>n;
    tot=0;
    int64_t inv=0;
    for(int i=1; i<=n; ++i) {
        cin>>a[i];
        change(t[i],t[i-1],1,n,a[i]);
        inv+=query(1,i-1,a[i]+1,n);
        // cerr<<inv<<"\n";
    }

    for(int i=1; i<=n; ++i) {
        cin>>p[i];
    }
    
    set<pair<int,int>> inter;
    set<pair<int64_t,int>> all;
    inter.emplace(1,n);
    all.emplace(inv,1), num[1]=inv;

    int64_t z=inv;
    for(int i=1; i<=n; ++i) {
        // cerr<<z<<endl;
        if(i>1) cout<<" ";
        cout<<z;
        p[i]^=z;
        debug(p[i]);

        auto it=--inter.upper_bound({p[i],1e9});
        auto [l,r]=*it;
        auto invNum=num[l];
        auto it1=all.lower_bound({invNum,l});
        debug(l,r,p[i]);

        all.erase(it1);
        inter.erase(it);

        if(p[i]-l<=r-p[i]) {
            int64_t num0=0,numlr=0;
            for(int j=l; j<p[i]; ++j) {
                num0+=query(j+1,p[i]-1,1,a[j]-1);
                numlr+=query(j+1,r,1,a[j]-1);
                debug(numlr);
            }
            numlr+=query(p[i]+1,r,1,a[p[i]]-1);
            debug(num0,numlr);


            if(l<p[i]) {
                all.emplace(num0,l), num[l]=num0;
                inter.emplace(l,p[i]-1);
            }
            if(p[i]<r) {
                all.emplace(invNum-numlr,p[i]+1), num[p[i]+1]=invNum-numlr;
                inter.emplace(p[i]+1,r);
            }
        }else {
            int64_t num0=0,numlr=0;
            for(int j=p[i]+1; j<=r; ++j) {
                num0+=query(p[i]+1,j-1,a[j]+1,n);
                numlr+=query(l,j-1,a[j]+1,n);
            }
            numlr+=query(l,p[i]-1,a[p[i]]+1,n);

            
            if(l<p[i]) {
                all.emplace(invNum-numlr,l), num[l]=invNum-numlr;
                inter.emplace(l,p[i]-1);
            }
            if(p[i]<r) {
                all.emplace(num0,p[i]+1), num[p[i]+1]=num0;
                inter.emplace(p[i]+1,r);
            }
        }
        if(i==n) break;
        z=all.rbegin()->first;
    }
    cout<<"\n";
}
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int T;
    cin>>T;
    while(T--) {
        solve();
    }
}

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

详细

Test #1:

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

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: 1343ms
memory: 33992kb

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