Alex_tz307

USACO 2019 December Contest, Platinum Problem 2. Bessie's Snow Cow

Jun 1st, 2021 (edited)
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. using int64 = long long;
  5.  
  6. ifstream fin("snowcow.in");
  7. ofstream fout("snowcow.out");
  8.  
  9. struct SegTree {
  10.   int Size;
  11.   vector<int64> tree;
  12.   vector<int> lazy;
  13.  
  14.   SegTree(int n) {
  15.     Size = 1;
  16.     while (Size < n)
  17.       Size <<= 1;
  18.     tree.resize(Size << 1);
  19.     lazy.resize(Size << 1);
  20.   }
  21.  
  22.   void push(int x, int lx, int rx) {
  23.     if (lazy[x] == 0)
  24.       return;
  25.     int mid = (lx + rx) >> 1;
  26.     int64 l[] = {mid - lx + 1, rx - mid};
  27.     for (int i = 0; i < 2; ++i) {
  28.       int son = x << 1 | i;
  29.       tree[son] += l[i] * lazy[x];
  30.       lazy[son] += lazy[x];
  31.     }
  32.     lazy[x] = 0;
  33.   }
  34.  
  35.   void update(int x, int lx, int rx, int st, int dr, bool type) {
  36.     if (st <= lx && rx <= dr) {
  37.       if (type) {
  38.         tree[x] += rx - lx + 1;
  39.         ++lazy[x];
  40.       } else {
  41.         tree[x] -= rx - lx + 1;
  42.         --lazy[x];
  43.       }
  44.       return;
  45.     }
  46.     push(x, lx, rx);
  47.     int mid = (lx + rx) >> 1, lSon = x << 1, rSon = x << 1 | 1;
  48.     if (st <= mid)
  49.       update(lSon, lx, mid, st, dr, type);
  50.     if (mid < dr)
  51.       update(rSon, mid + 1, rx, st, dr, type);
  52.     tree[x] = tree[lSon] + tree[rSon];
  53.   }
  54.  
  55.   int64 query(int x, int lx, int rx, int st, int dr) {
  56.     if (st <= lx && rx <= dr)
  57.       return tree[x];
  58.     push(x, lx, rx);
  59.     int mid = (lx + rx) >> 1;
  60.     int64 ans = 0;
  61.     if (st <= mid)
  62.       ans += query(x << 1, lx, mid, st, dr);
  63.     if (mid < dr)
  64.       ans += query(x << 1 | 1, mid + 1, rx, st, dr);
  65.     return ans;
  66.   }
  67. };
  68.  
  69. const int MAXN = 1e5;
  70. vector<int> G[MAXN];
  71. int timer, l[MAXN], r[MAXN];
  72.  
  73. void dfs(int u, int p) {
  74.   l[u] = ++timer;
  75.   for (int v : G[u])
  76.     if (v != p)
  77.       dfs(v, u);
  78.   r[u] = timer;
  79. }
  80.  
  81. int main() {
  82.   int n, Q;
  83.   fin >> n >> Q;
  84.   for (int i = 1; i < n; ++i) {
  85.     int u, v;
  86.     fin >> u >> v;
  87.     --u, --v;
  88.     G[u].emplace_back(v);
  89.     G[v].emplace_back(u);
  90.   }
  91.   dfs(0, -1);
  92.   set<pair<int,int>> nodes[MAXN];
  93.   SegTree tree(n);
  94.   for (int q = 0; q < Q; ++q) {
  95.     int t, u;
  96.     fin >> t >> u;
  97.     --u;
  98.     if (t == 1) {
  99.       int c;
  100.       fin >> c;
  101.       --c;
  102.       auto it = nodes[c].upper_bound(make_pair(l[u], 0));
  103.       if (it != nodes[c].begin() && prev(it)->second >= r[u])
  104.         continue;
  105.       while (it != end(nodes[c]) && it->second <= r[u]) {
  106.         tree.update(1, 1, n, it->first, it->second, false);
  107.         nodes[c].erase(it++);
  108.       }
  109.       tree.update(1, 1, n, l[u], r[u], true);
  110.       nodes[c].emplace(l[u], r[u]);
  111.     } else fout << tree.query(1, 1, n, l[u], r[u]) << '\n';
  112.   }
  113.   fin.close();
  114.   fout.close();
  115.   return 0;
  116. }
  117.  
Add Comment
Please, Sign In to add comment