HDU 1698 Just a Hook 区间更新 lazy标记

时间:2023-08-05 11:50:02

lazy标记

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string>
#include <algorithm>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <conio.h>
using namespace std;
#define clc(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
const int N=;
const int MOD = 1e9+;
#define LL long long
double const pi = acos(-);
void fre() {
freopen("in.txt","r",stdin);
}
// inline int r() {
// int x=0,f=1;char ch=getchar();
// while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}
// while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f;
// }
int n;
struct node{
int l,r,sum;
int lazy;
}tree[N<<]; void pushdown(int rt){
if(tree[rt].lazy!=){
tree[rt<<].lazy=tree[rt<<|].lazy=tree[rt].lazy;
tree[rt<<].sum=(tree[rt<<].r-tree[rt<<].l+)*tree[rt<<].lazy;
tree[rt<<|].sum=(tree[rt<<|].r-tree[rt<<|].l+)*tree[rt<<|].lazy;
tree[rt].lazy=;
}
} void updata(int l,int r,int x,int L,int R,int rt){
if(tree[rt].lazy==x) return;
if(l<=L&&r>=R){
tree[rt].lazy=x;
tree[rt].sum=x*(R-L+);
return;
}
pushdown(rt);
int mid=(L+R)>>;
if(r<=mid)
updata(l,r,x,L,mid,rt<<);
else if(l>mid)
updata(l,r,x,mid+,R,rt<<|);
else{
updata(l,r,x,L,mid,rt<<);
updata(l,r,x,mid+,R,rt<<|);
}
tree[rt].sum=tree[rt<<].sum+tree[rt<<|].sum;
return;
} void build(int l,int r,int rt){
tree[rt].l=l;
tree[rt].r=r;
tree[rt].lazy=;
if(l==r){
tree[rt].sum=;
return;
}
int mid=(l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
tree[rt].sum=tree[rt<<].sum+tree[rt<<|].sum;
return;
} int main(){
// fre();
int T,q,l,r,x;
scanf("%d",&T);
int cas=;
while(T--){
scanf("%d",&n);
build(,n,);
// for(int i=1;i<=18;i++)
// {
// printf("%d\n",tree[i].sum);
// }
// getch();
scanf("%d",&q);
while(q--){
scanf("%d%d%d",&l,&r,&x);
updata(l,r,x,,n,);
// for(int i=1;i<=25;i++){
// printf("%d:%d\n",i,tree[i].sum);
// }
// system("pause");
}
printf("Case %d: The total value of the hook is %d.\n",cas++,tree[].sum);
}
return ;
}