「雅礼集训 2018 Day2」颜色

· · 个人记录

题目

\text{Description:}

m个询问,静态+强制在线,k个区间颜色并集的card。

\text{Solution:}

好题!

首先,分块套bitset,暴力去对于每个块or一下。

这样就可以做到 O( \dfrac{n \sqrt{n}} w)。

但是这样打不过这道题,考虑整块的时候倍增就好了。

卡卡常就好了,块长调到1000,把L,R,id都去掉。

\text{Code:}

#include <bits/stdc++.h>

#define N (int)(1e5+1)
#define M 101
#define L(X) ((X-1)*bl+1)
#define R(X) (min(n,X*bl))
#define id(X) ((X-1)/bl+1)

using namespace std;

bitset<N>b[8][M],qaq;
int n,m,p,bl,a[N];

int rd() {
    int f=1,sum=0; char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
    while(isdigit(ch)) {sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
    return sum*f;
}

void qry(int l,int r) {
    if(l>r) return;
    int tmp=log2(r-l+1);
    qaq|=b[tmp][l]; qaq|=b[tmp][r-(1<<tmp)+1];
}

void query(int l,int r) {
    if(id(l)==id(r)) {  
        for(int i=l;i<=r;i++) qaq[a[i]]=1;
    } else {
        //bitset<N>qwq;
        for(int i=l;i<=R(id(l));i++) qaq[a[i]]=1;
        for(int i=L(id(r));i<=r;i++) qaq[a[i]]=1;
        qry(id(l)+1,id(r)-1);
    }
}

int main() {
//  freopen("color1.in","r",stdin);
    //freopen("a.out","w",stdout);
    n=rd(); m=rd(); p=rd(); bl=1000;
    int las=0,x,l,r;
    for(int i=1;i<=n;i++) {
    //  id[i]=(i-1)/bl+1;
        a[i]=rd();
    }
    for(int i=1;i<=id(n);i++) {
        for(int j=L(i);j<=R(i);j++) b[0][i][a[j]]=1;
    }
    for(int j=1;j<=7;j++) {
        for(int i=1;i+(1<<j)-1<=id(n);i++) {
            b[j][i]=b[j-1][i]|b[j-1][i+(1<<(j-1))];
        }
    }
    //puts(":");
    las=0; 
    for(int i=1;i<=m;i++) {
        x=rd();
        qaq.reset();    
        for(int j=1;j<=x;j++) {
            l=rd(); r=rd();
            if(i!=1&&p) {
                l^=las; r^=las;
                l=(l%n)+1; r=(r%n)+1;
                if(l>r) swap(l,r);
            }
            query(l,r);
            //cout<<l<<" "<<r<<endl;
        }
        las=qaq.count();
        printf("%d\n",las);
    }
    return 0;
}