Advertisement
pb_jiang

CF797D AC

Jan 31st, 2023
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <assert.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
  5. template <typename... Args> void logger(string vars, Args &&... values)
  6. {
  7.     cerr << vars << " = ";
  8.     string delim = "";
  9.     (..., (cerr << delim << values, delim = ", "));
  10.     cerr << endl;
  11. }
  12.  
  13. template <class T> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m)); }
  14. template <class T> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n)); }
  15. template <class T, T init> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m, init)); }
  16. template <class T, T init> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n, init)); }
  17.  
  18. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  19.  
  20. using ll = long long;
  21. using pii = pair<int, int>;
  22. using vl = vector<ll>;
  23. using vi = vector<int>;
  24. using a2i = array<int, 2>;
  25.  
  26. int ans = 0;
  27. int n;
  28. a2i pts[100003];
  29. ll val[100003];
  30. map<int, int> vcnt;
  31.  
  32. void dfs(int u, ll lb, ll ub)
  33. {
  34.     if (lb > ub)
  35.         return;
  36.     if (u < 0)
  37.         return;
  38.     if (lb <= val[u] && val[u] <= ub)
  39.         ans += vcnt[val[u]];
  40.     dfs(pts[u][0], lb, min(ub, val[u] - 1));
  41.     dfs(pts[u][1], max(lb, val[u] + 1), ub);
  42. }
  43.  
  44. int main(int argc, char **argv)
  45. {
  46.     cin >> n;
  47.     int x, l, r;
  48.     vi noroot(n + 1);
  49.     for (int i = 1; i <= n; ++i) {
  50.         cin >> x >> l >> r, pts[i][0] = l, pts[i][1] = r, val[i] = x, vcnt[x]++;
  51.         if (l != -1)
  52.             noroot[l] = 1;
  53.         if (r != -1)
  54.             noroot[r] = 1;
  55.     }
  56.     int root = 0;
  57.     for (int i = 1; i <= n; ++i)
  58.         if (noroot[i] == 0) {
  59.             root = i;
  60.             break;
  61.         }
  62.     dfs(root, LLONG_MIN / 2, LLONG_MAX / 2);
  63.     cout << n - ans << endl;
  64.     return 0;
  65. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement