QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#152334#6330. XOR ReachableaestheticRE 24ms28220kbC++145.6kb2023-08-28 03:06:502023-08-28 03:06:51

Judging History

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

  • [2023-08-28 03:06:51]
  • 评测
  • 测评结果:RE
  • 用时:24ms
  • 内存:28220kb
  • [2023-08-28 03:06:50]
  • 提交

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;
	if(terminal[x])tempos.pb(ini[x]);
	dfs(trie[x][0]), dfs(trie[x][1]);
	fim[x] = tempo;
}
vector<vi> edges;
vector<pii> intervalo[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]) intervalo[id].pb({ini[trie[no][bx]], fim[trie[no][bx]]});
			no = trie[no][bx^1];
		}
		else no = trie[no][bx];
	}
}

map<int, ll> mapa;
struct dcq{
	vector<vector<ll>> st; int n;
	dcq(int n =112345) : st(2*n , vector<ll>()),  n(n){}
	void upd(int x , int y , ll q){ //evento Q em [X,Y] (eventos 0 index)
		for(x += n, y += n+1; x < y ; x/=2 , y/=2){
			if(x&1) st[x++].push_back(q);
			if(y&1) st[--y].push_back(q);
		}
		return;
	}
	void solve(int curr = 1){
		allin(w, st[curr]){
			int a = edges[w][0], b = edges[w][1];
			T.join(a, b);
		}
		if(curr >= n){
			mapa[curr-n] = T.ans;
		}
		else{
			solve(2*curr) ; solve(2*curr+1);
		}
		// da roll back
		for(int c=0;c<sz(st[curr]);c++)T.rollback();
		return;
	}	
};
int ininode[N];
int noe[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});
	}
	cin>>q;
	for(int i = 1; i <= q; i++){
		cin>>qr[i];
		add(qr[i]);
	}
	dcq D;
	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;
	    tempos.pb(ini[no]);
	}
	for(int i=0;i<m;i++){
		get(edges[i][2], i);
		for(auto w: intervalo[i]) tempos.pb(w.f), tempos.pb(w.s);
	}
	sort(all(tempos)), tempos.erase(unique(all(tempos)), tempos.end());
	for(int i=0;i<m;i++){
		get(edges[i][2], i);

		for(auto w: intervalo[i]){
			auto l=upper_bound(all(tempos), w.f) - tempos.begin();
			auto r=upper_bound(all(tempos), w.s) - tempos.begin();		
			D.upd(l,r,i);
			// D.upd(w.f,w.s,i);
		}
	}

	for(int i = 1; i<= q; i++){
		int x=qr[i];
		int no = noe[i];
	    ininode[i] = upper_bound(all(tempos), ini[no]) - tempos.begin();
	}

	D.solve();

	for(int i = 1; i <= q; i++){
		cout<<mapa[ ininode[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();
	}
}

詳細信息

Test #1:

score: 100
Accepted
time: 21ms
memory: 28220kb

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: 24ms
memory: 26432kb

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
Dangerous Syscalls

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: