作诗(si)[分块]

题目描述

神犇SJY虐完HEOI之后给傻×LYD出了一题:

SHY是T国的公主,平时的一大爱好是作诗。

由于时间紧迫,SHY作完诗之后还要虐OI,于是SHY找来一篇长度为N的文章,阅读M次,每次只阅读其中连续的一段[l,r],从这一段中选出一些汉字构成诗。因为SHY喜欢对偶,所以SHY规定最后选出的每个汉字都必须在[l,r]里出现了正偶数次。而且SHY认为选出的汉字的种类数(两个一样的汉字称为同一种)越多越好(为了拿到更多的素材!)。于是SHY请LYD安排选法。

LYD这种傻×当然不会了,于是向你请教……

问题简述:N个数,M组询问,每次问[l,r]中有多少个数出现正偶数次。

输入输出格式

输入格式:

输入第一行三个整数n、c以及m。表示文章字数、汉字的种类数、要选择M次。

第二行有n个整数,每个数Ai在[1, c]间,代表一个编码为Ai的汉字。

接下来m行每行两个整数l和r,设上一个询问的答案为ans(第一个询问时ans=0),令L=(l+ans)mod n+1, R=(r+ans)mod n+1,若L>R,交换L和R,则本次询问为[L,R]。

输出格式:

输出共m行,每行一个整数,第i个数表示SHY第i次能选出的汉字的最多种类数。

输入输出样例

输入样例#1:
5 3 5
1 2 2 3 1
0 4
1 2
2 2
2 3
3 5
输出样例#1:
2
0
0
0
1

说明

对于100%的数据,1<=n,c,m<=10^5

题解

  1.我们考虑一下分块的话要每一块都保存是正偶数的数字的个数,用一个ans[i][j]保存第i块到第j块内符合条件的数字的个数,o(1)的查询,前缀和的思想

  2.在最左端的最右端的用一个统计数组暴力即可

  3.但是要记住最左端和最右端的数字要与整个[l,r]区间相关联,所以用一个sum[i][j]保存第[i]块第[j]种颜色的数量

  4.luogu上记得开02....

// luogu-judger-enable-o2
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=100001;
int ch[N],bl[N],cnt[N],ans[330][330],sum[330][N];
int l[N],r[N],n,m,c,last,tmp,res;
int read()
{
    int x=0,w=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*w;
}

/*int prep(int x)
{
    return (x+last)%n+1;
}*/

void build()
{
    for(int i=1;i<=tmp;i++)
    l[i]=(i-1)*tmp+1,r[i]=tmp*i;
    r[tmp]=n;
    for(int i=1;i<=n;i++)
    {
        bl[i]=(i-1)/(tmp)+1;
        sum[bl[i]][ch[i]]++;
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=tmp;j++)
        {
            sum[j][i]+=sum[j-1][i];
        }
    for(int i=1;i<=tmp;i++)
    {
        int now=0;
        for(int j=l[i];j<=n;j++)
        {
            ++cnt[ch[j]];
            if (!(cnt[ch[j]] & 1)) ++now;
            else if (cnt[ch[j]] > 2) --now;
            ans[i][bl[j]]=now;
        }
        for(int j=l[i];j<=n;j++)
            --cnt[ch[j]];
    }
}

int query(int x,int y)
{    
    x=(x+res)%n+1;y=(y+res)%n+1;
    if(x>y)swap(x,y);
    res=0;
    if(bl[y]<=bl[x]+1)
    {
        for(int i=x;i<=y;i++)
        {
            ++cnt[ch[i]];
            if(!(cnt[ch[i]]&1))res++;
            else if(cnt[ch[i]]>2)res--;
        }
        for(int i=x;i<=y;i++)--cnt[ch[i]];
        return last=res;
    }
    res=ans[bl[x]+1][bl[y]-1];
    for(int i=x;i<=r[bl[x]];i++)
    {
        ++cnt[ch[i]];
        if(!((cnt[ch[i]]+sum[bl[y]-1][ch[i]]-sum[bl[x]][ch[i]])&1))++res;
        else if(cnt[ch[i]]+sum[bl[y]-1][ch[i]]-sum[bl[x]][ch[i]]>2)--res;
    }
    for (int i=l[bl[y]];i<=y;++i)
    {
        ++cnt[ch[i]];
         if(!((cnt[ch[i]]+sum[bl[y]-1][ch[i]]-sum[bl[x]][ch[i]])&1))++res;
        else if(cnt[ch[i]]+sum[bl[y]-1][ch[i]]-sum[bl[x]][ch[i]]>2)--res;
    }
    for(int i=x;i<=r[bl[x]];i++)--cnt[ch[i]];
    for(int i=l[bl[y]];i<=y;i++)--cnt[ch[i]];
    return last=res;
}

int main()
{
    n=read();c=read();m=read();tmp=sqrt(n);
    tmp++;
    for(int i=1;i<=n;i++)ch[i]=read();
    build();
    while(m--)
    {
        int x=read(),y=read();
        printf("%d
",query(x,y));
    }
    return 0;
}
原文地址:https://www.cnblogs.com/hhh1109/p/8716913.html