QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#152344#6330. XOR ReachableaestheticRE 3ms22140kbC++144.8kb2023-08-28 03:34:572023-08-28 03:34:58

Judging History

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

  • [2023-08-28 03:34:58]
  • 评测
  • 测评结果:RE
  • 用时:3ms
  • 内存:22140kb
  • [2023-08-28 03:34:57]
  • 提交

answer

#include "bits/stdc++.h"
#define endl '\n'
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
using namespace std;
// #define int long long
 
#define dbg_loc() cerr << __PRETTY_FUNCTION__ << " : " << __LINE__ << "\n"
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p){ 
	return os << '(' << p.first << ", " << p.second << ')'; 
}
template<typename T_container,typename T=typename enable_if<!is_same<T_container,string>::value, typename T_container::value_type>::type> 
ostream& operator<<(ostream &os, const T_container &v){ 
	os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; 
}
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T){ 
	cerr << ' ' << H; 
	dbg_out(T...); 
}
#define LOCAL
#define LOCAL
#ifdef LOCAL 
#define dbg(...) cerr<<"(" << #__VA_ARGS__<<"):" , dbg_out(__VA_ARGS__) , cerr << endl
#else
#define dbg(...)
#endif
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
ll mod = (1000000007LL);
inline ll Mod(ll a, ll b){return (a%b);}
inline ll poww(ll a, ll b){ll res = 1;while (b > 0){if(b & 1) res = (res * a)%mod;a = (a * a)%mod;b >>= 1;}return res;}
ll gcd (ll a, ll b) { while (b) { a %= b,swap(a, b);}return a;}
void read(vector<int> &w, int n){w.resize(n);for(int i = 0; i < n; i++) cin>>w[i];}
void print(vector<int> &w){for(int i =0; i < sz(w); i++){if(i == sz(w) - 1) cout<<w[i]<<"\n";else cout<<w[i]<<" ";}}
 #define allin(a , x) for(auto a : x)

///CUIDADO COM MAXN
#define N 112345 // 1E6
 
int n, m, q, k, v[N];

const int MAXL = 31;
const int MX = N * MAXL;
int trie[MX][2], qr[MX];
int cnt[MX];
int nodes = 1;

// s(s-1)/2
//sˆ2 - s
struct UF{
	vi pai, peso, S;
	vector<vector<ll>> pilha;

	ll ans = 0;
	UF(int n=0){
		pai.resize(n+1), peso.resize(n+1);
		rep(i,1,n+1) pai[i]=i,peso[i]=1;
	}
	int Find(int x){
		if(x==pai[x]) return x;
		return Find(pai[x]);
	}
	//S2 - S
	void join(int a, int b){
		a = Find(a), b= Find(b);
		pilha.pb({a, b, pai[a], pai[b], peso[a], peso[b], ans});
		if(a==b) return;
		ans += -(1LL*peso[a]*peso[a] - 1LL*peso[a]) - (1LL*peso[b]*peso[b] - 1LL*peso[b]);
		if(peso[a] < peso[b]) swap(a,b);
		peso[a] += peso[b];
		pai[b]=a;
		ans += 1LL*peso[a]*peso[a] - 1LL*peso[a];
	}

	void rollback(){
		// assert(!pilha.empty());
		auto r = pilha.back();
		int a = r[0], b = r[1];
		pai[a] = r[2], pai[b] = r[3], peso[a] = r[4], peso[b] = r[5];
		// dbg("rem", a, b);
		ans = r[6];
		pilha.pop_back();
	}
} T;

bool terminal[30*N];
void add(int x,int val = 1){
    int no = 1; // root
    cnt[no]+=val;
    for(int i=MAXL-1;i >= 0;i--){
        int b = x>>i&1;
        if(!trie[no][b])trie[no][b] = ++nodes;
        no = trie[no][b];
        cnt[no]+=val;
    }
    terminal[no] = 1;
}

int tempo=0, ini[30*N],fim[30*N];
vi tempos;
void dfs(int x){
	if(!x) return;
	ini[x] = ++tempo;
	dfs(trie[x][0]), dfs(trie[x][1]);
	fim[x] = tempo;
}
vector<vi> edges;
vector<pii> intervalo[N], pares;

vector<int> arestas[N];
int bit(int x, int i){
	if(x&(1<<i)) return 1;
	return 0;
}
void get(int x, int id){
	int no = 1;
	for(int i = MAXL - 1; i >= 0; i--){
		if(!no) return;
		int bk = bit(k, i), bx = bit(x, i);
		if(bk == 1){
			// se C e D tem o mesmo bit entao é sempre < K
			if(trie[no][bx]) arestas[trie[no][bx]].pb(id);
			no = trie[no][bx^1];
		}
		else no = trie[no][bx];
	}
}

ll mapa[2*412345];


void dfs2(int x){
	// dbg(x);
	if(!x) return;
	//liga as arestas
	for(auto w: arestas[x]){
		// dbg(w, pares[w].f, pares[w].s);
		T.join(pares[w].f, pares[w].s);
	}
	mapa[x] = T.ans;
	dfs2(trie[x][0]), dfs2(trie[x][1]);
	for(int c=0;c<sz(arestas[x]); c++) T.rollback();
}

int ininode[N];
int noe[N];

int porra[30*N];
void solve_case(){
	cin>>n>>m>>k;
	T = UF(n);
	for(int i = 1, a, b, c; i <= m; i++){
		cin>>a>>b>>c;
		edges.pb({a, b, c});
		pares.pb({a,b});
	}
	cin>>q;
	for(int i = 1; i <= q; i++){
		cin>>qr[i];
		add(qr[i]);
	}
	dfs(1);
	for(int i = 1; i<= q; i++){
		int x=qr[i];
		int no = 1; // root
	    for(int i=MAXL-1;i >= 0;i--){
	        int b = x>>i&1;
	        no = trie[no][b];
	    }
	    noe[i] = no;
	}
	for(int i=0;i<m;i++){
		get(edges[i][2], i);
	}

	for(int i = 1; i<= q; i++){
		int x=qr[i];
		int no = noe[i];
	    ininode[i] = porra[ini[no]];
	}

	dfs2(1);

	for(int i = 1; i <= q; i++){
		cout<<mapa[ noe[i] ]/2<<"\n";
	}
 
}
 
int32_t main(){
	ios::sync_with_stdio(false); cin.tie(0);
	
	int test_case=1;
	// cin>>test_case;
	while(test_case--){
		solve_case();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 19940kb

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: 2ms
memory: 22140kb

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: