Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <bits/stdc++.h>
- using namespace std;
- #define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
- template <typename... Args> void logger(string vars, Args &&... values)
- {
- cerr << vars << " = ";
- string delim = "";
- (..., (cerr << delim << values, delim = ", "));
- cerr << endl;
- }
- template <class T> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m)); }
- template <class T> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n)); }
- template <class T, T init> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m, init)); }
- template <class T, T init> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n, init)); }
- template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
- using ll = long long;
- using pii = pair<int, int>;
- using vl = vector<ll>;
- using vi = vector<int>;
- using a2i = array<int, 2>;
- int ans = 0;
- int n;
- a2i pts[100003];
- ll val[100003];
- map<int, int> vcnt;
- void dfs(int u, ll lb, ll ub)
- {
- if (lb > ub)
- return;
- if (u < 0)
- return;
- if (lb <= val[u] && val[u] <= ub)
- ans += vcnt[val[u]];
- dfs(pts[u][0], lb, min(ub, val[u] - 1));
- dfs(pts[u][1], max(lb, val[u] + 1), ub);
- }
- int main(int argc, char **argv)
- {
- cin >> n;
- int x, l, r;
- vi noroot(n + 1);
- for (int i = 1; i <= n; ++i) {
- cin >> x >> l >> r, pts[i][0] = l, pts[i][1] = r, val[i] = x, vcnt[x]++;
- if (l != -1)
- noroot[l] = 1;
- if (r != -1)
- noroot[r] = 1;
- }
- int root = 0;
- for (int i = 1; i <= n; ++i)
- if (noroot[i] == 0) {
- root = i;
- break;
- }
- dfs(root, LLONG_MIN / 2, LLONG_MAX / 2);
- cout << n - ans << endl;
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement