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
| struct Segment_Tree { struct Node { int l, r; int mn, cnt, mntag, valtag; LL val; } tr[N << 2]; void pushup(int x) { auto &rt = tr[x], &nl = tr[x << 1], &nr = tr[x << 1 | 1]; if (nl.mn < nr.mn) rt.mn = nl.mn, rt.val = nl.val, rt.cnt = nl.cnt; else if (nl.mn > nr.mn) rt.mn = nr.mn, rt.val = nr.val, rt.cnt = nr.cnt; else rt.mn = nl.mn, rt.val = nl.val + nr.val, rt.cnt = nl.cnt + nr.cnt; } void build(int x, int l, int r) { tr[x] = {l, r, 0, r - l + 1}; if (l == r) return; int mid = (l + r) >> 1; build(x << 1, l, mid), build(x << 1 | 1, mid + 1, r); } void update_key(int x, int c) { tr[x].mn += c, tr[x].mntag += c; } void update_val(int x, int c) { tr[x].val += (LL) c * tr[x].cnt, tr[x].valtag += c; } void pushdown(int x) { if (tr[x].mntag) update_key(x << 1, tr[x].mntag), update_key(x << 1 | 1, tr[x].mntag), tr[x].mntag = 0; if (tr[x].valtag) update_val(x << 1, tr[x].valtag), update_val(x << 1 | 1, tr[x].valtag), tr[x].valtag = 0; } void modify_key(int x, int l, int r, int c) { if (tr[x].l > r || tr[x].r < l) return; if (tr[x].l >= l && tr[x].r <= r) return update_key(x, c); pushdown(x); modify_key(x << 1, l, r, c), modify_key(x << 1 | 1, l, r, c); pushup(x); } void modify_val(int x, int l, int r, int c) { if (tr[x].l > r || tr[x].r < l) return; if (tr[x].l >= l && tr[x].r <= r) return update_val(x, c); pushdown(x); modify_val(x << 1, l, r, c), modify_val(x << 1 | 1, l, r, c); pushup(x); } } seg;
void link(int x, int y) { seg.modify_key(1, 1, std::min(nw[x], nw[y]) - 1, -1); seg.modify_val(1, std::max(nw[x], nw[y]), n - 1, -1); }
void cut(int x, int y) { seg.modify_key(1, 1, std::min(nw[x], nw[y]) - 1, 1); seg.modify_val(1, std::max(nw[x], nw[y]), n - 1, 1); }
void answer() { if (seg.tr[1].mn == 1) printf("%lld\n", seg.tr[1].val); else puts("0"); }
|