1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| #include <bits/stdc++.h>
#define R register #define ll long long #define sum(a, b, mod) (((a) + (b)) % mod) #define meow(cat...) fprintf(stderr, cat)
const int MaxN = 2e5 + 10;
struct edge { int next, to; } e[MaxN << 1];
struct node { int l, r; int sum, len; };
struct query { int pos, l, r, c; } Q[MaxN << 2];
int n, m, q, cnt, now, dfscnt, ans[MaxN]; int head[MaxN], dfn[MaxN], siz[MaxN];
struct SegmentTree { node t[MaxN << 2]; void build(int id, int l, int r) { t[id].l = l, t[id].r = r; if (l == r) return; int mid = (l + r) >> 1; build(id << 1, l, mid); build(id << 1 | 1, mid + 1, r); } void pushup(int id) { int l = t[id].l, r = t[id].r; if (t[id].sum) t[id].len = r - l + 1; else t[id].len = t[id << 1].len + t[id << 1 | 1].len; } void modify(int id, int l, int r, int val) { if (t[id].r < l || r < t[id].l) return; if (l <= t[id].l && t[id].r <= r) { t[id].sum += val, pushup(id); return; } modify(id << 1, l, r, val); modify(id << 1 | 1, l, r, val), pushup(id); } } T;
int cmp(query a, query b) { return a.pos < b.pos; }
void add(int a, int b, int l, int r) { Q[++q] = (query){a, l, r, 1}; Q[++q] = (query){b + 1, l, r, -1}; }
void add_edge(int u, int v) { ++cnt; e[cnt].to = v; e[cnt].next = head[u]; head[u] = cnt; }
inline int read() { int x = 0; char ch = getchar(); while(ch > '9' || ch < '0') ch = getchar(); while(ch <= '9' && ch >= '0') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); return x; }
void dfs(int u, int fa) { dfn[u] = ++dfscnt, siz[u] = 1; for(int i = head[u]; i; i = e[i].next) { int v = e[i].to; if(v == fa) continue; dfs(v, u), siz[u] += siz[v]; } }
signed main() { n = read(), m = read(), T.build(1, 1, n); for(int i = 1; i < n; i++) { int u = read(), v = read(); add_edge(u, v), add_edge(v, u); } dfs(1, 0); for(int i = 1; i <= m; i++) { int a, b, l, r; a = read(), b = dfn[a] + siz[a] - 1, a = dfn[a]; l = read(), r = dfn[l] + siz[l] - 1, l = dfn[l]; add(a, b, a, b), add(a, b, l, r); add(l, r, a, b), add(l, r, l, r); } std::sort(Q + 1, Q + q + 1, cmp), now = 1; for(int i = 1; i <= n; i++) { while(now <= q && Q[now].pos == i) T.modify(1, Q[now].l, Q[now].r, Q[now].c), ++now; ans[i] = T.t[1].len, ans[i] ? --ans[i] : 0; } for(int i = 1; i <= n; i++) printf("%d ", ans[dfn[i]]); return 0; }
|