https://codeforces.com/problemset/problem/1725/K
发现题目涉及值域的区间覆盖,可以考虑对值域维护珂朵莉树(应该是类似珂朵莉树思想的东西)。
但要把值域对应回原位置,我们可以拿并查集维护。
#include
using namespace std;
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar(); while(ch<'0'||
ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
//mt19937 rand(time(0));
//mt19937_64 rand(time(0));
//srand(time(0));
#define N 5000010
//#define M
//#define mo
struct node {
int x, id;
bool operator <(const node &A) const {
if(x==A.x) return id<A.id;
return x<A.x;
}
};
int n, m, i, j, k, T;
set<node>s;
set<node>::iterator it1, it2, it;
int f[N], p[N], a[N], l, r, q, w, mid, op;
int fa(int x) {
if(f[x]==x) return x;
return f[x]=fa(f[x]);
}
signed main()
{
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// T=read();
// while(T--) {
//
// }
n=read();
for(i=1; i<=n; ++i) {
a[i]=read(); s.insert({a[i], i});
p[i]=i; f[i]=i;
}
// for(it=s.begin(); it!=s.end(); ++it) printf("%d(%d) ", it->x, it->id);
// printf("\n");
q=read();
auto work = [&] (int l, int r, int k) -> void{
// printf("[%d %d] => %d\n", l, r, k);
a[++n]=k; f[n]=n; s.insert({a[n], n});
it1=it=s.lower_bound({l, 0}), it2=s.lower_bound({r+1, 0});
// printf("> %d(%d) %d(%d)\n", it1->id, it1->x, it2->id, it2->x);
for(; it1!=it2; ++it1) {
// printf("%d -> %d\n", it1->id, n);
f[fa(it1->id)]=fa(n);
}
s.erase(it, it2);
};
while(q--) {
op=read();
if(op==1) {
k=read(); w=read();
p[k]=++n; a[n]=w; f[n]=n;
s.insert({a[n], n});
}
if(op==2) {
k=read();
printf("%lld\n", a[fa(p[k])]);
}
if(op==3) {
l=read(); r=read();
mid=(l+r)/2;
work(l, mid, l-1);
work(mid+1, r, r+1);
}
// for(it=s.begin(); it!=s.end(); ++it) printf("%d(%d) ", it->x, it->id);
// printf("\n");
}
return 0;
}