QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#164678#7108. Couleurnotpasscet_6AC ✓4036ms67752kbC++143.6kb2023-09-05 12:26:062023-09-05 12:26:06

Judging History

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

  • [2023-09-05 12:26:06]
  • 评测
  • 测评结果:AC
  • 用时:4036ms
  • 内存:67752kb
  • [2023-09-05 12:26:06]
  • 提交

answer

// Problem: P3919 【模板】可持久化线段树 1(可持久化数组)
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3919
// Memory Limit: 1 MB
// Time Limit: 1500 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
#define x first
#define y second
const int N = 1e5 + 10;
int a[N], root[N];
int n, m, k;
struct Persistable_Segment_Tree {
    int lc[N * 30], rc[N * 30], val[N * 30], cnt;
    inline void build(int& o, int l, int r) {
        o = ++cnt;
        if (l == r) { return; }
        int mid = (l + r) >> 1;
        build(lc[o], l, mid); build(rc[o], mid + 1, r);
    }
    inline void ins(int& o, int pre, int l, int r, int q) {
        o = ++cnt; lc[o] = lc[pre]; rc[o] = rc[pre]; val[o] = val[pre] + 1;
        if (l == r) { return; }
        int mid = (l + r) >> 1;
        if (q <= mid)ins(lc[o], lc[pre], l, mid, q);
        else ins(rc[o], rc[pre], mid + 1, r, q);
    }
    inline int query(int o, int l, int r, int ql, int qr) {
    	if(ql > qr) return 0;
    	if(ql <= l && qr >= r){
    		return val[o];
    	}
        int mid = (l + r) >> 1, res = 0;
        if (ql <= mid) res += query(lc[o], l, mid, ql, qr);
        if (qr > mid) res += query(rc[o], mid + 1, r, ql, qr);
        return res;
    }
    
    inline void clear(){
    	for(int i = 0; i <= cnt; i++){
    		lc[i] = rc[i] = val[i] = 0;
    	}
    	cnt = 0;
    }
}T;

map<int, int> mp;
multiset<int> maxv;

void split(int l, int r, int x){
	int base = mp[l - 1]; // [l ~ r]整个区间的逆序对
	maxv.erase(maxv.lower_bound(base));
	int t1 = 0, t2 = 0; 
	// t1是 (l ~ x) 或者 (x ~ r) 内的逆序对, 
	// t2是跨了两个区间的逆序对
	// cout << l << ' ' << r << ' ' << x << '\n'; 
 	if(x - l < r - x){
		for(int i = l + 1; i <= x; i++){
			t1 += T.query(root[i - 1], 1, n, a[i] + 1, n)
				- T.query(root[l - 1], 1, n, a[i] + 1, n);
		}
		for(int i = l; i <= x; i++){
			t2 += T.query(root[r], 1, n, 1, a[i] - 1)
				- T.query(root[x], 1, n, 1, a[i] - 1);
		}
		
		int temp = T.query(root[x - 1], 1, n, a[x] + 1, n)
			- T.query(root[l - 1], 1, n, a[x] + 1, n);
		
		mp[l - 1] = t1 - temp, mp[x] = base - t1 - t2;
		maxv.insert(mp[l - 1]); maxv.insert(mp[x]);
	}
	else{
		  for(int i = x + 1; i <= r; i++){
		  	t1 += T.query(root[i - 1], 1, n, a[i] + 1, n)
		  		- T.query(root[x - 1], 1, n, a[i] + 1, n);
		  }
		  for(int i = x; i <= r; i++){
		  	t2 += T.query(root[x - 1], 1, n, a[i] + 1, n)
		  		- T.query(root[l - 1], 1, n, a[i] + 1, n);
		  }
		  
		  int temp = T.query(root[r], 1, n, 1, a[x] - 1)
		  	- T.query(root[x], 1, n, 1, a[x] - 1);
		  mp[x] = t1 - temp, mp[l - 1] = base - t1 - t2;
		  maxv.insert(mp[x]); maxv.insert(mp[l - 1]);
	}
}
void solve(){
	mp.clear(); maxv.clear();
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
	}
	T.build(root[0], 1, n);
	for(int i = 1; i <= n; i++){
		T.ins(root[i], root[i - 1], 1, n, a[i]);
	}
	
	int base = 0;
	for(int i = 2; i <= n; i++){
		base += T.query(root[i - 1], 1, n, a[i] + 1, n);
	}
	mp[0] = base, mp[n + 1] = 0;
	maxv.insert(base);
	
	int ans = *maxv.rbegin();
	for(int i = 1; i <= n; i++){
		cout << ans << " \n"[i == n];
		int x; cin >> x;
		x ^= ans;
		auto it = mp.lower_bound(x);
		split((prev(it)->x) + 1, (it->x) - 1, x);
		ans = *maxv.rbegin();
		
	}
	T.clear();
}
signed main(){
    std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int t = 1;
    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: 2ms
memory: 7756kb

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: 4036ms
memory: 67752kb

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