线段树,手痒,又写了一题,我的作业,我作业落下好多了,就是不想安静下来看书
#include<iostream>
using namespace std;
const int MAX=100001;
struct T
{
int l,r,m,va;
}tree[MAX*3];
void Build_tree(int root,int l,int r)
{
tree[root].l=l;
tree[root].r=r;
tree[root].m=(l+r)>>1;
if(l==r)
{
tree[root].va=1;
return ;
}
tree[root].va=-1;
Build_tree(root<<1,l,tree[root].m);
Build_tree(root<<1|1,tree[root].m+1,r);
}
void Updata(int root,int l,int r,int va)
{
if(tree[root].l==l&&tree[root].r==r)
{
tree[root].va=va;
return ;
}
if(tree[root].va!=-1)
{
tree[root<<1].va=tree[root<<1|1].va=tree[root].va;
tree[root].va=-1;
}
if(r<=tree[root].m)
Updata(root<<1,l,r,va);
else if(l>tree[root].m)
Updata(root<<1|1,l,r,va);
else
{
Updata(root<<1,l,tree[root].m,va);
Updata(root<<1|1,tree[root].m+1,r,va);
}
}
int Query(int root)
{
if(tree[root].va!=-1)
{
return (tree[root].r-tree[root].l+1)*tree[root].va;
}
return Query(root<<1)+Query(root<<1|1);
}
int main()
{
int t,m,n,l,r,va;
int tt=1;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
Build_tree(1,1,n);
scanf("%d",&m);
while (m--)
{
scanf("%d%d%d",&l,&r,&va);
Updata(1,l,r,va);
}
printf("Case %d: The total value of the hook is %d.\n",tt++,Query(1));
}
return 0;
}