这道题我干了一个下午和晚上,就是不知道哪里出错了,求助dalao
查看原帖
这道题我干了一个下午和晚上,就是不知道哪里出错了,求助dalao
120017
JeffZhao楼主2021/7/3 21:58
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define int long long

using namespace std;

const int N = 2e5 + 10;

int n, m, a[N], before[N], cnt[N], c[N];

inline int du(void) {
	int x = 0, f = 1;
	char ch = getchar();
	while (ch > '9' || ch < '0') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
inline void write(int x) {
	if (x < 0) { x = -x; putchar('-'); }
	if (x > 9)write(x / 10);
	putchar(x % 10 ^ 48);
	return;
}

inline int lowbit(int x) { return x & (-x); }

inline void update(int x, int v) {
	for (int i = x; i <= N; i += lowbit(i))
		c[i] += v;
	return;
}
inline int Sum(int x) {
	int s = 0;
	for (int i = x; i; i -= lowbit(i)) 
		s += c[i];
	return s;
}

int tot = 0;

signed main(void) {
	n = du(), m = du();

	for (int i = 1; i <= n; ++i)
		a[i] = du();

	for (int i = 1; i <= n; ++i) {
		int t = Sum(a[i]);
		before[i] = i - t;
		tot += before[i];
		++ cnt[before[i]];
		update(a[i], 1);
	}

	memset(c, 0, sizeof(c));
	
	update(1, tot);
	tot = 0;

	for (int i = 0; i < n; ++i) {
		tot += cnt[i];
		update(i + 2, -(n - tot));
	}

	int x, k, t;

	for (int i = 1; i <= m; ++i) {
		t = du();
		if (t == 2) {
			k = du();
			k = min(k, n - 1);
			write(Sum(k + 1));
			putchar('\n');
		}
		else {
			x = du();
			if (a[x] < a[x + 1]) {
				swap(a[x], a[x + 1]);
				swap(before[x], before[x + 1]);
				update(1, 1);
				++before[x + 1];
				update(before[x + 1] + 1, -1);
			}
			else {
				swap(a[x], a[x + 1]);
				swap(before[x], before[x + 1]);
				update(1, -1);
				--before[x];
				update(before[x] + 2, 1);
			}
		}
	}

	return 0;
}
2021/7/3 21:58
加载中...