版本比较
北大集训 2021 小明的树 题解报告
正文
增加 5 行删除 5 行
34 行未改动353637383940414243444546474849505152534 行未改动585960616263646566678 行未改动34 行未改动if(mn[p] == mn[rs(p)]) cnt[p] += cnt[rs(p)], sum[p] += sum[rs(p)];}void pushdown(int p){if(tag[p][0]) {tag[ls(p)][0] += tag[p][0]; tag[rs(p)][0] += tag[p][0]; mn[ls(p)] += tag[p][0]; mn[rs(p)] += tag[p][0]; tag[p][0] = 0;}if(tag[p][1]) {tag[ls(p)][1] += tag[p][1]; tag[rs(p)][1] += tag[p][1]; sum[ls(p)] += 1ll * tag[p][1] * cnt[ls(p)]; sum[rs(p)] += 1ll * tag[p][1] * cnt[rs(p)]; tag[p][1] = 0;}}void build(int p, int l, int r){cnt[p] = mn[p] = sum[p] = tag[p][0] = tag[p][1] = 0;if(l == r) {cnt[p] = 1; mn[p] = n - l + (n == l); return;}int mid = (l + r) >> 1; build(ls(p), l, mid); build(rs(p), mid + 1, r); pushup(p);}void motify(int p, int l, int r, int ql, int qr, int x, int y){if(ql <= l and r <= qr) {tag[p][0] += x; tag[p][1] += y; mn[p] += x; sum[p] += 1ll * cnt[p] * y; return;}pushdown(p); int mid = (l + r) >> 1;if(ql <= mid) motify(ls(p), l, mid, ql, qr, x, y);if(qr > mid) motify(rs(p), mid + 1, r, ql, qr, x, y); pushup(p);}void add(int x, int y, int k){x = p[x], y = p[y]; if(x > y) swap(x, y);4 行未改动int main(){ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0);cin >> n >> q;for(int i = 1; i < n; i ++) cin >> U[i] >> V[i];for(int i = 1; i < n; i ++) cin >> a[i], p[a[i]] = i; p[a[n] = 1] = n;t.build(1, 1, n);for(int i = 1; i < n; i ++) t.add(U[i], V[i], 1); cout << t.sum[1] << "\n";for(int u, v, l, w; q; q --){cin >> u >> v >> l >> w;t.add(u, v, -1); t.add(l, w, 1);8 行未改动34 行未改动353637383940414243444546474849505152534 行未改动585960616263646566678 行未改动34 行未改动if(mn[p] == mn[rs(p)]) cnt[p] += cnt[rs(p)], sum[p] += sum[rs(p)];}void pushdown(int p){if(tag[p][0]) {tag[ls(p)][0] += tag[p][0]; tag[rs(p)][0] += tag[p][0]; mn[ls(p)] += tag[p][0]; mn[rs(p)] += tag[p][0]; tag[p][0] = 0;}if(tag[p][1]) {tag[ls(p)][1] += tag[p][1]; tag[rs(p)][1] += tag[p][1]; sum[ls(p)] += 1ll * tag[p][1] * cnt[ls(p)]; sum[rs(p)] += 1ll * tag[p][1] * cnt[rs(p)]; tag[p][1] = 0;}}void build(int p, int l, int r){cnt[p] = mn[p] = sum[p] = tag[p][0] = tag[p][1] = 0;if(l == r) {cnt[p] = 1; mn[p] = n - l + (n == l); return;}int mid = (l + r) >> 1; build(ls(p), l, mid); build(rs(p), mid + 1, r); pushup(p);}void motify(int p, int l, int r, int ql, int qr, int x, int y){if(ql <= l and r <= qr) {tag[p][0] += x; tag[p][1] += y; mn[p] += x; sum[p] += 1ll * cnt[p] * y; return;}pushdown(p); int mid = (l + r) >> 1;if(ql <= mid) motify(ls(p), l, mid, ql, qr, x, y);if(qr > mid) motify(rs(p), mid + 1, r, ql, qr, x, y); pushup(p);}void add(int x, int y, int k){x = p[x], y = p[y]; if(x > y) swap(x, y);4 行未改动int main(){ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0);cin >> n >> q;for(int i = 1; i < n; i ++) cin >> U[i] >> V[i];for(int i = 1; i < n; i ++) cin >> a[i], p[a[i]] = i; p[a[n] = 1] = n;t.build(1, 1, n);for(int i = 1; i < n; i ++) t.add(U[i], V[i], 1); cout << t.sum[1] << "\n";for(int u, v, l, w; q; q --){cin >> u >> v >> l >> w;t.add(u, v, -1); t.add(l, w, 1);8 行未改动