标签:
链接:点击打开链接
题意:给出一个t代表几组数据,给出一个n代表1~n都为1,然后输入命令(x,y,z),(x,y)为区间,z为将区间内的数改为z
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int node[1000000*3];
int lazy[1000000];
void update(int pos,int l,int r,int ll,int rr,int z){
int mid;
if(ll<=l&&rr>=r){
lazy[pos]=z; //懒惰标记,直接更改大区间的值,以后用到小区间时再更改小区间
node[pos]=lazy[pos]*(r-l+1);
return;
}
mid=(l+r)/2;
if(lazy[pos]!=-1){ //lazy[pos]不是-1代表这个区间更改过
lazy[pos*2]=lazy[pos*2+1]=lazy[pos]; //更改小区间lazy值
node[2*pos]=lazy[pos]*(mid-l+1);
node[2*pos+1]=lazy[pos]*(r-mid); //更改小区间上线段树的值
lazy[pos]=-1; //更改结束,回复大区间上lazy得值
}
if(ll<=mid)
update(2*pos,l,mid,ll,rr,z);
if(rr>mid)
update(2*pos+1,mid+1,r,ll,rr,z);
node[pos]=node[2*pos]+node[2*pos+1]; //线段树求区间和
}
int main(){
int i,j,t,m,n,x,y,z;
scanf("%d",&t);
for(j=1;j<=t;j++){
fill(node,node+1000000*3,1); //直接用fill初始化线段树和lazy数组
fill(lazy,lazy+1000000,-1);
scanf("%d%d",&m,&n);
for(i=1;i<=n;i++){
scanf("%d%d%d",&x,&y,&z);
update(1,1,m,x,y,z);
}
printf("Case %d: The total value of the hook is %d.\n",j,node[1]);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/stay_accept/article/details/47359267