QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#361254#7108. CouleurYeongTreeAC ✓2104ms134072kbC++173.5kb2024-03-23 03:14:312024-03-23 03:14:31

Judging History

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

  • [2024-03-23 03:14:31]
  • 评测
  • 测评结果:AC
  • 用时:2104ms
  • 内存:134072kb
  • [2024-03-23 03:14:31]
  • 提交

answer

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <tuple>
#include <queue>
#include <numeric>
#include <set>
#include <map>
#include <random>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define piii pair<int, pii>
#define plll pair<long long, pll>
#define tiii array<int, 3>
#define tiiii array<int, 4>
#define ff first
#define ss second
#define ee ss.ff
#define rr ss.ss
typedef long long ll;
const int INF = (int)1e9 + 7;

using namespace std;

int A[101010];

struct FEN
{
	int n;
	int fen[101010];

	void init(int _n)
	{
		n = _n;
		fill(fen, fen + n + 1, 0);
	}
	void upd(int pos, int x) { for(++pos; pos <= n; pos += pos & -pos) fen[pos] += x; }
	int qry(int pos) { int ret = 0; for(; pos; pos &= pos - 1) ret += fen[pos]; return ret; }
}fen;

ll inv(int l, int r)
{
	ll ret = 0;
	for(int i = r - 1; i >= l; --i)
	{
		ret += fen.qry(A[i]);
		fen.upd(A[i], 1);
	}
	for(int i = l; i < r; ++i) fen.upd(A[i], -1);
	return ret;
}

struct Node
{
	int x;
	int l, r;
	Node(void) : x(0), l(0), r(0) {}
}seg[10101010];
int cnt;

void mrge(int ind)
{
	seg[ind].x = seg[seg[ind].l].x + seg[seg[ind].r].x;
}

int init(int s, int e)
{
	int ret = cnt++;
	seg[ret] = Node();
	if(s + 1 == e) return ret;
	
	int mid = s + e >> 1;
	seg[ret].l = init(s, mid);
	seg[ret].r = init(mid, e);
	return ret;
}

int upd(int ind, int s, int e, int pos, int x)
{
	int ret = cnt++;
	seg[ret] = seg[ind];
	if(s + 1 == e)
	{
		seg[ret].x += x;
		return ret;
	}

	int mid = s + e >> 1;
	if(pos < mid) seg[ret].l = upd(seg[ret].l, s, mid, pos, x);
	else seg[ret].r = upd(seg[ret].r, mid, e, pos, x);
	mrge(ret);
	return ret;
}

int qry(int ind, int s, int e, int l, int r)
{
	if(e <= l || r <= s) return 0;
	if(l <= s && e <= r) return seg[ind].x;

	int mid = s + e >> 1;
	return qry(seg[ind].l, s, mid, l, r) + qry(seg[ind].r, mid, e, l, r);
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	int T; cin >> T;
	while(T--)
	{
		int n; cin >> n;
		for(int i = 0; i < n; ++i) cin >> A[i], --A[i];
		fen.init(n);
		cnt = 1;

		int root[n + 1];
		root[0] = init(0, n);
		for(int i = 0; i < n; ++i) root[i + 1] = upd(root[i], 0, n, A[i], 1);

		map<pii, ll> M;
		multiset<ll> S;

		ll pr = inv(0, n);
		S.insert(pr);
		M[{0, n}] = pr;
		cout << pr << (n == 1 ? '\n' : ' ');

		for(int i = 0; i < n; ++i)
		{
			ll p; cin >> p; p ^= pr; --p;
			if(i == n - 1) break;
			auto it = prev(M.lower_bound({p, INF}));
			int l = it->ff.ff;
			int r = it->ff.ss;
			ll x = it->ss;
			// cout << endl << "!!" << l << ' ' << r << ' ' << x << endl;
			M.erase(it);
			S.erase(S.find(x));
			x -= qry(root[p], 0, n, A[p] + 1, n) - qry(root[l], 0, n, A[p] + 1, n);
			x -= qry(root[r], 0, n, 0, A[p]) - qry(root[p + 1], 0, n, 0, A[p]);

			if(p - l < r - p)
			{
				ll y = inv(l, p);
				x -= y;
				M[{l, p}] = y;
				S.insert(y);
				for(int i = l; i < p; ++i) x -= qry(root[r], 0, n, 0, A[i]) - qry(root[p + 1], 0, n, 0, A[i]);
				M[{p + 1, r}] = x;
				S.insert(x);
			}
			else
			{
				ll y = inv(p + 1, r);
				x -= y;
				M[{p + 1, r}] = y;
				S.insert(y);
				for(int i = p + 1; i < r; ++i) x -= qry(root[p], 0, n, A[i] + 1, n) - qry(root[l], 0, n, A[i] + 1, n);
				M[{l, p}] = x;
				S.insert(x);
			}

			pr = *prev(S.end());
			cout << pr << (i == n - 2 ? '\n' : ' ');
			// cout << endl << "??"; for(auto i : S) cout << i << ' '; cout << endl;
		}
	}
}

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

详细

Test #1:

score: 100
Accepted
time: 8ms
memory: 122020kb

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: 2104ms
memory: 134072kb

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