E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

时间:2022-03-28 11:34:59

题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少

思路:标准线段树区间和模版题

 #include<cstdio>
#include<algorithm>
#include<set>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=1e5+;
struct Node{
int l,r,sum;
int lazy;
void update(int val){
sum=(r-l+)*val;
lazy=val; }
}tree[maxn*];
void push_up(int x){
tree[x].sum=tree[x<<].sum+tree[x<<|].sum;
}
void push_down(int x){
if(tree[x].lazy!=){
tree[x<<].update(tree[x].lazy);
tree[x<<|].update(tree[x].lazy);
tree[x].lazy=;
}
}
void build(int x,int l,int r){
tree[x].l=l,tree[x].r=r;
tree[x].sum=tree[x].lazy=;
if(l==r){
tree[x].sum=;
}
else {
int mid=l+r>>;
build(x<<,l,mid);
build(x<<|,mid+,r);
push_down(x);
}
}
void update(int x,int l,int r,int c){
int L=tree[x].l,R=tree[x].r;
if(R<=r&&L>=l){
tree[x].update(c);
}
else {
int mid=L+R>>;
push_down(x);
if(mid>=l)update(x<<,l,r,c);
if(mid<r)update(x<<|,l,r,c);
push_up(x);
}
}
long long query(int x,int l,int r){
int L=tree[x].l,R=tree[x].r;
if(R<=r&&L>=l){
return tree[x].sum;
}
else {
int mid=L+R>>;
long long ans=;
//push_down(x);
if(mid>=l)ans+=query(x<<,l,r);
if(mid<r)ans+=query(x<<|,l,r);
return ans;
}
}
int main(){
int t,kase=;
scanf("%d",&t);
while(t--){
int n,q;
scanf("%d%d",&n,&q);
build(,,n);
for(int i=;i<q;i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
update(,x,y,z);
}
printf("Case %d: The total value of the hook is %lld.\n",kase++,query(,,n));
}
return ;
}