QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#563896#6434. Paimon SortingAmiyaCastAC ✓224ms19576kbC++143.1kb2024-09-14 16:53:252024-09-14 16:53:25

Judging History

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

  • [2024-09-14 16:53:25]
  • 评测
  • 测评结果:AC
  • 用时:224ms
  • 内存:19576kb
  • [2024-09-14 16:53:25]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define pii make_pair
#define pb push_back
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,b,a) for(int i=b;i>=a;--i)
const ll inf = 1145141919810;
using namespace std;
inline ll read() {
	ll x=0,f=1;
	char c=getchar();
	while (c<'0' || c>'9') {
		if (c=='-')  f=-1;
		c=getchar();
	}
	while (c>='0' && c<='9') {
		x=x*10+c-'0';
		c=getchar();
	}
	return x*f;
}
inline void print(ll x) {
	if(x < 0) putchar('-'), x = -x;
	if(x > 9) print(x / 10);
	putchar(x % 10 + '0');
	return ;
}
inline void pprint(ll x) {
	print(x);
	puts("");
}
const int N = 2e5 + 7;
ll c[N], a[N], pos[N];
int n;
void add(int x, ll y) {
	for(; x <= n; x += x & -x) c[x] += y;
}
ll ask(int x) {
	ll res = 0;
	for(; x; x -= x & -x) res += c[x];
	return res;
}
struct Node {
	int l, r;
	ll add;
	ll sum;
} t[N << 2];

void up(int p) {
	t[p].sum = t[p << 1].sum + t[p << 1 | 1].sum;
}
void build(int p, int l, int r) {
	t[p] = Node {l, r};
	if(l == r) return ;
	const int mid = l + r >> 1;
	build(p << 1, l, mid), build(p << 1 | 1, mid + 1, r);
}

void down(int p) {
	if(t[p].add) {
		t[p << 1].sum += (t[p << 1].r - t[p << 1].l + 1) * t[p].add, t[p << 1 | 1].sum += (t[p << 1 | 1].r - t[p << 1 | 1].l + 1) * t[p].add;
		t[p << 1].add += t[p].add, t[p << 1 | 1].add += t[p].add;
		t[p].add = 0;
	}
}
void add(int p, int x, int y, ll k) {
	int l = t[p].l, r = t[p].r;

	if(x <= l && r <= y) {
		t[p].sum += (r - l + 1) * k, t[p].add += k;
		return ;
	}
	down(p);
	const int mid = l + r >> 1;
	if(x <= mid) add(p << 1, x, y, k);
	if(y >= mid + 1) add(p << 1 | 1, x, y, k);
	up(p);
}
ll aask(int p, int x) {
	int l = t[p].l, r = t[p].r;
	if(l == r) {
		return t[p].sum;
	}
	down(p);
//	up(p);
	const int mid = l + r >> 1;
	if(x <= mid) return aask(p << 1, x);
	else return aask(p << 1 | 1, x);
}

void slv() {
	build(1, 1, n);
	vector<ll> ans;
	ans.pb(0);

	ll maxn = a[1];
	pos[a[1]] = -1;
	if(ask(a[1]) - ask(a[1] - 1) == 0) add(a[1], 1);

	for(int i = 2; i <= n; ++i) {


		if(a[i] > maxn) { //在第一轮做出改变
			add(1, 1, 1, 1);//第一个位置首先多进行一次交换
			add(1, i, i, 1);//最后一个位置会进行一次交换
			//下面进行修正
			if(pos[maxn] > 0) { //第二次出现的位置都会多进行一次add
				add(1, pos[maxn], i - 1, 1);

			}
			maxn = a[i];

		} else { //一直轮不到他,那无所谓就仅仅是加一下自己的贡献就行
			ll tmp = ask(n) - ask(a[i]);//比a[i]大的有几个?
			add(1, i, i, tmp);//在这个地方正常进行交换
		}

		ans.pb(t[1].sum);
		if(ask(a[i]) - ask(a[i] - 1) == 0) add(a[i], 1);
		if(pos[a[i]] == 0) pos[a[i]] = -1;
		else if(pos[a[i]] == -1) pos[a[i]] = i;

	}
	rep(i, 0, (int)ans.size() - 1) {
		printf("%lld%c", ans[i], i == (int)ans.size() - 1 ? '\n' : ' ');
	}
}
int main() {
	int T = read();
	while(T--) {
		n = read();
		rep(i, 1, n) a[i] = read();
		slv();
		rep(i, 0, n + 1) c[i] = pos[i] = 0;
		rep(i, 0, n * 4 + 1) t[i] = Node {0, 0, 0, 0};

	}
	return 0;
}



Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
5
2 3 2 1 5
3
1 2 3
1
1

output:

0 2 3 5 7
0 2 4
0

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 224ms
memory: 19576kb

input:

6107
19
10 13 8 8 11 18 12 9 15 19 6 13 11 11 17 9 14 2 18
12
1 8 10 2 10 2 6 1 5 9 5 7
16
14 4 2 15 12 14 10 3 2 9 15 4 12 9 5 15
10
3 2 5 6 7 8 6 1 6 4
18
6 5 12 12 11 2 10 10 5 10 13 15 13 10 17 7 11 2
1
1
2
1 1
3
2 1 2
17
11 15 3 10 7 15 15 10 5 17 3 3 14 13 11 11 2
3
2 2 3
7
6 1 7 5 3 5 1
7
2 1...

output:

0 2 4 6 7 9 11 16 17 19 28 31 36 41 43 51 55 67 68
0 2 4 6 6 8 10 14 17 18 22 25
0 1 3 5 7 8 11 16 22 26 26 31 33 37 42 42
0 1 3 5 7 9 11 17 19 23
0 1 3 3 4 8 10 12 16 18 27 29 30 34 36 42 46 55
0
0 0
0 1 1
0 2 4 6 9 9 9 11 15 21 27 33 35 38 42 46 55
0 0 3
0 1 3 5 8 10 14
0 1 3 4 6 9 9
0 1 1 3 7 11 ...

result:

ok 6107 lines