HDU 1698 Just a Hook (线段树,区间更新)

来源:互联网 发布:js 禁用y轴滑动 编辑:程序博客网 时间:2024/04/29 22:16

HDU 1689


模板题。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;int N, Q;const int MAXN = 100100;struct node {int l, r, m, val, book;};node T[4 * MAXN];void build(int rt, int begin, int end) {T[rt].l = begin, T[rt].r = end, T[rt].m = (begin + end) >> 1, T[rt].book = 0;if(begin == end) {T[rt].val = 1;return;}build(rt << 1, begin, T[rt].m);build(rt << 1 | 1, T[rt].m + 1, end);T[rt].val = T[rt << 1].val + T[rt << 1 | 1].val;}void updata(int rt, int l, int r, int num) {if(l == T[rt].l && r == T[rt].r) {T[rt].book = num;T[rt].val = num * (T[rt].r - T[rt].l + 1);return;}if(T[rt].book) {T[rt << 1].book = T[rt].book;T[rt << 1 | 1].book = T[rt].book;T[rt << 1].val = T[rt].book * (T[rt << 1].r - T[rt << 1].l + 1);T[rt << 1 | 1].val = T[rt].book * (T[rt << 1 | 1].r - T[rt << 1 | 1].l + 1);T[rt].book = 0;}if(l > T[rt].m) updata(rt << 1 | 1, l, r, num);else if(r <= T[rt].m) updata(rt << 1, l, r, num);else {updata(rt << 1, l, T[rt].m, num);updata(rt << 1 | 1, T[rt].m + 1, r, num);}T[rt].val = T[rt << 1].val + T[rt << 1 | 1].val;}int main() {int t;while(~scanf("%d", &t)) {int k = 1;while(t--) {scanf("%d%d", &N, &Q);build(1, 1, N);int i;int x, y, z;for(i = 0; i < Q; i++) {scanf("%d %d %d", &x, &y, &z);updata(1, x, y, z);}printf("Case %d: The total value of the hook is %d.\n", k++, T[1].val);}}    return 0;}


0 0
原创粉丝点击