QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#449094#7108. CouleurOneWanAC ✓1731ms27896kbC++233.6kb2024-06-20 17:10:382024-06-20 17:10:38

Judging History

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

  • [2024-06-20 17:10:38]
  • 评测
  • 测评结果:AC
  • 用时:1731ms
  • 内存:27896kb
  • [2024-06-20 17:10:38]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int __OneWan_2024 = [](){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    return 0;
}();
struct Node {
	int Lrt, Rrt;
	int sum;
} tr[100005 << 5];
int tot;
int a[100005];
int update(int old, int L, int R, int pos) {
	int now = ++tot;
	tr[now] = tr[old];
	tr[now].sum++;
	if (L == R) {
		return now;
	}
	int mid = L + R >> 1;
	if (pos <= mid) {
		tr[now].Lrt = update(tr[now].Lrt, L, mid, pos);
	} else {
		tr[now].Rrt = update(tr[now].Rrt, mid + 1, R, pos);
	}
	return now;
}
int query(int QL, int QR, int L, int R, int VL, int VR) {
	if (VL > VR) {
		return 0;
	}
    if (VL <= L && R <= VR) {
        return tr[QR].sum - tr[QL].sum;
    }
    int mid = L + R >> 1;
    int res = 0;
    if (VL <= mid) {
        res += query(tr[QL].Lrt, tr[QR].Lrt, L, mid, VL, VR);
    }
    if (VR >= mid + 1) {
        res += query(tr[QL].Rrt, tr[QR].Rrt, mid + 1, R, VL, VR);
    }
    return res;
}
int rt[100005];
struct Result {
	int L, R;
	i64 res;
	friend bool operator<(const Result &x, const Result &y) {
		return x.L < y.L;
	}
};
void solve() {
	int n;
    cin >> n;
    for (int i = 1 ; i <= n ; i++) {
        cin >> a[i];
    }
    tot = 0;
    for (int i = 1 ; i <= n ; i++) {
    	rt[i] = update(rt[i - 1], 1, n, a[i]);
    }
    multiset<i64> ans;
    set<Result> st;
    {
    	i64 res = 0;
    	for (int i = 1 ; i <= n ; i++) {
    		res += query(rt[0], rt[i], 1, n, a[i] + 1, n);
    	}
    	ans.insert(res);
    	st.insert({1, n, res});
    }
    for (int i = 1 ; i <= n ; i++) {
    	i64 t = 0;
    	if (!ans.empty()) {
    		t = *rbegin(ans);
    	}
    	cout << t << " ";
    	i64 tx;
    	cin >> tx;
    	tx ^= t;
    	int x = tx;
    	if (i == n) break;
    	auto it = st.upper_bound({x, x, -1});
    	if (it == begin(st)) {
    		continue;
    	}
    	it--;
    	auto [L, R, res] = *it;
    	st.erase(it);
    	ans.extract(res);
    	if (x - L <= R - x) {
    		i64 resL = 0;
    		for (int j = L ; j < x ; j++) { // 暴力计算左半部分的逆序对, 主席树快速计算左半对右半的贡献
    			resL += query(rt[L - 1], rt[j], 1, n, a[j] + 1, n);
    			res -= query(rt[x], rt[R], 1, n, 1, a[j] - 1);
    		}
    		res -= resL; // 减去左半部分的逆序对
    		res -= query(rt[L - 1], rt[x], 1, n, a[x] + 1, n); // 减去 x 对左半部分的逆序对
    		res -= query(rt[x], rt[R], 1, n, 1, a[x] - 1); // 减去 x 对右半部分的逆序对
    		// [L, x - 1]
    		if (L <= x - 1) {
    			st.insert({L, x - 1, resL});
    			ans.insert(resL);
    		}
    		// [x + 1, R]
    		if (x + 1 <= R) {
    			st.insert({x + 1, R, res});
    			ans.insert(res);
    		}
    	} else {
    		i64 resR = 0;
    		for (int j = x + 1 ; j <= R ; j++) { // 暴力计算右半部分的逆序对, 主席树快速计算右半对左半的贡献
    			resR += query(rt[j], rt[R], 1, n, 1, a[j] - 1);
    			res -= query(rt[L - 1], rt[x - 1], 1, n, a[j] + 1, n);
    		}
    		res -= resR; // 减去右半部分的逆序对
    		res -= query(rt[L - 1], rt[x], 1, n, a[x] + 1, n); // 减去 x 对左半部分的逆序对
    		res -= query(rt[x], rt[R], 1, n, 1, a[x] - 1); // 减去 x 对右半部分的逆序对
    		// [L, x - 1]
    		if (L <= x - 1) {
    			st.insert({L, x - 1, res});
    			ans.insert(res);
    		}
    		// [x + 1, R]
    		if (x + 1 <= R) {
    			st.insert({x + 1, R, resR});
    			ans.insert(resR);
    		}
    	}
    }
    cout << "\n";
}
int main() {
	int T;
	cin >> T;
	while (T--) {
		solve();
	}
    return 0;
}

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

详细

Test #1:

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

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: 1731ms
memory: 27896kb

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