QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#100662 | #6328. Many Products | SolitaryDream# | WA | 20ms | 76776kb | C++20 | 2.1kb | 2023-04-27 15:49:52 | 2023-04-27 15:49:56 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+1e3+7;
int n,m,Q,K;
int d[N];
tuple<int,int,int>e[N];
int son[N*31][2],tt;
using pii=pair<int,int>;
vector<pii>v[N*31];
long long fa[N],sz[N];
long long tot;
void ins(int &x,int p,int v)
{
if(!x)
x=++tt;
if(p==-1)
return;
ins(son[x][v>>p&1],p-1,v);
}
void go(int x,int p,pii e,int c)
{
if(p==-1||!x)
return;
int f=K>>p&1;
if(!f)
go(son[x][c>>p&1],p-1,e,c);
else
{
f=c>>p&1;
if(son[x][f])
v[son[x][f]].push_back(e);
go(son[x][f^1],p-1,e,c);
}
}
map<int,long long>ans;
int find(int x)
{
return x==fa[x]?x:find(fa[x]);
}
void merge(int x,int y,vector<pair<long long*,long long> >&r)
{
int fx=find(x),fy=find(y);
if(fx==fy)
return;
if(sz[fx]<sz[fy])
swap(fx,fy);
r.push_back({&tot,tot});
tot-=1ll*(sz[fx]-1)*sz[fx]/2;
tot-=1ll*(sz[fy]-1)*sz[fy]/2;
r.push_back({&(fa[fy]),fa[fy]});
fa[fy]=fx;
r.push_back({&(sz[fx]),sz[fx]});
sz[fx]+=sz[fy];
tot+=1ll*sz[fx]*(sz[fx]-1)/2;
}
void dfs(int x,int p,int val)
{
if(!x)
return;
vector<pair<long long*,long long> >rem;
for(auto [a,b]:v[x])
merge(a,b,rem);
if(p==-1)
{
ans[val]=tot;
return;
}
dfs(son[x][0],p-1,val<<1);
dfs(son[x][1],p-1,val<<1|1);
reverse(rem.begin(),rem.end());
for(auto [a,b]:rem)
*a=b;
}
int main()
{
son[0][0]=son[0][1]=-1;
scanf("%d%d%d",&n,&m,&K);
for(int i=1;i<=m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
e[i]={a,b,c};
}
scanf("%d",&Q);
int r=0;
for(int i=1;i<=Q;i++)
{
scanf("%d",&d[i]);
ins(r,30,d[i]);
}
for(int i=1;i<=m;i++)
{
auto [a,b,c]=e[i];
go(r,30,{a,b},c);
}
for(int i=1;i<=n;i++)
fa[i]=i,sz[i]=1;
dfs(r,30,0);
for(int i=1;i<=Q;i++)
printf("%lld\n",ans[d[i]]);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 20ms
memory: 76776kb
input:
2 3 0 1
output:
result:
wrong answer Answer contains longer sequence [length = 1], but output contains 0 elements