MnZn刚学莫队,TLE+RE,求条玄关
查看原帖
MnZn刚学莫队,TLE+RE,求条玄关
629377
iamajcer楼主2025/2/3 12:31

TLE #7-10,RE #11-13

找不出错了啊啊。求大佬条一下。

#include <bits/stdc++.h>
using namespace std;
const int N=133343, M=1e6+5;

struct node
{
	int id, L, R, tim;
}q[N];
struct node2
{
	int k, x;
}c[N];
int n, m, x, y, a[N], qn=0, cn=0, block=0, pos[N], res=0, ans[N], cnt[M];
char op;
bool cmp(node a, node b)
{
	if (pos[a.L]!=pos[b.L]) return pos[a.L]<pos[b.L];
	if (pos[a.R]!=pos[b.R]) return pos[a.R]<pos[b.R];
	return a.tim<b.tim;
}
void add(int x)
{
	if (!cnt[x]) res++;
	cnt[x]++;
}
void del(int x)
{
	cnt[x]--;
	if (!cnt[x]) res--;
}
void change(int qL, int qR, int tim)
{
	if (qL<=c[tim].k && c[tim].k<=qR) del(a[c[tim].k]), add(c[tim].x);
	swap(a[c[tim].k], c[tim].x);
}
int main()
{
	scanf("%d%d", &n, &m);
	for (int i=1; i<=n; i++) scanf("%d", &a[i]);
	for (int i=1; i<=m; i++)
	{
		cin>>op;
		scanf("%d%d", &x, &y);
		
		if (op=='Q') q[++qn]={qn, x, y, cn};
		else c[++cn]={x, y};
	} 
	
	block=cbrt(n*cn);
	for (int i=1; i<=n; i++) pos[i]=(i-1)/block+1;
	sort(q+1, q+1+qn, cmp);
	
	for (int L=1, R=0, tim=0, i=1; i<=qn; i++)
	{
		int id=q[i].id, qL=q[i].L, qR=q[i].R, qtim=q[i].tim;
		while (L<qL) del(a[L++]);
		while (L>qL) add(a[--L]);
		while (R<qR) add(a[++R]);
		while (R>qR) del(a[R--]);
		while (tim<qtim) change(qL, qR, ++tim);
		while (tim>qtim) change(qL, qR, tim--);
		ans[id]=res;
	}
	
	for (int i=1; i<=qn; i++) printf("%d\n", ans[i]);
    return 0;
}
/*
10 2
4 2 8 5 3 7 1 9 10 9
R 4 7
Q 1 6

4 2 8 7 3 7 1 9 10 9
*/
2025/2/3 12:31
加载中...