P4135 作诗
P4135 作诗
大致题意
给定 \(n\) 个不大于 \(c\) 的正整数\(a_1...a_n\) 和 \(m\) 组询问,每次问区间\([l,r]\)中有多少个数出现正偶数次。
分析
分块入门题
设\(sum[i][j]\)为前\(i\)个块中第\(j\)个元素出现的个数
\(ans[i][j]\)为第\(i\)个块到第\(j\)个块中出现正偶数次的数的个数
先来考虑如何预处理出\(ans[i][j]\)
考虑每个新进来的数对\(a[i][j]\)的贡献
若新进来的数使该数在这几个区间中的出现次数变成了奇数,则答案减一,反之加一
对于查询
若\(l,r\)在同一个块内,直接暴力查询即可
若\(l,r\)不在同一个块内:
对于完整块,其对答案的贡献即为\(ans[l+1][r-1]\)
对于不完整块,暴力统计其块内每个数出现的个数,再加上\(sum[r-1][a[i]]-sum[l][a[i]]\)
若总出现次数为奇数,则答案加一,反之减一
复杂度\(O(n \sqrt n)\)
code
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
inline int read(){
int X=0; bool flag=1; char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
if(flag) return X;
return ~(X-1);
}
const int MAXN = 100010;
#define now (sum[y-1][a[i]]-sum[x][a[i]]+b[a[i]])
int a[MAXN],belong[MAXN],t[MAXN],n,m,st[MAXN],ed[MAXN],tag[MAXN],num,sum[400][MAXN],ans[400][400],b[MAXN];
int query(int l,int r){
int res = 0;
int x = belong[l];
int y = belong[r];
if(x==y){
for(int i=l;i<=r;i++){
b[a[i]]++;
}
for(int i=l;i<=r;i++){
if(!b[a[i]]) continue;
else if(b[a[i]]%2==0) res++;
b[a[i]] = 0;
}
return res;
}
res = ans[x+1][y-1];
for(int i=l;i<=ed[x];i++){
b[a[i]]++;
if(now>1&&(now&1)) res--;
if(now%2==0) res++;
}
for(int i=st[y];i<=r;i++){
b[a[i]]++;
if(now>1&&(now&1)) res--;
if(now%2==0) res++;
}
memset(b,0,sizeof(b));
return res;
}
int main(){
n = read(),num = read(),m = read();
for(int i=1;i<=n;i++) a[i] = read();
int size = sqrt(n);
for(int i=1;i<=size;i++) st[i] = (i-1)*size+1,ed[i] = i*size;
ed[size] = n;
for(int i=1;i<=size;i++){
for(int j=st[i];j<=ed[i];j++){
belong[j] = i;
sum[i][a[j]]++;
}
}
for(int i=1;i<=size;i++){
for(int j=1;j<=num;j++){
sum[i][j] += sum[i-1][j];
}
}
for(int i=1;i<=size;i++){
for(int j=st[i];j<=ed[i];j++){
b[a[j]]++;
if(b[a[j]]==1) continue;
else if(b[a[j]]%2==0) ans[i][i]++;
else ans[i][i]--;
}
for(int j=i+1;j<=size;j++){
ans[i][j] = ans[i][j-1];
for(int k=st[j];k<=ed[j];k++){
b[a[k]]++;
if(b[a[k]]==1) continue;
else if(b[a[k]]%2==0) ans[i][j]++;
else ans[i][j]--;
}
}
memset(b,0,sizeof(b));
}
int A = 0;
for(int i=1;i<=m;i++){
int l,r;
l = read(),r=read();
l = (l+A)%n+1,r = (r+A)%n+1;
if(l>r) swap(l,r);
A = query(l,r);
printf("%d\n",A);
}
}