QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#527214 | #6330. XOR Reachable | solar_express# | Judgement Failed | / | / | C++20 | 2.4kb | 2024-08-22 12:14:00 | 2024-08-22 12:14:02 |
Judging History
answer
#include<bits/stdc++.h>
#define N 100005
#define ll long long
using namespace std;
int n,m,K,Q;
int rt,tot,ch[N*60][2];
ll ans[N*60],curans;
vector<pair<int,int> > tr[N*60];
void push(int &x,pair<int,int> e){
if(!x) x=++tot;
// cerr<<"new "<<x<<endl;
tr[x].push_back(e);
}
void solve(int &x,int val,int dep,pair<int,int> e){
if(!x) x=++tot;
if(dep==-1){
tr[x].push_back(e);
// cerr<<x<<endl;
return;
}
int c=(val>>dep)&1;
if(K&(1<<dep)){
// cerr<<x<<" fk "<<dep<<endl;
// cerr<<(c^1)<<endl;
push(ch[x][c],e);
solve(ch[x][c^1],val,dep-1,e);
}else{
// cerr<<c<<endl;
solve(ch[x][c],val,dep-1,e);
}
}
int fa[N],siz[N];
int find(int x){
return x==fa[x]?x:find(fa[x]);
}
struct node{
int pos,fa,siz;
};
void dfs(int x,int dep){
ll oans=curans;
vector<node> tmp;
for(auto e:tr[x]){
int a=e.first,b=e.second;
a=find(a),b=find(b);
if(siz[a]<siz[b]) swap(a,b);
if(a!=b){
tmp.push_back((node){a,fa[a],siz[a]});
tmp.push_back((node){b,fa[b],siz[b]});
fa[b]=a;
curans-=1ll*siz[a]*siz[a]+1ll*siz[b]*siz[b];
// assert(curans>=0);
siz[a]+=siz[b];
curans+=1ll*siz[a]*siz[a];
}
}
ans[x]=curans;
if(ch[x][0]&&dep>=0) dfs(ch[x][0],dep-1);
if(ch[x][1]&&dep>=0) dfs(ch[x][1],dep-1);
curans=oans;
for(int i=tmp.size()-1;i>=0;--i){
int p=tmp[i].pos;
fa[p]=tmp[i].fa;
siz[p]=tmp[i].siz;
}
}
ll qry(int x,int val,int dep){
// cerr<<x<<endl;
if(dep==-1) return ans[x];
// for(auto e:tr[x])
// cerr<<dep<<" "<<e.first<<" "<<e.second<<endl;
int c=(val>>dep)&1;
if(ch[x][c]) return qry(ch[x][c],val,dep-1);
else return ans[x];
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m>>K;
--K;
for(int i=1;i<=m;++i){
int a,b,c;
cin>>a>>b>>c;
solve(rt,c,29,make_pair(a,b));
}
for(int i=1;i<=n;++i) fa[i]=i,siz[i]=1;
curans=n;
dfs(rt,29);
cin>>Q;
for(int i=1;i<=Q;++i){
int v;
cin>>v;
if(k==0) cout<<"0\n";
else cout<<(qry(rt,v,29)-n)/2<<'\n';
}
return 0;
}
/*
4 3 5
1 3 4
2 4 3
3 4 5
1
0
*/
Details
Failed to show details