带 lazy tag 的线段树,维护区间和,含区间修改操作(最后一次询问)。
# include <stdio.h> # define MAXN (100005 * 4) char lazy[MAXN]; int sum[MAXN]; void build(int x, int y, int r) { sum[r] = y-x+1; lazy[r] = 0; if (x == y) return ; int mid = (x+y)>>1, ls = r<<1, rs = r<<1|1; build(x, mid, ls); build(mid+1, y, rs); } void update(int r) { sum[r] = sum[r<<1] + sum[r<<1|1]; } void pushdown(int x, int y, int r) { int ls, rs, mid; if (lazy[r]) { ls = r << 1, rs = r << 1 | 1, mid = (x+y)>>1; lazy[ls] = lazy[rs] = lazy[r], lazy[r] = 0; sum[ls] = lazy[ls]*(mid-x+1); sum[rs] = lazy[rs]*(y - mid); } } void change(int x, int y, int r, int s, int t, int val) { if (s<=x && y<=t) { sum[r] = val*(y-x+1); lazy[r] = val; return ; } pushdown(x, y, r); int mid = (x+y)>>1, ls = r<<1, rs = r<<1|1; if (s <= mid) change(x, mid, ls, s, t, val); if (mid+1<=t) change(mid+1, y, rs, s, t, val); update(r); } void init(void) { int n, m, x, y, z; scanf("%d%d", &n, &m); build(1, n, 1); while (m--) { scanf("%d%d%d", &x, &y, &z); change(1, n, 1, x, y, z); } } int main() { int T, i; scanf("%d", &T); for (i = 1; i <= T; ++i) { init(); printf("Case %d: The total value of the hook is %d.\n", i, sum[1]); } return 0; }