我感觉复杂度是n^3/2也ok的为啥要t
查看原帖
我感觉复杂度是n^3/2也ok的为啥要t
57978
胡尔克HULK楼主2020/6/22 12:23
#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
const int MAXN=500001;
int aa[MAXN],N,P,sq,L,R,now,cnt[MAXN],ans[MAXN];
struct pr{
	int l,r,d;
}q[MAXN];
struct ls{
	int x,d;
}q2[MAXN];
bool operator<(ls a,ls b)
{
	return a.x<b.x; 
}
int f(int t)
{
	return t/sq;
}
bool operator<(pr a,pr b)
{
	if (f(a.l)!=f(b.l)) return f(a.l)<f(b.l);
//		else if (f(a.l)%2==1) return a.r<b.r;
			return a.r>b.r;
}
struct lst{
	int nx[MAXN]={0},end=0,pre[MAXN]={0},s=0;
	bool in[MAXN]={0};
	void insert(int n)
	{
		if (in[n]) return;
		nx[end]=n;
		pre[n]=end;
		end=n;
		in[n]=1;
		s++;
	}
	void erase(int n)
	{
		if (!in[n]) return;
		nx[pre[n]]=nx[n];
		if (n==end) end=pre[n];
		else
		pre[nx[n]]=pre[n];
		s--;
		in[n]=0;
	}
	int first()
	{
		if (s!=0)
		return nx[0];
		else return 0;
	}
}one;
int main()
{
	cin>>N;
	for (int i=1;i<=N;i++){
		scanf("%d",&aa[i]);
	}
	sq=sqrt(N)+1; 
	cin>>P;
	for (int i=1;i<=P;i++){
		scanf("%d",&q[i].l);
		scanf("%d",&q[i].r);
		q[i].d=i;
	}
	sort(q+1,q+P+1);
	L=1,R=0;
	for (int i=1;i<=P;i++){
		while(L > q[i].l) {
			cnt[aa[--L]]++;
			if (cnt[aa[L]]==1)
				one.insert(aa[L]);
			else one.erase(aa[L]);
		}
		while(R < q[i].r){
			cnt[aa[++R]]++;
			if (cnt[aa[R]]==1)
				one.insert(aa[R]);
			else one.erase(aa[R]);
		}		
		while(L < q[i].l){
			cnt[aa[L]]--;
			if (cnt[aa[L]]==1)
				one.insert(aa[L]);
			else one.erase(aa[L]);
			L++;
		}
		while(R > q[i].r){
			cnt[aa[R]]--;
			if (cnt[aa[R]]==1)
				one.insert(aa[R]);
			else one.erase(aa[R]);	
			R--;		
		}
		ans[q[i].d]=one.first();
	}
	for (int i=1;i<=P;i++)
		cout<<ans[i]<<endl;
}
2020/6/22 12:23
加载中...