64pts求调
查看原帖
64pts求调
1384818
HeYuChenC楼主2024/12/8 22:21
#include<bits/stdc++.h>
using namespace std;
int n, b, m;
int cnt[1000005], sum = 0, ans[133340], a[133340], f = 0;
struct que {
	int l = 0, r = 0, t = 0, id = -1;
	bool operator < (que a) {
		int lb = l / b, la = a.l / b;
		return lb != la ? lb < la : r != a.r ? r < a.r : t < a.t;
	}
} q[133340];
struct qu {
	int pos = -1, v = -1;
} p[133340];
void add(int x) {
	if (++cnt[x] == 1) ++sum;
}
void del(int x) {
	if (cnt[x]-- == 1) --sum;
}
int main() {
	memset(cnt, 0, sizeof(cnt));
	cin >> n >> m;
	for (int i = 1; i <= n; i++) cin >> a[i];
	b = pow (n, 0.6666);
	char c;
	for (int i = 1; i <= m; i++) {
		cin >> c;
		if (c == 'Q') {
			f++;
			cin >> q[f].l >> q[f].r;
			q[f].id = f;
			q[f].t = i;
		} else cin >> p[i].pos >> p[i].v;
	}
	sort(q + 1, q + f + 1);
	for (int i = 1, j = 0, k = 1, l = 0; k <= f; k++) {
		while (j < q[k].r) add(a[++j]);
		while (i > q[k].l) add(a[--i]);
		while (j > q[k].r) del(a[j--]);
		while (i < q[k].l) del(a[i++]);
		while (l < q[k].t) {
			++l;
			if (i <= p[l].pos && p[l].pos <= j) {
				del(a[p[l].pos]);
				add(p[l].v);
			}
			swap(a[p[l].pos], p[l].v);
		}
		while (l > q[k].t) {
			if (i <= p[l].pos && p[l].pos <= j) {
				del(a[p[l].pos]);
				add(p[l].v);
			}
			swap(a[p[l].pos], p[l].v);
			--l;
		}
		ans[q[k].id] = sum;
	}
	for (int i = 1; i <= f; i++) cout << ans[i] << endl;
	return 0;
}

TLE #7-#12 求助

2024/12/8 22:21
加载中...