2821: 作诗(Poetize)
Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 2565 Solved: 735
[ Submit][ Status][ Discuss]
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
1 2 2 3 1
0 4
1 2
2 2
2 3
3 5
Sample Output
2
0
0
0
1
0
0
0
1
HINT
对于100%的数据,1<=n,c,m<=10^5
Source
还是分块,,
还是f[i][j]代表第i块到第j块的答案
旁边两个小部分就,,暴力一下下
预处理一个前缀和sum[i][j]:i在前j块出现次数
这样判断就变成O(1),(一开始写了个O(logn)的判断)
判断的条件,,一开始特判没写好,傻逼了
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
const int maxn = 1E5 + 10;
const int maxm = 320;
int n,m,c,cnt,Sqrt,f[maxm][maxm],vis[maxn],co[maxn],num[maxn],sum[maxn][maxm];
int Judge(int x,int A,int B)
{
int r1 = sum[x][B] - sum[x][A - 1];
int r2 = r1 + co[x];
bool flag1 = (r1 % 2 == 0);
if (!r1) flag1 = 0;
bool flag2 = (r2 % 2 == 0);
if (!flag1 && flag2) return 1;
if (flag1 && flag2) return 0;
if (flag1 && !flag2) return -1;
return 0;
}
int getint()
{
int ret = 0;
char ch = getchar();
while (ch < '0' || '9' < ch) ch = getchar();
while ('0' <= ch && ch <= '9') ret = ret*10 + ch - '0',ch = getchar();
return ret;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> n >> c >> m; Sqrt = sqrt(n);
int A,B,R = Sqrt; A = 1;
for (int i = 1; i <= n; i++) {
num[i] = getint();
++sum[num[i]][A];
if (i == R) R += Sqrt,++A;
}
for (int i = 1; i <= c; i++)
for (int j = 2; j <= Sqrt + 1; j++)
sum[i][j] += sum[i][j-1];
A = B = 0;
for (int i = 1; i <= n; i += Sqrt) {
++cnt; ++A; B = A;
R = i + Sqrt - 1; int tot = 0;
for (int j = i; j <= n; j++) {
if (vis[num[j]] != cnt) vis[num[j]] = cnt,co[num[j]] = 0,++tot;
if (co[num[j]]&1) ++tot; else --tot;
++co[num[j]];
if (j == R) f[A][B] = tot,R += Sqrt,++B;
}
}
int Lastans = 0;
while (m--) {
int L2 = getint(),R2 = getint(); //scanf("%d%d",&L2,&R2);
L2 = (L2 + Lastans)%n + 1;
R2 = (R2 + Lastans)%n + 1;
if (L2 > R2) swap(L2,R2);
int tot = 0; ++cnt;
if (R2 - L2 + 1 <= 2*Sqrt) {
for (int j = L2; j <= R2; j++) {
if (vis[num[j]] != cnt) vis[num[j]] = cnt,co[num[j]] = 0,++tot;
if (co[num[j]]&1) ++tot; else --tot;
++co[num[j]];
}
Lastans = tot;
printf("%d\n",Lastans);
continue;
}
A = L2/Sqrt + 2;
if (L2 % Sqrt == 0 || L2 % Sqrt == 1) --A;
B = R2/Sqrt;
tot = f[A][B];
int L1 = (A - 1)*Sqrt + 1;
int R1 = B*Sqrt;
for (int j = L2; j < L1; j++) {
if (vis[num[j]] != cnt) vis[num[j]] = cnt,co[num[j]] = 0;
++co[num[j]];
}
for (int j = R1 + 1; j <= R2; j++) {
if (vis[num[j]] != cnt) vis[num[j]] = cnt,co[num[j]] = 0;
++co[num[j]];
}
++cnt;
for (int j = L2; j < L1; j++) {
if (vis[num[j]] == cnt) continue;
vis[num[j]] = cnt;
tot += Judge(num[j],A,B);
}
for (int j = R1 + 1; j <= R2; j++) {
if (vis[num[j]] == cnt) continue;
vis[num[j]] = cnt;
tot += Judge(num[j],A,B);
}
Lastans = tot;
printf("%d\n",Lastans);
}
return 0;
}