here is the problem,I learned another type of range tree and it’s a recursion version and a little slow than zkw’s tree , I made some change to fit my habit from the initial code
#include <bits/stdc++.h>
#include <cstring>
#define lc l,m,root<<1
#define rc m+1,r,root<<1|1
#define LEN r-l+1
using namespace std;
const int maxn = 100010;
int sum[maxn<<2];
int lazy[maxn<<2];
// extra space
int n;
// L R query range
// l r binary range
/**/
void up(int root)
{
sum[root] = sum[root<<1]+sum[root<<1|1];
}
void down(int root,int m)
{
if(lazy[root])
{
lazy[root<<1|1]=lazy[root<<1]=lazy[root];
sum[root<<1]=lazy[root<<1]* (m-(m>>1));
sum[root<<1|1]=lazy[root<<1|1]*(m>>1);
// change '=' to '+=' when add or sub
lazy[root] = 0;
}
}
void build(int l = 1,int r = n,int root = 1)
{
lazy[root] = 0;
if(l == r)
{
//the initialization
sum[root] = 1;
return ;
}
int m = (l+r)>>1;
build(l,m,root<<1);
build(m+1,r,root<<1|1);
up(root);
}
void update(int L,int R,int v,int l = 1,int r = n,int root = 1)
{
if(L<=l && r <= R)
{
lazy[root]=v;
sum[root]=v*(r-l+1);
// change '=' to '+=' when add or sub
return;
}
down(root,r-l+1);
int m = (r+l)>>1;
//tear down L,R to binary part
if(L <= m ) update(L,R,v,lc);
if(R >m ) update(L,R,v,rc);
up(root);
}
int querySum(int L,int R,int l = 1,int r = n,int root = 1)
{
if( L<=l && r <= R)
{
return sum[root];
}
down(root,LEN);
int m = (l+r)>>1;
int res = 0;
if(L <= m) res += querySum(L,R,lc);
if(R > m) res += querySum(L,R,rc);
return res;
}
int main()
{
int T = 0;
cin>>T;
for(int t = 1;t<=T;t++)
{
int q;
cin>>n>>q;
int l,r,v;
build();
for(int i = 0;i<q;i++)
{
scanf("%d%d%d",&l,&r,&v);
update(l,r,v);
}
cout<<"Case "<<t<<": The total value of the hook is "<<sum[1]<<"."<<endl;
}
return 0;
}