#include <cstdio>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define P(x) (x>>1)
#define PAUSE system("pause)
using namespace std;
#define MAXN 100005
int s[MAXN<<2];//
int c[MAXN<<2];//用来标记当前线段的权值,0表示该线段无效
int L, R, v;
void updata(int l, int r, int p)
{
if(L<=l && r<=R)
{
c[p] = v;
s[p] = v * (r-l+1);
return;
}
int m = (r+l)>>1;
if(c[p])
{
c[L(p)] = c[R(p)] = c[p];
s[L(p)] = (m-l+1)*c[p];
s[R(p)] = (r - m)*c[p];
c[p] = 0;//使此节点无效
}
if(L <= m) updata(l, m, L(p));
if(m < R) updata(m+1, r, R(p));
s[p] = s[L(p)]+s[R(p)];
}
void test(int l, int r, int p)
{
printf(" %2d %2d %2d %2d\n", l, r, s[p], c[p], p);
if(l == r) return;
int m = (l+r)>>1;
test(l, m, L(p));
test(m+1,r,R(p));
}
int main()
{
int n, q, T;
scanf("%d", &T);
for(int cas=1; cas<=T; cas++)
{
scanf("%d%d", &n, &q);
s[1] = n;
c[1] = 1;//这样可以省去build的过程
for(int i=0; i<q; i++)
{
scanf("%d%d%d", &L, &R, &v);
updata(1, n, 1);
//test(1, n, 1);
}
printf("Case %d: The total value of the hook is %d.\n",
cas, s[1]);
}
return 0;
}
hdoj 1698 Just a Hook 【线段树】
最新推荐文章于 2019-03-03 22:05:00 发布