HDU_1698
直接用线段树对区间成段更新即可。
#include<stdio.h>
#include<string.h>
#define MAXD 100010
int N, M, flag[4 * MAXD], sum[4 * MAXD];
void build(int cur, int x, int y)
{
int mid = (x + y) >> 1, ls = cur << 1, rs = (cur << 1) | 1;
flag[cur] = 0, sum[cur] = 1;
if(x == y)
return ;
build(ls, x, mid);
build(rs, mid + 1, y);
}
void init()
{
scanf("%d%d", &N, &M);
build(1, 1, N);
}
void pushdown(int cur, int x, int y)
{
if(flag[cur])
{
int mid = (x + y) >> 1, ls = cur << 1, rs = (cur << 1) | 1;
flag[ls] = flag[rs] = flag[cur];
sum[ls] = (mid - x + 1) * flag[cur];
sum[rs] = (y - mid) * flag[cur];
flag[cur] = 0;
}
}
void update(int cur)
{
sum[cur] = sum[cur << 1] + sum[(cur << 1) | 1];
}
void change(int cur, int x, int y, int s, int t, int v)
{
int mid = (x + y) >> 1, ls = cur << 1, rs = (cur << 1) | 1;
if(x >= s && y <= t)
{
flag[cur] = v;
sum[cur] = v * (y - x + 1);
return ;
}
pushdown(cur, x, y);
if(mid >= s)
change(ls, x, mid, s, t, v);
if(mid + 1 <= t)
change(rs, mid + 1, y, s, t, v);
update(cur);
}
void solve()
{
int i, x, y, z;
for(i = 0; i < M; i ++)
{
scanf("%d%d%d", &x, &y, &z);
change(1, 1, N, x, y, z);
}
printf("The total value of the hook is %d.\n", sum[1]);
}
int main()
{
int t, tt;
scanf("%d", &t);
for(tt = 0; tt < t; tt ++)
{
init();
printf("Case %d: ", tt + 1);
solve();
}
return 0;
}