样例过了,叫上去RE,还有两个点Tle,下载了个数据,试了下,发现输出没问题,求助。
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read() {
int x=0,f=0;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) f|=(ch=='-');
for(;isdigit(ch);ch=getchar()) x=(x<<1)+(x<<3)+(ch^48);
return f?-x:x;
}
void print(int x) {
if(x<0) putchar('-'),x=-x;
if(x>9) print(x/10);
putchar(x%10+48);
}
int n,m,x,y,z;
struct node{
int ls,rs,dis,val,root;
}tree[30203123];
#define lson tree[x].ls
#define rson tree[x].rs
int find(int x) {
if (tree[x].root!=x) return tree[x].root=find(tree[x].root);
return x;
}
int Merge(int x,int y) {
if (!x||!y) return x+y;
if (tree[x].val>tree[y].val||(tree[x].val==tree[y].val&&x>y)) swap(x,y);
rson=Merge(rson,y);
if (tree[lson].dis<tree[rson].dis) swap(lson,rson);
tree[lson].root=tree[rson].root=tree[x].root=x;
tree[x].dis=tree[rson].dis+1;
return x;
}
int Pop(int x) {
tree[x].val=-1; tree[lson].root=lson; tree[rson].root=rson; tree[x].root=Merge(lson,rson);
}
signed main() {
n=read();m=read();
tree[0].dis=-1;
for (int i=1;i<=n;++i) {
tree[i].root=i;
tree[i].val=read();
}
for (int i=1;i<=m;++i) {
int op=read();
if (op==1) {
x=read(); y=read();
if (tree[x].val==-1||tree[y].val==-1) continue;
int f1=find(x),f2=find(y);
if (f1!=f2) tree[f1].root=tree[f2].root=Merge(f1,f2);
}
else {
x=read();
if (tree[x].val==-1) printf("-1\n");
else {
printf("%d\n",tree[find(x)].val);
Pop(find(x));
}
}
}
return 0;
}