QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#527210 | #6330. XOR Reachable | solar_express# | WA | 251ms | 120056kb | C++20 | 2.3kb | 2024-08-22 12:07:45 | 2024-08-22 12:07:46 |
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: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];
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;
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
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 6ms
memory: 5604kb
input:
4 5 5 1 2 17 1 3 4 2 3 20 2 4 3 3 4 5 4 0 7 16 167
output:
2 6 3 0
result:
ok 4 number(s): "2 6 3 0"
Test #2:
score: 0
Accepted
time: 5ms
memory: 5612kb
input:
9 13 488888932 2 7 771479959 3 8 783850182 5 7 430673756 6 8 350738034 4 9 400768807 2 3 83653266 1 2 829786563 5 8 357613791 7 9 579696618 3 7 423191200 3 5 867380255 1 9 907715012 6 9 1033650694 8 498260055 144262908 117665696 848664012 983408133 32610599 478007408 134182829
output:
16 7 5 13 13 16 16 5
result:
ok 8 numbers
Test #3:
score: -100
Wrong Answer
time: 251ms
memory: 120056kb
input:
446 99235 764320136 1 2 467639025 1 39 240791819 1 197 320023434 1 391 968343602 1 116 841220144 1 345 697806443 1 409 489643820 1 339 733516272 1 89 560099922 1 431 722346703 1 433 246809211 1 344 769002876 1 234 597076758 1 178 505730391 1 75 826728597 1 74 14756981 1 63 280238017 1 288 638627144 ...
output:
469061081558 465117651398 464539139198 469467967898 99235 472554402758 471854701718 99235 99235 99235 99235 99235 99235 99235 471272013518 99235 99235 99235 466275755798 469409830478 464596974218 99235 99235 99235 99235 99235 99235 99235 468422045138 469467967898 463729826918 99235 465291275258 9923...
result:
wrong answer 1st numbers differ - expected: '99235', found: '469061081558'