带修莫队 6WA+4TLE 求调
查看原帖
带修莫队 6WA+4TLE 求调
357163
shyr楼主2022/5/9 13:31
#include<bits/stdc++.h>
using namespace std;
const int N = 133335;
int n, m, cnt1 = 1, cnt2 = 1, cnt[1000005], Ans[N], ans, a[N], b[N];
char opt[2];
struct Q{
	int l, r, t, pos, rpos, id;
}q[N];
struct xw{
	int i, a, b;
}g[N];
bool cmp(Q a, Q b){
	if(a.pos == b.pos){
		return a.r < b.r;
	}
	return a.l < b.l;
}
void add(int x){
	if(++cnt[a[x]] == 1) ans++;	
}
void del(int x){
	if(--cnt[a[x]] == 0) ans--;
}
int main(){
	scanf("%d%d", &n, &m);
	int s = pow(n, (double)2 / (double)3);
	for(int i = 1; i <= n; ++i) scanf("%d", &a[i]), b[i] = a[i];
	for(int i = 1; i <= m; ++i){
		scanf("%s", opt + 1);
		if(opt[1] == 'Q'){
			scanf("%d%d", &q[cnt1].l, &q[cnt1].r);
			q[cnt1].t = cnt2, q[cnt1].id = cnt1;
		    q[cnt1].pos = (q[cnt1].l - 1) / s + 1; 
			q[cnt1].rpos = (q[cnt1].r - 1) / s + 1;
			++cnt1;
		}
		else {
			scanf("%d%d", &g[cnt2].i, &g[cnt2].b);
			g[cnt2].a = b[g[cnt2].i];
			b[g[cnt2].i] = g[cnt2].b;
			++cnt2;
		}
	}
	sort(q + 1, q + cnt1, cmp);
	int L = 1, R = 0, T = 0;
	for(int i = 1; i < cnt1; ++i){
	//	printf("%d %d %d\n", q[i].l, q[i].r, q[i].t);
		while(T < q[i].t){
		//	printf("%d %d\n", g[T].i, q[i].t);
			if(q[i].l <= g[T].i && g[T].i <= q[i].r){
				add(g[T].b), del(g[T].a);
				a[g[T].i] = g[T].b;
			}
			++T;
		}
		while(T > q[i].t){
			--T;
			if(q[i].l <= g[T].i && g[T].i <= q[i].r){
				add(g[T].a), del(g[T].b);
				a[g[T].i] = g[T].a;
			}
		}
		while(L < q[i].l) del(L++);
		while(L > q[i].l) add(--L);
		while(R < q[i].r) add(++R);
		while(R > q[i].r) del(R--);
		Ans[q[i].id] = ans;
	}
	for(int i = 1; i < cnt1; ++i) printf("%d\n", Ans[i]);
}

非常感谢!

2022/5/9 13:31
加载中...