如题,代码如下:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+50;
const int maxNum=0x7fffffff-10;
struct node{
int l,r,sum,lazy;
}tree[maxn<<2];
void build(int p,int l,int r)
{
tree[p].l=l;
tree[p].r=r;
if(l==r){
tree[p].sum=0,tree[p].lazy=-maxNum; //注意要将标记设置为不可能作为区间赋值的数(同维护区间标记)
return ;
}
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
tree[p].sum=tree[p<<1].sum+tree[p<<1|1].sum;
}
void push_down(int p)
{
if(tree[p].lazy!=-maxNum){
tree[p<<1].lazy=tree[p<<1|1].lazy=tree[p].lazy;
int mid=(tree[p].l+tree[p].r)>>1;
tree[p<<1].sum=tree[p].lazy*(mid-tree[p].l+1);
tree[p<<1|1].sum=tree[p].lazy*(tree[p].r-mid);
tree[p].sum=tree[p].lazy*(tree[p].r-tree[p].l+1);
tree[p].lazy=-maxNum;
}
}
void update(int p,int l,int r,int v)
{
if(tree[p].l>=l&&tree[p].r<=r){
tree[p].lazy=v;
tree[p].sum=(tree[p].r-tree[p].l+1)*tree[p].lazy;
return ;
}
push_down(p);
int mid=(tree[p].l+tree[p].r)>>1;
if(l<=mid) update(p<<1,l,r,v);
if(r>mid) update(p<<1|1,l,r,v);
tree[p].sum=tree[p<<1].sum+tree[p<<1|1].sum;
}
int main()
{
int n,q;
scanf("%d%d",&n,&q);
build(1,1,n),update(1,1,n,1);
int x,y,z;
for(int i=0;i<q;i++){
scanf("%d%d%d",&x,&y,&z);
update(1,x,y,z);
}
printf("The total value of the hook is %d.\n",tree[1].sum);
return 0;
}
转载于原地址