QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#163302#7108. Couleurucup-team061RE 2ms3516kbC++203.5kb2023-09-03 23:59:342023-09-03 23:59:34

Judging History

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

  • [2023-09-03 23:59:34]
  • 评测
  • 测评结果:RE
  • 用时:2ms
  • 内存:3516kb
  • [2023-09-03 23:59:34]
  • 提交

answer

#include <bits/stdc++.h>
//#define int long long
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define pb push_back
#define eb emplace_back
#define eps 1e-6
using namespace std;
typedef long long ll;
const int N = 1e5 + 10, M = 1e7 + 10;
int n, m, q, mx;
int t[M], ls[M], rs[M], rt[N], tot;
int update(int pre, int l, int r, int pos, int x){
    int p = ++ tot;
    t[p] = t[pre] + x;
    ls[p] = ls[pre], rs[p] = rs[pre];
    if(l == r) return p;
    int mid = (l + r) >> 1;
    if(pos <= mid) ls[p] = update(ls[pre], l, mid, pos, x);
    else rs[p] = update(rs[pre], mid + 1, r , pos, x);
    return p;
}
int query(int p, int l, int r, int ql, int qr){
    if(ql > r || qr < l) return 0;
    if(ql <= l && qr >= r) return t[p];
    int mid = (l + r) >> 1;
    return query(ls[p], l, mid, ql, qr) + query(rs[p], mid + 1, r, ql, qr);
}
int a[N], p[N];
int cal_rev(int l, int r){
    int s = 0;
    for(int i = l + 1; i <= r; i++){
        s += query(rt[i], 1, mx, a[i] + 1, mx) - query(rt[l - 1], 1, mx, a[i] + 1, mx);
    }
    return s;
}
int cal_sum(int l1, int r1, int l2, int r2, int tp){
    int s = 0;
    if(tp == 1){//遍历左区间,从右区间找比左边元素小的个数
        for(int i = l1; i <= r1; i++){
            s += query(rt[r2], 1, mx, 1, a[i] - 1) - query(rt[l2 - 1], 1, mx, 1, a[i] - 1);
        }
    }else{//遍历右区间,从左区间找比右边元素大的个数
        for(int i = l2; i <= r2; i++){
            s += (query(rt[r1], 1, mx, a[i] + 1, mx) - query(rt[l1 - 1], 1, mx, a[i] + 1, mx));
        }
    }
    return s;
}
void clear_all(){
    for(int i = 0; i <= n; i++) rt[i] = 0;
    for(int i = 0; i <= tot; i++) t[i] = 0, ls[i] = 0, rs[i] = 0;
    tot = 0, mx = 0;
}
void solve(){
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i], mx = max(mx, a[i]);
    for(int i = 1; i <= n; i++) cin >> p[i];
    int s = 0;
    for(int i = 1; i <= n; i++){
        rt[i] = update(rt[i - 1], 1, mx, a[i], 1);
        s += i - query(rt[i], 1, mx, 1, a[i]);
    }
    set<array<int, 3>> seg;//记录区间左端点
    multiset<int> sum;
    sum.insert(s);
    seg.insert({1, n, s});
    for(int i = 1; i <= n; i++){
        s = *sum.rbegin();
        cout << s << ' ';
        p[i] ^= s;
        auto pos = prev(seg.lower_bound({p[i] + 1, 0, 0}));
        auto [l, r, stot] = *pos;
        seg.erase(pos);
        sum.erase(sum.find(stot));
        if(l == r) continue;
        //cout << "\n ## No." << i << ":  " << l << ' ' << r << '\n';
        int sl = 0, sr = 0, smid = 0;
        if(p[i] - l <= r - p[i]){//左边小
            sl = cal_rev(l, p[i] - 1);
            smid = cal_sum(l, p[i] - 1, p[i] + 1, r, 1) + cal_sum(p[i], p[i], p[i] + 1, r, 1) + cal_sum(l, p[i] - 1, p[i], p[i], 2);
            sr = stot - sl - smid;
        }else{//右边小
            sr = cal_rev(p[i] + 1, r);
            smid = cal_sum(l, p[i] - 1, p[i] + 1, r, 2) + cal_sum(p[i], p[i], p[i] + 1, r, 1) + cal_sum(l, p[i] - 1, p[i], p[i], 2);
            sl = stot - sr - smid;
        }
        //cout << "p = " << p[i] << " sl =  " << sl << " sr = " << sr << " smid = " << smid << '\n';
        seg.insert({l, p[i] - 1, sl}), sum.insert(sl); 
        seg.insert({p[i] + 1, r, sr}), sum.insert(sr);
    }
    cout << '\n';
    clear_all();
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int T = 1;
    cin >> T;
    while(T--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
Runtime Error

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: