0pts RE/TLE/WA 求调
查看原帖
0pts RE/TLE/WA 求调
961972
Lele_Programmer楼主2024/8/1 13:55
#include <bits/stdc++.h>
using namespace std;

const int N=100005;
const int M=325;
const int inf=1e9;

int n,m,len,bk;
int arr[N];
int idx[M];
int to[M][N];
int dis[M][M][M];
int fir[M][M];
int las[M][M];
int last;

inline int get(int x) {
	return (x-1)/len;
}

inline int getl(int x) {
	return x*len+1;
}

inline int getr(int x) {
	return getl(x+1)-1;
}

inline void init() {
	for (int i=0;i<M;++i) {
		for (int j=0;j<M;++j) {
			for (int k=0;k<M;++k) {
				dis[i][j][k]=inf;
			}
			fir[i][j]=inf;
			las[i][j]=-inf;
		}
	}
	for (int i=1;i<=n;++i) {
		if (!to[get(i)][arr[i]]) to[get(i)][arr[i]]=++idx[get(i)];
		fir[get(i)][to[get(i)][arr[i]]]=min(fir[get(i)][to[get(i)][arr[i]]],i);
		las[get(i)][to[get(i)][arr[i]]]=max(las[get(i)][to[get(i)][arr[i]]],i);
	}
	for (int i=0;i<bk;++i) {
		for (int j=getl(i);j<=getr(i);++j) {
			for (int k=getl(i);k<=getr(i);++k) {
				dis[i][to[i][arr[j]]][to[i][arr[k]]]=min(dis[i][to[i][arr[j]]][to[i][arr[k]]],abs(k-j));
			}
		}
	}
}

inline void modify(int x,int y) {
	for (int i=0;i<bk;++i) {
		if (!to[i][x]) continue;
		if (!to[i][y]) {
			to[i][y]=to[i][x],to[i][x]=0;
		} else {
			fir[i][to[i][y]]=min(fir[i][to[i][y]],fir[i][to[i][x]]);
			las[i][to[i][y]]=max(las[i][to[i][y]],las[i][to[i][x]]);
			fir[i][to[i][x]]=inf,las[i][to[i][x]]=-inf;
			for (int j=getl(i);j<=getr(i);++j) {
				dis[i][to[i][y]][to[i][arr[j]]]=min(dis[i][to[i][y]][to[i][arr[j]]],dis[i][to[i][x]][to[i][arr[j]]]);
				dis[i][to[i][arr[j]]][to[i][y]]=dis[i][to[i][y]][to[i][arr[j]]];
			}
			to[i][x]=0;
		}
	}
}

inline int query(int x,int y) {
	int maxx=-1;
	int maxy=-1;
	int ans=inf;
	for (int i=0;i<bk;++i) {
		ans=min(ans,fir[i][to[i][x]]-maxy);
		ans=min(ans,fir[i][to[i][y]]-maxx);
		maxx=max(maxx,las[i][to[i][x]]);
		maxy=max(maxy,las[i][to[i][y]]);
	}
	for (int i=0;i<bk;++i) {
		ans=min(ans,dis[i][to[i][x]][to[i][y]]);
		ans=min(ans,dis[i][to[i][y]][to[i][x]]);
	}
	if (maxx==-1 || maxy==-1) return -1;
	else return ans;
}

int main() {
//	freopen("1.in","r",stdin);
//	freopen("wa.out","w",stdout);
	scanf("%d %d",&n,&m);
	len=sqrt(n); bk=n/len; if (n%len) bk++;
	for (int i=1;i<=n;++i) {
		scanf("%d",&arr[i]);
	}
	init();
	while (m--) {
		int op,x,y;
		scanf("%d %d %d",&op,&x,&y);
		x^=last;
		y^=last;
		if (op==1) {
			if (x==y) continue;
			modify(x,y);
		} else {
			int ans=query(x,y);
			if (ans==-1) {
				last=0;
				puts("Ikaros");
			} else {
				printf("%d\n",last=ans);
			}
		}
	}
	return 0;
}

/*
5 5
1 2 2 4 4
2 3 3
x=3, y=3, Ikaros, last=0
2 2 4
x=2, y=4, 1, last=1
1 3 2
x=2, y=3, none, last=1
array: 1 3 3 4 4
1 5 5
x=4, y=4, none, last=1
array: 1 3 3 4 4
2 2 5
x=3, y=4, 1, last=1
*/
2024/8/1 13:55
加载中...