This commit is contained in:
Zengtudor 2025-08-23 10:49:44 +08:00
parent 8b7ef64107
commit 42fd440053
2 changed files with 50 additions and 25 deletions

View File

@ -2,50 +2,44 @@
#include <iostream>
#include <vector>
void fast_io() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
}
const int MAXN = 100005;
const int BITS = 31;
const int MAXN = 1e5+5;
const int bit = 31;
std::vector<std::pair<int, int>> adj[MAXN];
int dist[MAXN];
namespace Trie {
int trie[MAXN * BITS][2];
namespace t {
int t[MAXN * bit][2];
int nodec;
void init() {
trie[0][0] = trie[0][1] = 0;
t[0][0] = t[0][1] = 0;
nodec = 1;
}
void insert(int val) {
int curnode = 0;
for (int i = BITS - 1; i >= 0; --i) {
for (int i = bit - 1; i >= 0; --i) {
int bit = (val >> i) & 1;
if (!trie[curnode][bit]) {
trie[nodec][0] = trie[nodec][1] = 0;
trie[curnode][bit] = nodec++;
if (!t[curnode][bit]) {
t[nodec][0] = t[nodec][1] = 0;
t[curnode][bit] = nodec++;
}
curnode = trie[curnode][bit];
curnode = t[curnode][bit];
}
}
int qmaxor(int val) {
int curnode = 0;
int maxorval = 0;
for (int i = BITS - 1; i >= 0; --i) {
for (int i = bit - 1; i >= 0; --i) {
int bit = (val >> i) & 1;
if (trie[curnode][!bit]) {
if (t[curnode][!bit]) {
maxorval |= (1 << i);
curnode = trie[curnode][!bit];
curnode = t[curnode][!bit];
} else {
curnode = trie[curnode][bit];
curnode = t[curnode][bit];
}
}
return maxorval;
@ -65,7 +59,8 @@ void dfs(int u, int p, int curxorsum) {
}
int main() {
fast_io();
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
@ -80,14 +75,14 @@ int main() {
dfs(1, 0, 0);
Trie::init();
t::init();
for (int i = 1; i <= n; ++i) {
Trie::insert(dist[i]);
t::insert(dist[i]);
}
int max_ans = 0;
for (int i = 1; i <= n; ++i) {
max_ans = std::max(max_ans, Trie::qmaxor(dist[i]));
max_ans = std::max(max_ans, t::qmaxor(dist[i]));
}
std::cout << max_ans << '\n';

30
src/8/23/discrete.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <vector>
using ll = int64_t;
int main(){
ll m;
std::cin>>m;
std::vector<ll> arr;
arr.reserve(m);
for(ll i=1;i<=m;i++){
ll op;
std::cin>>op;
if(op==1){
ll x,y;
std::cin>>x>>y;
arr.push_back(x);
}else {
ll x,y;
std::cin>>x>>y;
arr.push_back(x);
arr.push_back(y);
}
}
std::sort(arr.begin(),arr.end());
arr.erase(std::unique(arr.begin(),arr.end()),arr.end());
for(ll i=0;i<arr.size();i++)std::cout<<arr[i]<<",";
std::cout<<'\n';
}