萌新求助,待修莫队WA了三个点
查看原帖
萌新求助,待修莫队WA了三个点
428350
Lower_HeSn楼主2022/8/22 21:48

8 9 10WA了

代码:

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 150000;
int n, m, s, a[MAXN], num, cnt, tot, b[MAXN], res, ans[MAXN];
map<int, int>id;
struct node {
	int l, r, id, time;
}q[MAXN];
struct Node {
	int x, w, z;
}c[MAXN];
bool cmp(node x, node y) {
	if(x.l / s != y.l / s) {
		return x.l / s < y.l / s;
	}
	if(x.r / s != y.r / s) {
		return x.r / s < y.r / s;
	}
	return x.time < y.time;
}
void add(int x) {
	b[a[x]] ++;
	if(b[a[x]] == 1) {
		res ++;
	}
}
void del(int x) {
	b[a[x]] --;
	if(!b[a[x]]) {
		res --;
	}
}
void update(int x, int l, int r) {
	if(c[x].x >= l && c[x].x <= r) {
		if(-- b[a[c[x].x]] == 0) {
			res --;
		}
		if(++ b[c[x].w] == 1) {
			res ++;
		}
	}
	swap(a[c[x].x], c[x].w);
}
signed main() {
	cin >> n >> m;
	s = pow(n, 2.0 / 3) + 1;
	for(int i = 1; i <= n; i ++) {
		cin >> a[i];
		if(!id[a[i]]) {
			id[a[i]] = ++ num;
		}
		a[i] = id[a[i]];
	}
	for(int i = 1; i <= m; i ++) {
		char op;
		int x, y;
		cin >> op >> x >> y;
		if(op == 'Q') {
			++ cnt;
			q[cnt].id = cnt;
			q[cnt].l = x;
			q[cnt].r = y;
			q[cnt].time = tot;
		}
		else {
			if(!id[y]) {
				id[y] = ++ num;
			}
			y = id[y];
			++ tot;
			c[tot].x = x;
			c[tot].w = y;
		}
	}
	sort(q + 1, q + cnt + 1, cmp);
	int l = 1, r = 0, t = 0;
	for(int i = 1; i <= cnt; i ++) {
//		cout << 1;
		while(l > q[i].l) {
			add(-- l);
		}
		while(r < q[i].r) {
			add(++ r);
		}
		while(l < q[i].l) {
			del(l ++);
		}
		while(r > q[i].r) {
			del(r --);
		}
		while(t < q[i].time) {
			update(++ t, l, r);
		}
		while(t > q[i].time) {
			update(t --, l, r);
		}
		ans[q[i].id] = res;
	}
	for(int i = 1; i <= cnt; i ++) {
		cout << ans[i] << endl;
	}
	return 0;
}

球球大佬们帮忙看看吧,调了好久了,评论区里面能犯的错误都犯了一遍,就这个不知道咋回事。

2022/8/22 21:48
加载中...