线段树-区间更新-HDU 1689
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <iomanip>
using namespace std;
#define maxn 200005
#define MOD 1000000007
#define mem(a , b) memset(a , b , sizeof(a))
#define LL long long
#define ULL unsigned long long
const long long INF=0x3fffffff;
int n,m;
LL add[maxn<<2];
LL sum[maxn<<2];
void pushUp(int root)
{
sum[root]=sum[root<<1]+sum[root<<1|1];
}
void pushDown(int root,int m)
{
if(add[root]>0)
{
add[root<<1]=add[root];
add[root<<1|1]=add[root];
sum[root<<1]=(m-(m>>1))*add[root];
sum[root<<1|1]=(m>>1)*add[root];
add[root]=0;
}
}
void build(int l,int r,int root)
{
add[root]=0;
if(l==r)
{
sum[root]=1;
return ;
}
int mid=(l+r)>>1;
build(l,mid,root<<1);
build(mid+1,r,root<<1|1);
pushUp(root);
}
void updata(int rootL,int rootR,int v,int l,int r,int root)
{
if(rootL<=l&&rootR>=r)
{
add[root]=v;
sum[root]=add[root]*(r-l+1);
}
else
{
pushDown(root,r-l+1);
int mid=(r+l)>>1;
if(rootL<=mid)
updata(rootL, rootR, v, l, mid, root<<1);
if(rootR>mid)
updata(rootL, rootR, v, mid+1, r, root<<1|1);
pushUp(root);
}
}
int main()
{
int T;
int k=1;
scanf("%d",&T);
while(T--)
{
//mem(add,0);
scanf("%d",&n);
build(1,n,1);
scanf("%d",&m);
while(m--)
{
int rootL,rootR,v;
scanf("%d%d%d",&rootL,&rootR,&v);
updata(rootL, rootR, v, 1, n, 1);
}
printf("Case %d: The total value of the hook is ",k++);
printf("%lld.\n",sum[1]);
}
return 0;
}