3537 2023. 02. 28 19:51:41 gortomi XORfa visszatér cpp17 Accepted 100/100 698ms 119336 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<vector<pair<int, int> > > g;
vector<int> v, ans;
vector<vector<int> > t;

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);
        }
    }
};
node root = node();
int maxi = 0;
void update(int no, int l, int r, int tl, int tr, int val)
{
    if(tl > tr) return;
    if(l == tl && r == tr)
    {
        t[no].push_back(val);
        return;
    }
    int m = (l + r) / 2;
    update(no * 2, l, m, tl, min(tr, m), val);
    update(no * 2 + 1, m + 1, r, max(tl, m + 1), tr, val);
}
void get(int no, int l, int r)
{
    int k = maxi;
    for(auto x : t[no])
    {
        root.upd(x, 29, 1);
        maxi = max(maxi, root.get(x, 29));
    }
    if(l == r) ans[l] = maxi;
    else
    {
        int m = (l + r) / 2;
        get(no * 2, l, m);
        get(no * 2 + 1, m + 1, r);
    }
    for(auto x : t[no])
    {
        root.upd(x, 29, -1);
    }
    maxi = k;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    g.resize(n + 1);
    v.resize(n + 1);
    ans.resize(q);
    t.resize(4 * q);
    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});
    for(auto x : add)
    {
        update(1, 0, q - 1, x.first, x.second, v[no[x.first]]);
    }
    get(1, 0, q - 1);
    for(auto x : ans) cout << x << "\n";
    return 0;
}
Subtask Sum Test Verdict Time Memory
subtask1 0/0
1 Accepted 3ms 1828 KiB
2 Accepted 3ms 2060 KiB
subtask2 11/11
3 Accepted 3ms 2520 KiB
4 Accepted 3ms 2748 KiB
5 Accepted 3ms 3036 KiB
6 Accepted 3ms 3184 KiB
7 Accepted 3ms 3404 KiB
8 Accepted 3ms 3572 KiB
9 Accepted 3ms 3640 KiB
10 Accepted 3ms 3420 KiB
11 Accepted 3ms 3744 KiB
12 Accepted 3ms 3892 KiB
13 Accepted 3ms 3844 KiB
subtask3 13/13
14 Accepted 74ms 27564 KiB
15 Accepted 79ms 28292 KiB
16 Accepted 178ms 33984 KiB
17 Accepted 197ms 35928 KiB
18 Accepted 207ms 35964 KiB
19 Accepted 215ms 35568 KiB
20 Accepted 192ms 35432 KiB
21 Accepted 201ms 36136 KiB
22 Accepted 83ms 28656 KiB
23 Accepted 82ms 28572 KiB
subtask4 17/17
24 Accepted 7ms 6608 KiB
25 Accepted 6ms 6520 KiB
26 Accepted 8ms 6952 KiB
27 Accepted 8ms 7280 KiB
28 Accepted 8ms 7492 KiB
29 Accepted 7ms 6608 KiB
30 Accepted 7ms 6632 KiB
31 Accepted 8ms 6628 KiB
32 Accepted 4ms 5100 KiB
33 Accepted 4ms 5096 KiB
34 Accepted 4ms 5096 KiB
35 Accepted 4ms 5160 KiB
subtask5 59/59
36 Accepted 495ms 90752 KiB
37 Accepted 384ms 80836 KiB
38 Accepted 598ms 102432 KiB
39 Accepted 698ms 113468 KiB
40 Accepted 630ms 119336 KiB
41 Accepted 481ms 90120 KiB
42 Accepted 519ms 90652 KiB
43 Accepted 453ms 90580 KiB
44 Accepted 435ms 90072 KiB
45 Accepted 206ms 70224 KiB
46 Accepted 231ms 36864 KiB
47 Accepted 212ms 36876 KiB
48 Accepted 218ms 36880 KiB
49 Accepted 216ms 36860 KiB