D貌似要用离散化的思想,还不会,下次补。
#include <iostream>
#include <cstdio>
#include <cstring>
#define lid (id << 1)
#define rid (id << 1 | 1)
using namespace std;
const int maxn = 1e5+6;
int n,a[maxn],q;
struct node
{
int l,r;
long long sum,lazy;
void updata(long long x)
{
sum = (r-l+1)*x; //修改
lazy=x;
}
} tr[maxn*4];
void push_up(int id)
{
tr[id].sum=tr[lid].sum+tr[rid].sum;
}
void push_down(int id)
{
int lazyvalue=tr[id].lazy;
if(lazyvalue)
{
tr[lid].updata(lazyvalue);
tr[rid].updata(lazyvalue);
tr[id].lazy=0;
}
}
void build(int id,int l,int r)
{
tr[id].l=l,tr[id].r=r,tr[id].lazy=0;
if(l==r) tr[id].sum=1;
else
{
int mid=(l+r)/2;
build(lid,l,mid);
build(rid,mid+1,r);
push_up(id);
}
}
void updata(int id,int l,int r,long long val)
{
if(l==tr[id].l&&tr[id].r==r) tr[id].updata(val);
else
{
push_down(id);
int mid = (tr[id].l+tr[id].r)>>1;
if(r<=mid) updata(lid,l,r,val);
else if (l>mid) updata(rid,l,r,val);
else
{
updata(lid,l,mid,val);
updata(rid,mid+1,r,val);
}
push_up(id);
}
}
long long query_sum(int id,int l,int r)
{
if(l==tr[id].l&&tr[id].r==r) return tr[id].sum;
else
{
push_down(id);
int mid = (tr[id].l+tr[id].r)/2;
if(r<=mid) return query_sum(lid,l,r);
else if(l>mid) return query_sum(rid,l,r);
else return query_sum(lid,l,mid)+query_sum(rid,mid+1,r);
push_up(id);
}
}
int main(){
int m,ca,num=1;
scanf("%d",&m);
while(m--){
scanf("%d%d",&n,&ca);
build(1,1,n);
while(ca--){
int l,r,x;
scanf("%d%d%d",&l,&r,&x);
updata(1,l,r,x);
}
printf("Case %d: The total value of the hook is %d.\n",num++,query_sum(1,1,n));
}
return 0;
}