基础莫队TLE40pts求条
查看原帖
基础莫队TLE40pts求条
1023793
yaodiguoan楼主2024/4/26 14:02

提交纪录

#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define hh putchar('\n')
inline 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<<3)+(x<<1)+ch-'0';ch=getchar();}
	return x*f;
}
inline void write(int x){
    if(x<0){putchar('-');x=-x;}
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
const int N=1e6+10;
int n,m,len,pos[N];
struct node{
	int l,r,id;
} q[N];
bool cmp(node A,node B){
	if(pos[A.l]!=pos[B.l]) return pos[A.l]<pos[B.l];
	if(pos[A.l]&1) return A.r>B.r;
	return A.r<B.r;
}
int a[N],ans[N],cnt[N],L=1,R=0,now=0;

void update_add(int x){
	if(!cnt[a[x]]) now++;
	cnt[a[x]]++;
}
void update_del(int x){
	cnt[a[x]]--;
	if(!cnt[a[x]]) now--;
}
signed main(){
//	ios::sync_with_stdio(0);
//	cin.tie();cout.tie();
	n=read();
	for(int i=1;i<=n;i++) a[i]=read();
	len=sqrt(n);
	for(int i=1;i<=n;i++) pos[i]=(i-1)/len+1;
	m=read();
	for(int i=1;i<=m;i++){
		q[i].l=read(),q[i].r=read();
		q[i].id=i;
	}
 	sort(q+1,q+m+1,cmp);
	for(int i=1;i<=m;i++){
		while(L<q[i].l) update_del(L++);
		while(R>q[i].r) update_del(R--);
		while(L>q[i].l) update_add(--L);
		while(R<q[i].r) update_add(++R); 
		ans[q[i].id]=now;
	}
	for(int i=1;i<=m;i++) write(ans[i]),hh;
	return 0; 
}

2024/4/26 14:02
加载中...