3322 | 2023-02-25 14:33:01 | gortomi | XORfa visszatér | cpp17 | Time limit exceeded 28/100 | 1.613s | 521828 KiB |
#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<vector<pair<int, int> > > g;
vector<int> v;
void dfs(int n, int p)
{
for(auto x : g[n])
{
int y = x.first, z = x.second;
if(y != p)
{
v[y] = v[n] ^ z;
dfs(y, n);
}
}
}
struct node
{
int w;
node *lc, *rc;
node()
{
w = 0;
lc = nullptr;
rc = nullptr;
}
void extend()
{
if(lc == nullptr)
{
lc = new node();
rc = new node();
}
}
void upd(int k, int ind, int del)
{
w += del;
if(ind == -1) return;
extend();
if((k >> ind) & 1) rc -> upd(k, ind - 1, del);
else lc -> upd(k, ind - 1, del);
}
int get(int k, int ind)
{
if(ind == -1) return 0;
extend();
if((k >> ind) & 1)
{
if(lc -> w > 0) return lc -> get(k, ind - 1) + (1 << ind);
else return rc -> get(k, ind - 1);
}
else
{
if(rc -> w > 0) return rc -> get(k, ind - 1) + (1 << ind);
else return lc -> get(k, ind - 1);
}
}
};
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
g.resize(n + 1);
v.resize(n + 1);
for(int i = 0; i < n - 1; i++)
{
int x, y, w;
cin >> x >> y >> w;
g[x].push_back({y, w});
g[y].push_back({x, w});
}
dfs(1, 0);
const int b = 300;
vector<int> no(q);
vector<int> act(n + 1, -1);
vector<pair<int, int> > add;
for(int i = 0; i < q; i++)
{
cin >> no[i];
if(act[no[i]] == -1)
{
act[no[i]] = i;
}
else
{
add.push_back({act[no[i]], i - 1});
act[no[i]] = -1;
}
}
for(int i = 1; i <= n; i++) if(act[i] != -1) add.push_back({act[i], q - 1});
node root = node();
vector<node> trie(b, root);
vector<int> maxi(b);
vector<vector<int> > ins(q);
for(auto x : add)
{
int l = x.first, r = x.second;
int val = no[l];
while(l % b != 0 && l <= r)
{
ins[l].push_back(val);
l++;
}
while(l + b - 1 <= r)
{
trie[l / b].upd(v[val], 29, 1);
maxi[l / b] = max(maxi[l / b], trie[l / b].get(v[val], 29));
l += b;
}
while(l <= r)
{
ins[l].push_back(val);
l++;
}
}
for(int i = 0; i < q; i++)
{
int actb = i / b;
int ans = maxi[actb];
for(auto x : ins[i])
{
trie[actb].upd(v[x], 29, 1);
ans = max(ans, trie[actb].get(v[x], 29));
}
cout << ans << "\n";
for(auto x : ins[i]) trie[actb].upd(v[x], 29, -1);
}
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1836 KiB | ||||
2 | Accepted | 3ms | 2060 KiB | ||||
subtask2 | 11/11 | ||||||
3 | Accepted | 4ms | 2528 KiB | ||||
4 | Accepted | 3ms | 2580 KiB | ||||
5 | Accepted | 4ms | 2752 KiB | ||||
6 | Accepted | 4ms | 2860 KiB | ||||
7 | Accepted | 4ms | 3072 KiB | ||||
8 | Accepted | 4ms | 3408 KiB | ||||
9 | Accepted | 4ms | 3336 KiB | ||||
10 | Accepted | 4ms | 3516 KiB | ||||
11 | Accepted | 4ms | 3604 KiB | ||||
12 | Accepted | 4ms | 3648 KiB | ||||
13 | Accepted | 4ms | 3592 KiB | ||||
subtask3 | 0/13 | ||||||
14 | Accepted | 86ms | 20708 KiB | ||||
15 | Accepted | 123ms | 23460 KiB | ||||
16 | Time limit exceeded | 1.567s | 124772 KiB | ||||
17 | Time limit exceeded | 1.557s | 146548 KiB | ||||
18 | Time limit exceeded | 1.572s | 137720 KiB | ||||
19 | Time limit exceeded | 1.582s | 167104 KiB | ||||
20 | Time limit exceeded | 1.552s | 140116 KiB | ||||
21 | Time limit exceeded | 1.595s | 146492 KiB | ||||
22 | Accepted | 133ms | 24168 KiB | ||||
23 | Accepted | 123ms | 24268 KiB | ||||
subtask4 | 17/17 | ||||||
24 | Accepted | 75ms | 10496 KiB | ||||
25 | Accepted | 41ms | 8548 KiB | ||||
26 | Accepted | 112ms | 13392 KiB | ||||
27 | Accepted | 144ms | 15148 KiB | ||||
28 | Accepted | 174ms | 16468 KiB | ||||
29 | Accepted | 64ms | 10808 KiB | ||||
30 | Accepted | 85ms | 11540 KiB | ||||
31 | Accepted | 87ms | 11768 KiB | ||||
32 | Accepted | 68ms | 10652 KiB | ||||
33 | Accepted | 68ms | 10668 KiB | ||||
34 | Accepted | 68ms | 10764 KiB | ||||
35 | Accepted | 68ms | 10768 KiB | ||||
subtask5 | 0/59 | ||||||
36 | Runtime error | 754ms | 521828 KiB | ||||
37 | Runtime error | 630ms | 521820 KiB | ||||
38 | Runtime error | 652ms | 521800 KiB | ||||
39 | Runtime error | 638ms | 521776 KiB | ||||
40 | Runtime error | 732ms | 521768 KiB | ||||
41 | Runtime error | 651ms | 521676 KiB | ||||
42 | Runtime error | 633ms | 521660 KiB | ||||
43 | Runtime error | 615ms | 521660 KiB | ||||
44 | Runtime error | 615ms | 521644 KiB | ||||
45 | Time limit exceeded | 1.564s | 109492 KiB | ||||
46 | Time limit exceeded | 1.582s | 237536 KiB | ||||
47 | Time limit exceeded | 1.57s | 238116 KiB | ||||
48 | Time limit exceeded | 1.613s | 254384 KiB | ||||
49 | Time limit exceeded | 1.578s | 241176 KiB |