33022023-02-24 18:26:10gortomiXORfa visszatércpp17Time limit exceeded 28/1001.588s522192 KiB
#include <bits/stdc++.h>
using namespace std;
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 l, r, w, m;
    node *lc, *rc;
    node(int il, int ir)
    {
        l = il;
        r = ir;
        m = (l + r) / 2;
        w = 0;
        lc = nullptr;
        rc = nullptr;
    }
    void extend()
    {
        if(lc == nullptr)
        {
            lc = new node(l, m);
            rc = new node(r, m);
        }
    }
    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);
        }
    }
};
int 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 = sqrt(q) + 1;
    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 neu = node(0, 0);
    vector<node> trie(b, neu);
    vector<int> maxi(b);
    vector<vector<int> > ins(q);
    for(int i = 0; i < b; i++) trie[i] = node(0, (1 << 30) - 1);
    for(auto x : add)
    {
        int val = no[x.first];
        int sb = x.first / b  + 1, eb = x.second / b - 1;
        for(int i = sb; i <= eb; i++)
        {
            trie[i].upd(v[val], 29, 1);
            maxi[i] = max(maxi[i], trie[i].get(v[val], 29));
        }
        for(int i = x.first; i <= min(sb * b - 1, x.second); i++) ins[i].push_back(val);
        for(int i = max(eb * b + 1, x.first); i <= x.second; i++) ins[i].push_back(val);
    }
    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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1828 KiB
2Accepted3ms2064 KiB
subtask211/11
3Accepted4ms3520 KiB
4Accepted4ms3404 KiB
5Accepted4ms4580 KiB
6Accepted4ms4124 KiB
7Accepted4ms4136 KiB
8Accepted4ms4872 KiB
9Accepted4ms4496 KiB
10Accepted3ms3388 KiB
11Accepted3ms3396 KiB
12Accepted3ms3652 KiB
13Accepted3ms3612 KiB
subtask30/13
14Accepted115ms18348 KiB
15Accepted180ms20984 KiB
16Time limit exceeded1.577s75120 KiB
17Time limit exceeded1.575s83000 KiB
18Time limit exceeded1.567s76520 KiB
19Time limit exceeded1.567s75068 KiB
20Time limit exceeded1.569s91256 KiB
21Time limit exceeded1.531s82256 KiB
22Accepted185ms21516 KiB
23Accepted184ms21392 KiB
subtask417/17
24Accepted46ms32384 KiB
25Accepted28ms18876 KiB
26Accepted71ms45416 KiB
27Accepted92ms58744 KiB
28Accepted100ms66668 KiB
29Accepted41ms29428 KiB
30Accepted50ms35196 KiB
31Accepted50ms35460 KiB
32Accepted21ms6328 KiB
33Accepted21ms6488 KiB
34Accepted21ms6336 KiB
35Accepted21ms6208 KiB
subtask50/59
36Runtime error518ms522192 KiB
37Runtime error519ms522076 KiB
38Runtime error519ms521836 KiB
39Runtime error603ms521812 KiB
40Runtime error600ms521796 KiB
41Runtime error527ms521560 KiB
42Runtime error626ms521552 KiB
43Runtime error560ms521556 KiB
44Runtime error560ms521536 KiB
45Time limit exceeded1.58s130600 KiB
46Time limit exceeded1.572s213328 KiB
47Time limit exceeded1.562s204348 KiB
48Time limit exceeded1.588s214372 KiB
49Time limit exceeded1.575s212620 KiB