###### bzoj2821: 作诗(Poetize)

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
const int maxn=100010,maxb=320;
using namespace std;
int n,c,m,sum[maxb][maxn],f[maxb][maxb],a[maxn],sz,cnt,bel[maxn],l[maxn],r[maxn],tsum[maxn],ans,tmp[maxn];

void work(){
int x,y,L,R;scanf("%d%d",&x,&y);
x=(x+ans)%n+1,y=(y+ans)%n+1,ans=0;
if (x>y) swap(x,y);
//printf("%d %d\n",x,y);
L=bel[x],R=bel[y];
if (L==R){
for (int i=x;i<=y;i++) tsum[a[i]]=0;
for (int i=x;i<=y;i++)
if ((++tsum[a[i]])!=1)
ans+=(tsum[a[i]]&1)?-1:1;
}
else{
ans=f[L+1][R-1];
//printf("ans%d %d %d\n",L,R,ans);
for (int i=x;i<=r[L];i++) tsum[a[i]]=sum[R-1][a[i]]-sum[L][a[i]];
for (int i=l[R];i<=y;i++) tsum[a[i]]=sum[R-1][a[i]]-sum[L][a[i]];
//for (int i=1;i<=c;i++) printf("tsum%d\n",tsum[i]);
for (int i=x;i<=r[L];i++)
if (++tsum[a[i]]!=1)
ans+=(tsum[a[i]]&1)?-1:1;
for (int i=l[R];i<=y;i++)
if (++tsum[a[i]]!=1)
ans+=(tsum[a[i]]&1)?-1:1;
}
printf("%d\n",ans);
}

int main(){
scanf("%d%d%d",&n,&c,&m),sz=sqrt(n),cnt=n/sz+(n%sz!=0);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
for (int i=1;i<=n;i++) bel[i]=(i-1)/sz+1;
for (int i=1;i<=n;i++){r[bel[i]]=i;if (!l[bel[i]]) l[bel[i]]=i;}
for (int i=1;i<=cnt;i++){
for (int j=1;j<=c;j++) sum[i][j]=sum[i-1][j];
for (int j=l[i];j<=r[i];j++) sum[i][a[j]]++;
}
for (int i=1;i<=cnt;i++){
for (int j=1;j<=c;j++) tsum[j]=0;int tmp=0;
for (int j=i;j<=cnt;j++){
for (int k=l[j];k<=r[j];k++)
if (tsum[a[k]]++) tmp+=(tsum[a[k]]&1)?-1:1;
f[i][j]=tmp;
}
}
//for (int i=1;i<=cnt;i++) printf("%d %d\n",l[i],r[i]);
/*for (int i=1;i<=cnt;i++,puts(""))
for (int j=1;j<=cnt;j++)
printf("%d ",f[i][j]);*/
for (int i=1;i<=m;i++) work();
return 0;
}

#### BZOJ 2821 作诗(Poetize) 分块

2014-10-22 10:14:06

#### [bzoj2821]作诗

2016-03-07 14:03:32

#### bzoj2821: 作诗(Poetize) 分块

2018-03-06 19:50:48

#### bzoj 2821

2015-08-07 18:38:14

#### BZOJ 2821 分块+二分

2017-02-15 16:11:01

#### bzoj2821

2016-11-27 12:44:40

#### bzoj 2821: 作诗(Poetize) （分块）

2016-12-10 17:44:36

#### 【bzoj 2821】 作诗 分块

2016-03-17 22:04:52

#### 【bzoj2821】作诗(Poetize)

2016-05-23 11:33:43

#### bzoj2821[作诗(Poetize)]

2017-07-19 21:19:06

## 不良信息举报

bzoj2821: 作诗(Poetize)