//author: ycp | https://ycpedef.github.io //#pragma GCC diagnostic error "-std=c++11" //#pragma GCC optimize(2) #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cstdarg> #include<set> #define debug(x) cerr << #x << " = " << x << endl #define debugf(...) fprintf(stderr, __VA_ARGS__) #define debugs(x) fputs(x"\n", stderr) usingnamespace std; template <classT> boolcmax(T &a, T b){ return b > a ? (a = b, 1) : 0; } template <classT> boolcmin(T &a, T b){ return b < a ? (a = b, 1) : 0; } template <classT> voidread(T &a){ a = 0; char c = getchar(); int f = 0; while (!isdigit(c)) { f ^= c == '-', c = getchar(); } while (isdigit(c)) { a = a * 10 + (c ^ 48), c = getchar(); } a *= f ? -1 : 1; } structFastin { template <classT> Fastin& operator >> (T &x) {read(x); return *this;} } fastin;
#define int long long
constint MAXN = 2e5 + 10;
template <typename T, int n> classbinary_indexed_tree { T c[n + 10]; public: voidadd(int p, T v){ p++; for (; p <= n; p += p & -p) c[p] += v; } T query(int p){ p++; T v = 0; for (; p; p -= p & -p) v += c[p]; return v; } T range(int l, int r){ return (r >= l) ? query(r) - query(l - 1) : 0; } };
int n, q; int in[MAXN], out[MAXN], id[MAXN], siz[MAXN]; set <int> pos[MAXN]; // pos where color appears binary_indexed_tree <int, MAXN> a, b;
classGraph { int tot = 0, head[MAXN] = {0}, dfx = 0; structEdge { int to; int next; } e[MAXN << 1] = {0}; public: voidaddEdge(int x, int y){ tot++; e[tot].to = y; e[tot].next = head[x]; head[x] = tot; } intdfs(int u, int f){ //debugf("dfs(%lld, %lld)\n", u, f); in[u] = ++dfx; id[in[u]] = u; siz[u] = 1; for (int i = head[u], v; i; i = e[i].next) { v = e[i].to; if (v == f) continue; siz[u] += dfs(v, u); } out[u] = ++dfx; return siz[u]; } } G;