Luogu P4135/XJOI Summer Camp 501 Test 12 Poems (Block)

Topic link:
Luogu: https://www.luogu.org/problem/P4135
XJOI: https://dev.xjoi.net/contest/1208

Question summary:
Number N, group M asked how many times there were positive and even numbers in [l,r] each time.

Title Description:
After Shengong SJY abused HEOI, he posed a question to SYD.
SHY is the princess of T country, and one of her hobbies is poetry.
Because of the time constraints, SHY abused OI even after he finished his poem. So SHY found an article of length N, read it M times, and read only a continuous paragraph [l,r] at a time. From this paragraph, he selected some Chinese characters to form a poem. Because SHY likes duality, SHY stipulates that every Chinese character selected must appear in [l,r] positive and even numbers. And SHY believes that the more types of Chinese characters (two identical characters are called the same kind) the better (in order to get more material!). So SHY asked LYD to arrange the selection.
LYD is such a silly * of course not, so I ask you for advice...

Input format:
Enter three integers n, c and m in the first line. Represents the number of words in an article, the number of types of Chinese characters, and the number of M times to be selected.
The second line has n integers, each number Ai between [1, c], representing a Chinese character coded as Ai.
Next, line m has two integers L and R in each line. Let the answer of the previous question be ans (when the first question is ans=0). Let L=(l+ans)%n+1, R=(r+ans)%n+1. If L > R, exchange L and R, then this question is [L,R].
Output format:
The output consists of m lines, one integer per line, and the number i represents the maximum number of types of Chinese characters that SHY can select for the first time.
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
Data range:
For 30% of the data, 1 < n,c,m < 103
For 100% data, 1 < n,c,m < 105
Time limit:
5s
Spatial constraints:
128MB

Practice:
Block size sqrt(n)sqrt(n)sqrt(n) sqrt (n);
f[i][j]f[i][j]f[i][j] f [i] [j] denotes the answer from block iii to block jjj;
id[i]id[i]id[i] ID [i] denotes the block in which the number of iii is located;
list[i]list[i]list[i] list [i] stores all locations where each number appears;

See the code for details.

#pragma GCC optimize(2)//Because I don't want to get stuck, I turn on O 2.
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#define N 100005
#define M 320
using namespace std;
int n,c,m,sz,a[N],id[N],f[M][M],cnt[N],q[N];
vector <int> list[N];
void cal(int x)//Pretreatment
{
	memset(cnt,0,sizeof(cnt));
	for (int i=(x-1)*sz+1,tot=0;i<=n;i++)
	{
		cnt[a[i]]++;
		if ((cnt[a[i]]&1)&&cnt[a[i]]!=1) tot--;
		if (cnt[a[i]]%2==0) tot++;
		if (id[i]!=id[i+1]) f[x][id[i]]=tot;
	}
	return;
}
int qsum(int x,int y,int c)
{
	return upper_bound(list[c].begin(),list[c].end(),y)-lower_bound(list[c].begin(),list[c].end(),x);
}
int query(int x,int y) //lookup
{
	int ans=f[id[x]+1][id[y]-1];
	int tot=0;
	for (int i=x;i<=min(id[x]*sz,y);i++)
	{
		if (!cnt[a[i]]) q[++tot]=a[i];
		cnt[a[i]]++;
	}
	if (id[x]!=id[y])
	{	
		for (int i=(id[y]-1)*sz+1;i<=y;i++)
		{
			if (!cnt[a[i]]) q[++tot]=a[i];
			cnt[a[i]]++;
		}
	}
	for (int i=1;i<=tot;i++)
	{
		int sum=qsum(x,y,q[i]);
		int sum1=sum-cnt[q[i]];
		int sum2=cnt[q[i]];
		if (sum1&1)
		{
			if (sum2&1) ans++;
		}
		else if (sum1)
		{
			if (sum2&1) ans--;
		}
		else
		{
			if (sum2%2==0) ans++;
		}
	}
	for (int i=1;i<=tot;i++) cnt[q[i]]=0;
	return ans;
}
int main()
{
	scanf("%d%d%d",&n,&c,&m);
	sz=sqrt(n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		id[i]=(i-1)/sz+1;
		list[a[i]].push_back(i);
	}
	for (int i=1;i<=sz+1;i++) cal(i);
	//Here's sz+1 or sz=sqrt(n)+1, and I've been here for a couple of hours.
	//If not, Lough Valley can pass, but XJOI is 80 points. (XJOI data is strong)
	memset(cnt,0,sizeof(cnt));
	int ans=0,x,y;
	while (m--)
	{
		scanf("%d%d",&x,&y);
		x=(x+ans)%n+1;
		y=(y+ans)%n+1;
		if (x>y) swap(x,y);
		ans=query(x,y);
		printf("%d\n",ans);
	}
	return 0;
}

Posted by martin_g on Sat, 03 Aug 2019 23:05:51 -0700