[BZOJ2821] 作诗(Poetize)

Description

神犇SJY虐完HEOI之后给傻×LYD出了一题:SHY是T国的公主,平时的一大爱好是作诗。由于时间紧迫,SHY作完诗
之后还要虐OI,于是SHY找来一篇长度为N的文章,阅读M次,每次只阅读其中连续的一段[l,r],从这一段中选出一
些汉字构成诗。因为SHY喜欢对偶,所以SHY规定最后选出的每个汉字都必须在[l,r]里出现了正偶数次。而且SHY认
为选出的汉字的种类数(两个一样的汉字称为同一种)越多越好(为了拿到更多的素材!)。于是SHY请LYD安排选
法。LYD这种傻×当然不会了,于是向你请教……问题简述:N个数,M组询问,每次问[l,r]中有多少个数出现正偶
数次。

Input

输入第一行三个整数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]。

Output

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

Sample Input

5 3 5
1 2 2 3 1
0 4
1 2
2 2
2 3
3 5

Sample Output

2
0
0
0
1

HINT

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

Source

 

 
巨神的分块好题。
设$f[i][j]$为$i$块到$j$块的答案,$cnt[i][j]$表示从第$i$块的左端点开始,到结尾的数字$j$的出现次数。
 

 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
using namespace std;
#define reg register
inline char gc() {
    static const int bs = 1 << 22;
    static unsigned char buf[bs], *st, *ed;
    if (st == ed) ed = buf + fread(st = buf, 1, bs, stdin);
    return st == ed ? EOF : *st++;
}
#define gc getchar
inline int read() {
    int res=0;char ch=gc();bool fu=0;
    while(!isdigit(ch))fu|=(ch=='-'), ch=gc();
    while(isdigit(ch))res=(res<<3)+(res<<1)+(ch^48), ch=gc();
    return fu?-res:res;
}

int n, C, m;
int a[100005];
int belong[100005], L[505], R[505];
int f[505][505], cnt[505][100005];
int num[100005], stack[100005], top;


int main()
{
    n = read(), C = read(), m = read();
    for (reg int i = 1 ; i <= n ; i ++) a[i] = read();
    int Block = 300;
    for (reg int i = 1 ; i <= n ; i ++)
    {
        belong[i] = (i - 1) / Block + 1;
        if (!L[belong[i]]) L[belong[i]] = i, R[belong[i - 1]] = i - 1;
    }
    R[belong[n]] = n;
    for (reg int i = 1 ; i <= belong[n] ; i ++)
    {
        int tmp = 0;
        for (reg int j = L[i] ; j <= n ; j ++)
        {
            cnt[i][a[j]]++;
            if (cnt[i][a[j]] % 2 == 0) tmp++;
            if ((cnt[i][a[j]] & 1) and cnt[i][a[j]] > 1) tmp--;
            if (R[belong[j]] == j) f[i][belong[j]] = tmp;
        }
    }
    int ans = 0;
    while(m--)
    {
        int l = read(), r = read();
        l = (l + ans) % n + 1, r = (r + ans) % n + 1;
        if (l > r) swap(l, r);
//        printf("%d %d\n", l, r);
        ans = 0;
        if (belong[l] == belong[r]) {
            for (reg int i = l ; i <= r ; i ++)
            {
                if (!num[a[i]]) stack[++top] = a[i];
                num[a[i]]++;
                if (num[a[i]] % 2 == 0) ans++;
                if (num[a[i]] % 2 == 1 and num[a[i]] > 1) ans--;
            }
            printf("%d\n", ans);
            while(top) num[stack[top--]] = 0;
            continue;
        }
        if (belong[l] + 1 <= belong[r] - 1) ans = f[belong[l] + 1][belong[r] - 1];
//        printf("%d\n", ans);
        for (reg int i = l ; i <= R[belong[l]] ; i ++) {
            if (!num[a[i]]) stack[++top] = a[i];
            num[a[i]]++;
        }
        for (reg int i = L[belong[r]] ; i <= r ; i ++) {
            if (!num[a[i]]) stack[++top] = a[i];
            num[a[i]]++;
        }
        while(top) 
        {
            int all = cnt[belong[l] + 1][stack[top]] - cnt[belong[r]][stack[top]];
//            printf("%d %d\n", stack[top], all);
            if (!all) if (num[stack[top]] % 2 == 0) ans++;
            if (all % 2 == 1) if ((all + num[stack[top]]) % 2 == 0) ans++;
            if (all % 2 == 0 and all) if ((all + num[stack[top]]) % 2 == 1) ans--;
            num[stack[top--]] = 0;
        }
        printf("%d\n", ans);
//        puts("");
    }
    return 0;
}

 

posted @ 2018-10-08 14:03  zZhBr  阅读(135)  评论(0编辑  收藏  举报