結果

問題 No.1641 Tree Xor Query
ユーザー masutech16masutech16
提出日時 2021-08-06 22:53:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,240 bytes
コンパイル時間 2,104 ms
コンパイル使用メモリ 212,704 KB
実行使用メモリ 22,216 KB
最終ジャッジ日時 2023-10-17 04:23:01
合計ジャッジ時間 7,185 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 WA -
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 192 ms
22,216 KB
testcase_14 AC 189 ms
22,216 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 3,686 ms
14,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "/workspaces/compro/lib/template.hpp"


#line 1 "/workspaces/compro/lib/io/vector.hpp"
#include <iostream>
#include <vector>

#ifndef IO_VECTOR
#define IO_VECTOR

template <class T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  int size = v.size();
  for (int i = 0; i < size; i++) {
    std::cout << v[i];
    if (i != size - 1)
      std::cout << " ";
  }
  return out;
}

template <class T> std::istream &operator>>(std::istream &in, std::vector<T> &v) {
  for (auto &el : v) {
    std::cin >> el;
  }
  return in;
}

#endif
#line 4 "/workspaces/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

// 重複を消す。計算量はO(NlogN)
template <class T> void unique(std::vector<T> &v) {
  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());
}


#line 2 "main.cpp"

void solve(ll N, ll Q, const std::vector<int> &C, const std::vector<long long> &a, const std::vector<long long> &b,
           const std::vector<long long> &T, const std::vector<long long> &x, const std::vector<int> &y) {
  ll M = 2 * sqrt(N * Q);

  vector<int> cost = C;
  vector<int> dp(N, 0);
  vector<vector<int>> g(N);
  REP(i, N - 1) {
    g[a[i]].push_back(b[i]);
    g[b[i]].push_back(a[i]);
  }

  vector<bool> isLarge(N, false);
  vector<int> parent(N);
  auto dfs = [&](int cur, int par, auto self) -> void {
    int s = 0;
    dp[cur] = cost[cur];
    parent[cur] = par;
    for (const auto &el : g[cur]) {
      if (el == par)
        continue;
      self(el, cur, self);
      dp[cur] ^= dp[el];
      s++;
    }
    if (s > M)
      isLarge[cur] = true;
  };
  dfs(0, -1, dfs);

  REP(i, Q) {
    if (T[i] == 1) {
      cost[x[i]] ^= y[i];
      dp[x[i]] ^= y[i];
      int target = parent[i];
      while (target != -1 && isLarge[target]) {
        cost[target] ^= y[i];
        dp[target] ^= y[i];
        target = parent[target];
      }
    } else {
      if (isLarge[x[i]]) {
        cout << dp[x[i]] << endl;
      } else {
        int ans = cost[x[i]];
        for (const auto &el : g[x[i]]) {
          if (el == parent[x[i]])
            continue;
          ans ^= dp[el];
        }
        cout << ans << endl;
      }
    }
  }
}

// generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator)
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, Q;
  std::cin >> N;
  std::vector<int> C(N);
  vl a(N - 1), b(N - 1);
  std::cin >> Q;
  std::vector<long long> T(Q), x(Q);
  vi y(Q);
  for (int i = 0; i < N; ++i) {
    std::cin >> C[i];
  }
  for (int i = 0; i < N - 1; ++i) {
    std::cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  for (int i = 0; i < Q; ++i) {
    std::cin >> T[i] >> x[i] >> y[i];
    x[i]--;
  }
  solve(N, Q, C, a, b, T, x, y);
  return 0;
}
0