結果

問題 No.1641 Tree Xor Query
ユーザー KudeKude
提出日時 2021-08-06 22:22:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 96 ms / 5,000 ms
コード長 4,071 bytes
コンパイル時間 5,276 ms
コンパイル使用メモリ 272,104 KB
実行使用メモリ 24,860 KB
最終ジャッジ日時 2023-10-17 03:46:28
合計ジャッジ時間 6,050 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 94 ms
24,860 KB
testcase_14 AC 96 ms
24,860 KB
testcase_15 AC 4 ms
4,348 KB
testcase_16 AC 8 ms
4,348 KB
testcase_17 AC 6 ms
4,348 KB
testcase_18 AC 6 ms
4,348 KB
testcase_19 AC 4 ms
4,348 KB
testcase_20 AC 65 ms
13,688 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
In member function 'void HLD::assign_idx(int, int, int&, int)',
    inlined from 'void HLD::assign_idx(int, int, int&, int)' at main.cpp:60:23,
    inlined from 'void HLD::assign_idx(int, int, int&, int)' at main.cpp:60:23,
    inlined from 'void HLD::assign_idx(int, int, int&, int)' at main.cpp:60:23:
main.cpp:57:19: warning: 'mi' may be used uninitialized [-Wmaybe-uninitialized]
   57 |         assign_idx(mi, h, nxt, u);
      |         ~~~~~~~~~~^~~~~~~~~~~~~~~
main.cpp: In member function 'void HLD::assign_idx(int, int, int&, int)':
main.cpp:49:13: note: 'mi' was declared here
   49 |         int mi;
      |             ^~
In member function 'void HLD::assign_idx(int, int, int&, int)',
    inlined from 'void HLD::assign_idx(int, int, int&, int)' at main.cpp:60:23,
    inlined from 'void HLD::assign_idx(int, int, int&, int)' at main.cpp:60:23:
main.cpp:57:19: warning: 'mi' may be used uninitialized [-Wmaybe-uninitialized]
   57 |         assign_idx(mi, h, nxt, u);
      |         ~~~~~~~~~~^~~~~~~~~~~~~~~
main.cpp: In member function 'void HLD::assign_idx(int, int, int&, int)':
main.cpp:49:13: note: 'mi' was declared here
   49 |         int mi;
      |             ^~
In member function 'void HLD::assign_idx(int, int, int&, int)',
    inlined from 'void HLD::assign_idx(int, int, int&, int)' at main.cpp:57:19,
    inlined from 'void HLD::assign_idx(int, int, int&, int)' at main.cpp:60:23:
main.cpp:57:19: warning: 'mi' may be used uninitialized [-Wmaybe-uninitialized]
   57 |         assign_idx(mi, h, nxt, u);
      |         ~~~~~~~~~~^~~~~~~~~~~~~~~
main.cpp: In member function 'void HLD::assign_idx(int, int, int&, int)':
main.cpp:49:13: note: 'mi' was declared here
   49 |         int mi;
      |             ^~
In member function 'void HLD::assign_idx(int, int, int&, int)',
    inlined from 'void HLD::assign_idx(int, int, int&, int)' at main.cpp:60:23,
    inlined from 'void HLD::assign_idx(int, int, int&, int)' at main.cpp:57:19:
main.cpp:57:19: warning: 'mi' m

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
#define rep(i,n)for (int i = 0; i < int(n); ++i)
#define rrep(i,n)for (int i = int(n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<class T> void chmax(T& a, const T& b) {a = max(a, b);}
template<class T> void chmin(T& a, const T& b) {a = min(a, b);}
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
struct HLD {
    std::vector<std::vector<int>> &to;
    int root, n;
    std::vector<int> sz, parent, depth, idx, ridx, head, inv;
    HLD(std::vector<std::vector<int>>& to, int root=0):
        to(to), root(root), n(to.size()),
        sz(n), parent(n), depth(n), idx(n), ridx(n), head(n), inv(n)
    {
        init_tree_data(root);
        int x = 0;
        assign_idx(root, root, x);
    }
    void init_tree_data(int u, int p=-1, int d=0) {
        parent[u] = p;
        depth[u] = d;
        int s = 1;
        for(int v: to[u]) {
            if (v == p) continue;
            init_tree_data(v, u, d+1);
            s += sz[v];
        }
        sz[u] = s;
    }
    void assign_idx(int u, int h, int &nxt, int p=-1) {
        head[u] = h;
        inv[nxt] = u;
        idx[u] = nxt++;
        if (sz[u] == 1) {
            ridx[u] = nxt;
            return;
        }
        int mxsize = 0;
        int mi;
        for(int v: to[u]) {
            if (v == p) continue;
            if (sz[v] > mxsize) {
                mxsize = sz[v];
                mi = v;
            }
        }
        assign_idx(mi, h, nxt, u);
        for(int v: to[u]) {
            if (v == p || v == mi) continue;
            assign_idx(v, v, nxt, u);
        }
        ridx[u] = nxt;
    }

    int lca(int u, int v) {
        while(head[u] != head[v]) {
            if (depth[head[u]] > depth[head[v]]) u = parent[head[u]];
            else v = parent[head[v]];
        }
        return (depth[u] < depth[v] ? u : v);
    }
    // returns (paths upto lca from x (excluding lca), those from y, lca)
    std::tuple<std::vector<std::pair<int, int>>, std::vector<std::pair<int, int>>, int> paths(int x, int y) {
        std::tuple<std::vector<std::pair<int, int>>, std::vector<std::pair<int, int>>, int> ret;
        std::vector<std::pair<int, int>>& x_paths = get<0>(ret);
        std::vector<std::pair<int, int>>& y_paths = get<1>(ret);
        int& lca = get<2>(ret);
        while(head[x] != head[y]) {
            int xhead = head[x], yhead = head[y];
            if (depth[xhead] > depth[yhead]) {
                x_paths.emplace_back(x, xhead);
                x = parent[xhead];
            } else {
                y_paths.emplace_back(y, yhead);
                y = parent[yhead];
            }
        }
        if (depth[x] > depth[y]) {
            int ychild = inv[idx[y] + 1];
            x_paths.emplace_back(x, ychild);
            x = y;
        } else if (depth[x] < depth[y]) {
            int xchild = inv[idx[x] + 1];
            y_paths.emplace_back(y, xchild);
            y = x;
        }
        lca = x;
        return ret;
    }
    
    int dist(int u, int v) {
        int w = lca(u, v);
        return depth[u] + depth[v] - 2 * depth[w];
    }
};

int op(int x, int y) {return x ^ y;}
int e() {return 0;}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    VI c_orig(n);
    rep(i, n) cin >> c_orig[i];
    VVI to(n);
    rep(_, n - 1) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        to[a].push_back(b);
        to[b].push_back(a);
    }
    HLD hld(to);
    VI c(n);
    rep(i, n) c[hld.idx[i]] = c_orig[i];
    segtree<int, op, e> seg(c);
    while(q--) {
        int t, x, y;
        cin >> t >> x >> y;
        x--;
        if (t == 1) {
            int idx = hld.idx[x];
            seg.set(idx, seg.get(idx) ^ y);
        } else {
            int ans = seg.prod(hld.idx[x], hld.ridx[x]);
            cout << ans << '\n';
        }
    }
}
0