1 10 2 1 5 2 5 9 3
Case 1: The total value of the hook is 24.
线段树区间更新 裸题,以后写线段树的风格大概就是这样了。。深受NotOlnySccess的影响,风格都是模仿他写的。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <ctype.h>
#define lson o << 1, l, m
#define rson o << 1|1, m+1, r
using namespace std;
typedef long long LL;
const int MAX = 0x3f3f3f3f;
const int maxn = 100010;
int t, n, q, a, b, c;
int sum[maxn<<2], tt[maxn<<2];
void up(int o) {
sum[o] = sum[o<<1] + sum[o<<1|1];
}
void down(int o, int m) {
if(tt[o]) {
tt[o<<1] = tt[o<<1|1] = tt[o];
sum[o<<1] = tt[o]*( m - (m>>1) );
sum[o<<1|1] = tt[o]*(m>>1);
tt[o] = 0;
}
}
void build(int o, int l, int r) {
if(l == r) {
sum[o] = 1;
return ;
}
int m = (l+r) >> 1;
build(lson);
build(rson);
up(o);
}
void update(int o, int l, int r) {
if(a <= l && r <= b) {
tt[o] = c;
sum[o] = c*(r-l+1);
return ;
}
int m = (l+r) >> 1;
down(o, r-l+1);
if(a <= m) update(lson);
if(m < b ) update(rson);
up(o);
}
int main()
{
scanf("%d", &t); for(int ca = 1; ca <= t; ca++) {
scanf("%d", &n);
build(1, 1, n);
scanf("%d", &q);
memset(tt, 0, sizeof(tt));
while(q--) {
scanf("%d%d%d", &a, &b, &c);
update(1, 1, n);
}
printf("Case %d: The total value of the hook is ", ca);
printf("%d.\n", sum[1] );
}
return 0;
}
HDU 1698 Just a Hook (线段树区间更新),布布扣,bubuko.com
HDU 1698 Just a Hook (线段树区间更新)
原文:http://blog.csdn.net/u013923947/article/details/38538533