社区讨论
我已被玄学问题击败
学术版参与者 3已保存回复 6
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 6 条
- 当前快照
- 1 份
- 快照标识符
- @mhj2qp19
- 此快照首次捕获于
- 2025/11/03 19:46 4 个月前
- 此快照最后确认于
- 2025/11/03 19:46 4 个月前
CPP
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 3e6 + 10,M = 1e5 + 10;
struct sgt{
int t[N],ls[N],rs[N],cnt = 1,loc[N];
//loc[0] = 1;
void build(int u,int l,int r,int v){
if(l == r){
if(v == 1) t[u] = l;
else t[u] = 1;
return;
}
int mid = (l + r) >> 1;
ls[u] = ++cnt,rs[u] = ++cnt;
build(ls[u],l,mid,v);
build(rs[u],mid + 1,r,v);
}
void update(int u,int l,int r,int p,int x){
int id = ++cnt;t[id] = t[u];
if(l == r){
t[id] = x;
return;
}
int mid = (l + r) >> 1;
if(p <= mid){
ls[id] = cnt + 1;
rs[id] = rs[u];
update(ls[u],l,mid,p,x);
}else{
rs[id] = cnt + 1;
ls[id] = ls[u];
update(rs[u],mid + 1,r,p,x);
}
}
int query(int u,int l,int r,int p){
if(l == r) return t[u];
int mid = (l + r) >> 1;
if(p <= mid) return query(ls[u],l,mid,p);
else return query(rs[u],mid + 1,r,p);
}
}tfa,tsz;
int n,m;
int found(int u,int pos){//cerr << 666;
//cout << tfa.loc[pos - 1] << ' ' << u << '\n';
//cout << tfa.query(tfa.loc[pos - 1],1,n,u) << '\n';
cout << tfa.query(1,1,n,3) << '\n';
//int fa = tfa.query(tfa.loc[pos - 1],1,n,u);
return 0;
//if(fa == u) return u;
//else return found(fa,pos);
}
void merge(int u,int v,int pos){
u = found(u,pos),v = found(v,pos);
int szu = tsz.query(tsz.loc[pos - 1],1,n,u);
int szv = tsz.query(tsz.loc[pos - 1],1,n,v);
if(szu < szv) swap(u,v);
tfa.loc[pos] = tfa.cnt + 1;
tfa.update(tsz.loc[pos - 1],1,n,v,u);
tsz.loc[pos] = tsz.cnt + 1;
tsz.update(tsz.loc[pos - 1],1,n,u,szu + szv);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,m;cin >> n >> m;
tfa.loc[0] = tsz.loc[0] = 1;
tfa.build(1,1,n,1);
tsz.build(1,1,n,2);
//cout << tfa.cnt;
//for(int i = 1;i <= 10;i ++) cout << tfa.ls[i] << ' ' << ;
cout << tfa.query(1,1,n,3) << '\n';
return 0;
cout << found(3,1);
return 0;
for(int i = 1;i <= m;i ++){
int op,k,a,b;cin >> op;
if(op == 1){
cin >> a >> b;
merge(a,b,i);
}else if(op == 2){
cin >> k;
tfa.loc[i] = tfa.loc[k];
tsz.loc[i] = tsz.loc[k];
}else{
cin >> a >> b;
cout << (found(a,i) == found(b,i));
tfa.loc[i] = tfa.loc[i - 1];
tsz.loc[i] = tsz.loc[i - 1];
}
}
return 0;
}
这是一份可持久化并查集的代码,但是这个并不重要。
我想问为什么这份代码直接执行不会 RE,但是把 85 行删掉就会 RE?84 行和 found 函数里的东西不是一模一样吗?
回复
共 6 条回复,欢迎继续交流。
正在加载回复...