您的位置:首页 > 其它

BZOJ 2821: 作诗(Poetize)

2017-05-03 21:29 225 查看

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

分析

处理ans[i,j]表示第i块到第j块的答案。

通常还需要预处理sum[i,j]表示前i块中元素j的奇偶性即可。

然后一次询问先通过ans数组得到一个答案,再暴力扫剩余部分并通过sum数组计算影响。

代码

#include <bits/stdc++.h>

#define N 100010
#define M 320

int a
;
int belong
;

int cnt
;

int sum
[M];
int ans[M][M];

int n,m;
int tot;

int read()
{
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
}

void preWork()
{
tot = floor(sqrt(n)) + 1;
for (int i = 1; i <= n; i++)
belong[i] = (i - 1) / tot + 1;
for (int i = 1; i <= n; i++)
sum[a[i]][belong[i]]++;
for (int i = 1; i < N; i++)
for (int j = 1; j <= belong
; j++)
sum[i][j] += sum[i][j - 1];
for (int i = 1; i <= belong
; i++)
{
int now = 0;
for (int j = (i - 1) * tot + 1; j <= n; j++)
{
cnt[a[j]]++;
if (cnt[a[j]] && cnt[a[j]] % 2 == 0)
now++;
else
if (cnt[a[j]] > 2)
now--;
ans[i][belong[j]] = now;
}
for (int j = (i - 1) * tot + 1; j <= n; j++)
cnt[a[j]]--;
}
}

int now;

int query(int x,int y)
{
x = (x + now) % n + 1, y = (y + now) % n + 1;
if (x > y)
std::swap(x,y);
int l = belong[x], r = belong[y];
if (r - 1 <= l)
{
now = 0;
for (int i = x; i <= y; i++)
{
cnt[a[i]]++;
if (cnt[a[i]] && cnt[a[i]] % 2 == 0)
now++;
else
if (cnt[a[i]] > 2)
now--;
}
for (int i = x; i <= y; i++)
cnt[a[i]]--;
return now;
}
now = ans[l + 1][r - 1];
for (int i = x; i <= l * tot; i++)
{
cnt[a[i]]++;
if (cnt[a[i]] + sum[a[i]][r - 1] - sum[a[i]][l] && (cnt[a[i]] + sum[a[i]][r-1] - sum[a[i]][l]) % 2 == 0)
now++;
else
if (cnt[a[i]] + sum[a[i]][r - 1] - sum[a[i]][l] > 2)
now--;
}
for (int i = (r - 1) * tot + 1; i <= y; i++)
{
cnt[a[i]]++;
if (cnt[a[i]] + sum[a[i]][r - 1] - sum[a[i]][l] && (cnt[a[i]] + sum[a[i]][r - 1] - sum[a[i]][l]) % 2 == 0)
now++;
else
if (cnt[a[i]] + sum[a[i]][r - 1] - sum[a[i]][l] > 2)
now--;
}
for (int i = x; i <= l * tot; i++)
cnt[a[i]]--;
for (int i = (r - 1) * tot + 1; i <= y; i++)
cnt[a[i]]--;
return now;
}

int main()
{
n = read(); m = read(); m = read();
for (int i = 1; i <= n; i++)
a[i] = read();
preWork();
while (m--)
{
int x,y;
x = read(); y = read();
printf("%d\n",query(x,y));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: