Just a Hook
思路
线段树区间修改。
代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<map>
#include<queue>
#include<string>
#include<vector>
#include<utility>
#include<algorithm>
using namespace std;
const int MAXN=1e5;
int sum[MAXN<<2],arr[MAXN],add[MAXN<<2];
inline int read(){
int s=0,w=1;
char ch=getchar();
for(;ch>'9' || ch<'0';ch=getchar()){
if(ch=='-') w=-1;
}
for(;ch<='9' && ch>='0';ch=getchar()){
s=(s<<3)+(s<<1)+(ch^'0');
}
return s*w;
}
void pushUp(int rt){
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void pushDown(int rt,int ln,int rn){
add[rt<<1]=add[rt];
add[rt<<1|1]=add[rt];
sum[rt<<1]=add[rt]*ln;
sum[rt<<1|1]=add[rt]*rn;
add[rt]=0;
}
void build(int rt,int l,int r){
add[rt]=0;
if(l==r){
sum[rt]=1;
return;
}
int m=(l+r)>>1;
build(rt<<1,l,m);
build(rt<<1|1,m+1,r);
pushUp(rt);
}
void update(int rt,int l,int r,int L,int R,int C){
if(L<=l && r<=R){
sum[rt]=C*(r-l+1);
add[rt]=C;
return;
}
int m=(l+r)>>1;
if(add[rt]!=0) pushDown(rt,m-l+1,r-m);
if(L<=m) update(rt<<1,l,m,L,R,C);
if(m<R) update(rt<<1|1,m+1,r,L,R,C);
pushUp(rt);
}
int query(int rt,int l,int r,int L,int R){
if(L<=l && r<=R){
return sum[rt];
}
int m=(l+r)>>1;
if(add[rt]!=0) pushDown(rt,m-l+1,r-m);
int ans=0;
if(L<=m) ans+=query(rt<<1,l,m,L,R);
if(m<R) ans+=query(rt<<1|1,m+1,r,L,R);
return ans;
}
int main(){
int t;
t=read();
for(int pos=1;pos<=t;pos++){
int n,m;
n=read();
build(1,1,n);
m=read();
while(m--){
int L,R,C;
L=read();R=read();C=read();
update(1,1,n,L,R,C);
}
printf("Case %d: The total value of the hook is %d.\n",pos,query(1,1,n,1,n));
}
return 0;
}