BZOJ 2821: 作诗(Poetize) [分块]

题意:

N个数,Q组询问,每次问[l,r]中有多少个数出现正偶数次。

和上题一样呀呀呀
稍微改一改交上就行了
写错好多地方...要认真啊...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=1e5+5,M=320;
typedef unsigned long long ll;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
int n,k,Q,x,y,a[N];
int pos[N],m,block;
struct _blo{int l,r;} b[M];
inline void ini(){
    block=sqrt(n);
    m=(n-1)/block+1;
    for(int i=1;i<=n;i++) pos[i]=(i-1)/block+1;
    for(int i=1;i<=m;i++) b[i].l=(i-1)*block+1,b[i].r=i*block;
    b[m].r=n;
}

int f[M][M];
int t[N],s[M][N];
struct Block{
    void set(int x){
        memset(t,0,sizeof(t));
        int now=0;
        for(int i=x;i<=m;i++){
            for(int j=b[i].l ; j<=b[i].r ; j++) 
                now+= t[a[j]]++ ? ( t[a[j]]&1 ? -1 : 1) : 0;
            f[x][i]=now;
        }
        for(int i=1;i<=k;i++) s[x][i]=s[x-1][i];
        for(int i=b[x].l;i<=b[x].r;i++) s[x][a[i]]++;
    }

    int que(int l,int r){
        int L=pos[l],R=pos[r];
        int re=f[L+1][R-1];
        if(L==R){
            for(int i=l;i<=r;i++) t[a[i]]=0;
            for(int i=l;i<=r;i++) re+= t[a[i]]++ ? ( t[a[i]]&1 ? -1 : 1) : 0;
        }else{
            for(int i=l ; i<=b[L].r ; i++) t[a[i]]=s[R-1][ a[i] ] - s[L][ a[i] ];
            for(int i=b[R].l ; i<=r ; i++) t[a[i]]=s[R-1][ a[i] ] - s[L][ a[i] ];
            for(int i=l ; i<=b[L].r ; i++) re+= t[a[i]]++ ? ( t[a[i]]&1 ? -1 : 1) : 0;
            for(int i=b[R].l ; i<=r ; i++) re+= t[a[i]]++ ? ( t[a[i]]&1 ? -1 : 1) : 0;
        }
        return re;
    }
}B;
int main(){
    freopen("in","r",stdin);
    n=read();k=read();Q=read();
    for(int i=1;i<=n;i++) a[i]=read();

    ini();
    for(int i=1;i<=m;i++) B.set(i);
    int last=0;
    while(Q--){
        int l=(read()+last)%n+1,r=(read()+last)%n+1;
        if(l>r) swap(l,r);
        last=B.que(l,r);
        printf("%d
",last);
    }
}
 
 
原文地址:https://www.cnblogs.com/candy99/p/6555214.html