luogu4135 作诗

这里

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int n, c, m, a[100005], bel[100005], blc, f[325][325], sum[325][100005], cnt[100005];
int uu, vv, ans;
int query(int uu, int vv){
	int re=0;
	if(bel[uu]==bel[vv]){
		for(int i=uu; i<=vv; i++)	cnt[a[i]] = 0;
		for(int i=uu; i<=vv; i++){
			cnt[a[i]]++;
			if(!(cnt[a[i]]&1))	re++;
			else if(cnt[a[i]]>2)	re--;
		}
	}
	else{
		re = f[bel[uu]+1][bel[vv]-1];
		for(int i=uu; i<=bel[uu]*blc; i++)
			cnt[a[i]] = sum[bel[vv]-1][a[i]] - sum[bel[uu]][a[i]];
		for(int i=(bel[vv]-1)*blc+1; i<=vv; i++)
			cnt[a[i]] = sum[bel[vv]-1][a[i]] - sum[bel[uu]][a[i]];
		for(int i=uu; i<=bel[uu]*blc; i++){
			cnt[a[i]]++;
			if(!(cnt[a[i]]&1))	re++;
			else if(cnt[a[i]]>2)	re--;
		}
		for(int i=(bel[vv]-1)*blc+1; i<=vv; i++){
			cnt[a[i]]++;
			if(!(cnt[a[i]]&1))	re++;
			else if(cnt[a[i]]>2)	re--;
		}
	}
	return re;
}
int main(){
	cin>>n>>c>>m;
	blc = sqrt(n);
	for(int i=1; i<=n; i++){
		scanf("%d", &a[i]);
		bel[i] = (i - 1) / blc + 1;
	}
	for(int i=1; i<=n; i++){
		if(i==(bel[i]-1)*blc+1){
			for(int j=1; j<=c; j++)
				sum[bel[i]][j] = sum[bel[i]-1][j];
		}
		sum[bel[i]][a[i]]++;
	}
	for(int i=1; i<=bel[n]; i++){
		memset(cnt, 0, sizeof(cnt));
		int now=0;
		for(int j=(i-1)*blc+1; j<=n; j++){
			cnt[a[j]]++;
			if(!(cnt[a[j]]&1))	now++;
			else if(cnt[a[j]]>2)	now--;
			f[i][bel[j]] = now;
		}
	}
	while(m--){
		scanf("%d %d", &uu, &vv);
		uu = (uu + ans) % n + 1;
		vv = (vv + ans) % n + 1;
		if(uu>vv)	swap(uu, vv);
		ans = query(uu, vv);
		printf("%d\n", ans);
	}
	return 0;
}
posted @ 2018-03-04 09:33  poorpool  阅读(100)  评论(0编辑  收藏  举报