链接:https://ac.nowcoder.com/acm/contest/553/I
思路:离线整棵树,用并查集维护下联通的情况,因为值只有60个,用2的x(1<=x<=60)次方表示,树链剖分线段树区间取或维护,取得的值只要数二进制里面有多少个1就代表有多少个相同的数。
实现代码;
#includeusing namespace std;#define ll long long#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define mid ll m = (l + r) >> 1const int M = 3e5+10;ll sum[M<<2],f[M],head[M],son[M],siz[M],fa[M],dep[M];ll cnt,cnt1,n,tid[M],top[M],a[M],op[M],u[M],v[M],ans[M];struct node{ ll to,next;}e[M];ll Find(ll x){ if(x == f[x]) return x; return f[x] = Find(f[x]);}void add(ll u,ll v){ e[++cnt1].to=v;e[cnt1].next=head[u];head[u]=cnt1; e[++cnt1].to=u;e[cnt1].next=head[v];head[v]=cnt1;}void dfs1(ll u,ll faz,ll deep){ dep[u] = deep; siz[u] = 1; fa[u] = faz; //cout< <<" "< <<" "< < siz[son[u]]) son[u] = v; } }}void dfs2(ll u,ll t){ top[u] = t; tid[u] = cnt; //cout<<1< = r){ return sum[rt]; } mid; ll ret = 0; if(L <= m) ret |= query(L,R,lson); if(R > m) ret |= query(L,R,rson); return ret;}ll solve(ll x){ ll ans = 0; while(x){ ans++; x -= (x&-x); } return ans;}ll ask(ll x,ll y){ ll ans = 0; ll fx = top[x],fy = top[y]; while(fx != fy){ if(dep[fx] < dep[fy]) swap(x,y),swap(fx,fy); ans |= query(tid[fx],tid[x],1,n,1); x = fa[fx]; fx = top[x]; } if(dep[x] > dep[y]) swap(x,y); ans |= query(tid[x],tid[y],1,n,1); return ans;}int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll q; cnt = 1; cin>>n>>q; memset(son,-1,sizeof(son)); for(ll i = 1;i <= n;i ++){ cin>>a[i];f[i] = i; } for(ll i = 1;i <= q;i ++){ cin>>op[i]>>u[i]>>v[i]; if(op[i] == 1){ ll fx = Find(u[i]),fy = Find(v[i]); if(fx != fy) f[fx] = fy,add(u[i],v[i]),ans[i]=1; } else{ ll fx = Find(u[i]),fy = Find(v[i]); if(fx != fy) ans[i] = -1; } } dfs1(1,0,1); dfs2(1,0); for(ll i = 1;i <= q;i ++){ if(op[i] == 1&&ans[i]==1){ ll num = (a[u[i]] + a[v[i]])/2; a[u[i]] = a[v[i]] = num; update(tid[u[i]],1LL< <= q;i ++) if(op[i] == 2) cout< <