35372023-02-28 19:51:41gortomiXORfa visszatércpp17Accepted 100/100698ms119336 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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1828 KiB
2Accepted3ms2060 KiB
subtask211/11
3Accepted3ms2520 KiB
4Accepted3ms2748 KiB
5Accepted3ms3036 KiB
6Accepted3ms3184 KiB
7Accepted3ms3404 KiB
8Accepted3ms3572 KiB
9Accepted3ms3640 KiB
10Accepted3ms3420 KiB
11Accepted3ms3744 KiB
12Accepted3ms3892 KiB
13Accepted3ms3844 KiB
subtask313/13
14Accepted74ms27564 KiB
15Accepted79ms28292 KiB
16Accepted178ms33984 KiB
17Accepted197ms35928 KiB
18Accepted207ms35964 KiB
19Accepted215ms35568 KiB
20Accepted192ms35432 KiB
21Accepted201ms36136 KiB
22Accepted83ms28656 KiB
23Accepted82ms28572 KiB
subtask417/17
24Accepted7ms6608 KiB
25Accepted6ms6520 KiB
26Accepted8ms6952 KiB
27Accepted8ms7280 KiB
28Accepted8ms7492 KiB
29Accepted7ms6608 KiB
30Accepted7ms6632 KiB
31Accepted8ms6628 KiB
32Accepted4ms5100 KiB
33Accepted4ms5096 KiB
34Accepted4ms5096 KiB
35Accepted4ms5160 KiB
subtask559/59
36Accepted495ms90752 KiB
37Accepted384ms80836 KiB
38Accepted598ms102432 KiB
39Accepted698ms113468 KiB
40Accepted630ms119336 KiB
41Accepted481ms90120 KiB
42Accepted519ms90652 KiB
43Accepted453ms90580 KiB
44Accepted435ms90072 KiB
45Accepted206ms70224 KiB
46Accepted231ms36864 KiB
47Accepted212ms36876 KiB
48Accepted218ms36880 KiB
49Accepted216ms36860 KiB