TLE#9谁能教我卡常?
查看原帖
TLE#9谁能教我卡常?
794701
wo_hen_la楼主2024/7/16 16:53
#include <bits/stdc++.h>
using namespace std;

#define space putchar(' ')
#define enter putchar('\n')
#define pb push_back
#define int long long
#define P 998244353
#define HP 1000000000000002097
#define N (int)5e5+5

int read(){ char ch=getchar();int x=0,f=1;while(ch>'9' || ch<'0'){if(ch=='-') f=-1;ch=getchar();}while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();return x*f; }
void print(int x){ if(x<0) putchar('-'),x=-x;if(x>9)print(x/10);putchar(x%10+'0'); }

int n,m;
struct node{ int l,r,id; }q[N];
int loc[N],mp[N],a[N],ans[N];
int s[N],id[N],tot;
bool cmp(node x,node y)
{
	if(loc[x.l]!=loc[y.l]) return loc[x.l]<loc[y.l];
	if(loc[x.l]&1) return x.r<y.r;
	return x.r>y.r;
}
inline void add(int x)
{
	mp[x]++;
	if(mp[x]==1){
		s[++tot]=x;
		id[x]=tot;
	}
	else if(mp[x]==2){
		s[id[x]]=s[tot];
		id[s[tot]]=id[x];
		s[tot--]=id[x]=0;
	}
}
inline void del(int x)
{
	mp[x]--;
	if(mp[x]==1){
		s[++tot]=x;
		id[x]=tot;
	}
	else if(mp[x]==0){
		s[id[x]]=s[tot];
		id[s[tot]]=id[x];
		s[tot--]=id[x]=0;
	}
} 
main()
{
	n=read();
	for(int i=1,len=1000;i<=n;i++) a[i]=read(),loc[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+1+m,cmp);
	for(int i=1,l=1,r=0;i<=m;i++){
		int ql=q[i].l,qr=q[i].r;
		while(l>ql) add(a[--l]);
		while(r<qr) add(a[++r]);
		while(l<ql) del(a[l++]);
		while(r>qr) del(a[r--]);
		ans[q[i].id]=s[tot];
	}
	for(int i=1;i<=m;i++) print(ans[i]),enter;
	return 0;
}

2024/7/16 16:53
加载中...