/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 5ms 5.863 MiB
#2 Accepted 6ms 5.934 MiB
#3 Accepted 33ms 8.566 MiB
#4 Accepted 33ms 8.578 MiB
#5 Accepted 27ms 7.496 MiB
#6 Accepted 30ms 7.73 MiB
#7 Accepted 381ms 21.324 MiB
#8 Accepted 402ms 21.305 MiB
#9 Accepted 461ms 21.348 MiB
#10 Accepted 372ms 21.344 MiB
#11 Accepted 398ms 21.355 MiB
#12 Accepted 374ms 21.34 MiB
#13 Accepted 417ms 21.355 MiB
#14 Accepted 310ms 29.543 MiB
#15 Accepted 331ms 29.48 MiB
#16 Accepted 300ms 29.457 MiB

Code

#include <bits/stdc++.h>
using namespace std;

#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
using ll = long long;

template<typename T, typename L = T>
struct SegmentTreeLazy {
  int n;
  vector<T> tree;
  vector<L> lazy;

  // ----------

  template<typename VAL_T>
  T get_tree_val(VAL_T& val) {
    return val;
  }

  T merge(T a, T b) {
    return (a + b);
  }

  T lazy_apply(T val_curr, L val_lazy, int l, int r) {
    if (val_lazy == 0) return val_curr;
    return ((r-l+1) - val_curr);
  }

  L lazy_pull(L val_curr, L val_new) {
    return (val_curr ^ val_new);
  }

  L DEF_LAZY = 0;

  // ----------

  SegmentTreeLazy(int n = 0) : n(n) {
    tree.resize(n<<2);
    lazy.resize(n<<2);
    fill(lazy.begin(), lazy.end(), DEF_LAZY);
  }

  template<typename VAL_T>
  SegmentTreeLazy(vector<VAL_T>& data) : SegmentTreeLazy((int)data.size()) {
    __build(0, 0, n-1, data);
  }

  template<typename VAL_T>
  void __build(int ti, int left, int right, vector<VAL_T>& data) {
    if (left == right) {
      tree[ti] = get_tree_val(data[left]);
      return;
    }

    int tl, tr, tm;
    tl = (ti<<1)+1;
    tr = (ti<<1)+2;
    tm = (left+right)>>1;

    __build(tl, left, tm, data);
    __build(tr, tm+1, right, data);
    tree[ti] = merge(tree[tl], tree[tr]);
  }

  void __push(int ti, int left, int right) {
    if (lazy[ti] == DEF_LAZY) return;
    tree[ti] = lazy_apply(tree[ti], lazy[ti], left, right);

    int tl, tr;
    tl = (ti<<1)+1;
    tr = (ti<<1)+2;

    if (left != right) {
      lazy[tl] = lazy_pull(lazy[tl], lazy[ti]);
      lazy[tr] = lazy_pull(lazy[tr], lazy[ti]);
    }

    lazy[ti] = DEF_LAZY;
  }

  void __update(int ti, int left, int right, int l, int r, L val) {
    __push(ti, left, right);
    if ((l > right) || (r < left)) return;
    if ((l <= left) && (right <= r)) {
      lazy[ti] = lazy_pull(lazy[ti], val);
      __push(ti, left, right);
      return;
    }

    int tl, tr, tm;
    tl = (ti<<1)+1;
    tr = (ti<<1)+2;
    tm = (left+right)>>1;

    __update(tl, left, tm, l, r, val);
    __update(tr, tm+1, right, l, r, val);
    tree[ti] = merge(tree[tl], tree[tr]);
  }

  T __query(int ti, int left, int right, int l, int r) {
    __push(ti, left, right);
    if ((l <= left) && (right <= r)) return tree[ti];

    int tl, tr, tm;
    tl = (ti<<1)+1;
    tr = (ti<<1)+2;
    tm = (left+right)>>1;

    if (l > tm) return __query(tr, tm+1, right, l, r);
    if (r <= tm) return __query(tl, left, tm, l, r);
    return merge(
      __query(tl, left, tm, l, r),
      __query(tr, tm+1, right, l, r)
    );
  }

  void update(int l, int r, L val) { __update(0, 0, n-1, l, r, val); }
  T query(int l, int r) { return __query(0, 0, n-1, l, r); }
};

const int MX = 200005;
vector<int> g[MX];
int in[MX], out[MX], Timer;

void fdfs(int u, int p = -1) {
  in[u] = Timer++;
  for (int v : g[u]) {
    if (v == p) continue;
    fdfs(v, u);
  }
  out[u] = Timer-1;
}

void init(int n) {
  for (int i = 0; i <= n; ++i) {
    g[i].clear();
  }
  Timer = 0;
}

int main() {
  FAST;
  
  int n, i, u, v, q, t;
  cin >> n;
  init(n);

  vector<int> a(n), c(n);
  for (i = 0; i < n; ++i) cin >> a[i];

  for (i = 1; i < n; ++i) {
    cin >> u >> v;
    --u; --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  fdfs(0);
  for (i = 0; i < n; ++i) c[in[i]] = a[i];
  SegmentTreeLazy<int> seg(c);

  cin >> q; while (q--) {
    cin >> t >> u; --u;
    if (t == 1) {
      seg.update(in[u], out[u], 1);
    } else {
      cout << seg.query(in[u], out[u]) << "\n";
    }
  }

  return 0;
}

Information

Submit By
Type
Submission
Problem
P1101 Mr. Heart and the Enchanted Lights
Contest
Brain Booster #6
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-03 15:35:19
Judged At
2024-11-11 02:50:52
Judged By
Score
100
Total Time
461ms
Peak Memory
29.543 MiB