QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#401330#6330. XOR Reachableevirir#RE 47ms263744kbC++142.7kb2024-04-28 15:14:412024-04-28 15:14:42

Judging History

你现在查看的是最新测评结果

  • [2024-04-28 15:14:42]
  • 评测
  • 测评结果:RE
  • 用时:47ms
  • 内存:263744kb
  • [2024-04-28 15:14:41]
  • 提交

answer

#pragma GCC optimize("Ofast") 
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
const int MAXNODE = 1e7,MAXN = 1e6;
int szz[MAXN],par[MAXN];
int n,m,k;
ll cur = 0;
int find(int x){
	if(par[x] == x)return x;
	return par[x] = find(par[x]);
}
stack<pii>stk;
ll eval(ll x){
	return (x*(x-1))/2;
}
bool unite(int a,int b){
	a = find(a);
	b = find(b);
	if(a==b)return 0;
	if(szz[a] > szz[b])swap(a,b);
	cur -= eval(szz[a]);
	cur -= eval(szz[b]);
	szz[b] += szz[a];
	par[a] = b;
	cur += eval(szz[b]);
	stk.push({a,b});
	return 1;
}
void reback(){
	assert(!stk.empty());
	int a = stk.top().fi;
	int b = stk.top().se;
	stk.pop();
	cur -= eval(szz[b]);
	par[a] = a;
	szz[b] -= szz[a];
    cur += eval(szz[a]);
	cur += eval(szz[b]);
}

int ch[2][MAXNODE],nodecnt = 0;
vector<pii>edge[MAXNODE];
vector<int>ansid[MAXN];
ll finans[MAXN];
int newnode(){
	nodecnt++;
	return nodecnt;
}
void add(int v,int cur,int dep,int id){
	if(dep == -1){
		ansid[v].pb(id);
		return;
	}
	int c = (cur&(1<<dep)) > 0;
	if(!ch[c][v])ch[c][v] = newnode();
	add(ch[c][v],cur,dep-1,id);
}
void addedge(int v,int cur,int dep,pii ed){
	if(dep == -1)return;
	if(k& (1<<dep)){ //edge can be added
		int x = (cur&(1<<dep)) > 0; //edge xor with query must equal 0
		if(ch[x][v])edge[ch[x][v]].pb(ed);
	}
	int c = ((cur^k) & (1<<dep)) > 0;
	if(ch[c][v])addedge(ch[c][v],cur,dep-1,ed);
}
bool vis[MAXN];
void dfs(int v){
	int cnt = 0;
	for(pii e : edge[v])cnt+=unite(e.fi,e.se);
	for(int id:ansid[v])finans[id] = cur;
	if(ch[0][v])dfs(ch[0][v]);
	if(ch[1][v])dfs(ch[1][v]);
	while(cnt--)reback(); //reverse the changes
}
int main()
{
	owo
	cin>>n>>m>>k;
	vector<array<int,3>>edges;
	for(int i=0;i<m;i++){
		int u,v,c;
		cin>>v>>u>>c;
		edges.pb({v-1,u-1,c});
	}
	int q;
	cin>>q;
	for(int i=1;i<=q;i++){
		int d;
		cin>>d;
		add(0,d,29,i);
	}
	for(int i=0;i<n;i++){
		par[i] = i;
		szz[i] = 1;
	}
	for(auto e : edges)addedge(0,e[2],29,{e[0],e[1]});
	dfs(0);
	for(int i=1;i<=q;i++)cout<<finans[i]<<'\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 47ms
memory: 263744kb

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: 40ms
memory: 263500kb

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
Runtime Error

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:


result: