![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/56ee48805cb9517fd70b2e314804e33f.png)
Input:
5
2 6 5 6 2
9
1 5 10
2 5
1 1 3
3 3 6
3 8 9
1 2 9
2 3
2 2
2 4
Output
10
7
9
7
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2686febd10b942fcd100e680a57fee7f.png)
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
const ll INF = 1e18 + 10;
const int maxn = 4e5 + 5;
const int mod = 1e9+7;
int fa[2 * maxn];
int b[maxn];
int a[2 * maxn];
map<int,int> mp;
int find(int x){
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void merge(int pos,int val){
if(mp[val]){
int y = mp[val];
fa[find(pos)] = find(y);
}else{
mp[val] = find(pos);
a[find(pos) ] = val;
}
}
void cover(int L,int R,int val){
for(auto t = mp.lower_bound(L); t != mp.end() && t->first <= R;t = mp.erase(t)){
merge(t->second,val);
}
}
int main()
{
IOS;
int _ = 1;
while(_--){
int n;
cin>>n;
for(int i = 1;i <= n;i++){
int w;
cin>>w;
a[i] = w;
fa[i] = b[i] = i;
merge(i,w);
}
int Q;
cin>>Q;
while(Q--){
int op;
cin>>op;
if(op == 1){
int pos;
int val;
cin>>pos>>val;
n++;
b[pos] = n;
fa[n] = n;
merge(n,val);
}else if(op == 2){
int pos;
cin>>pos;
cout<<a[find(b[pos])]<<"\n";
}else{
int L,R;
cin>>L>>R;
int mid = (L + R) / 2;
cover(L,mid,L-1);
cover(mid + 1,R,R+1);
}
}
}
}