hdu1698线段树
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAX=100000;
int svalue;
struct node
{
int left,right,cover;
};
node tree[MAX*3];
bool used[31];
void built(int L,int R,int id)//建树
{
tree[id].cover=1;//初始化
tree[id].left=L;
tree[id].right=R;
if(L<R)
{
int mid=(L+R)/2;
built(L,mid,2*id); //左子树
built(mid+1,R,2*id+1); //右子树
}
}
void insert(int id,int L,int R,int col)
{
if(tree[id].left>=L&&tree[id].right<=R)
{
tree[id].cover=col;
return;
}
if(tree[id].left<tree[id].right)
{
int mid=(tree[id].left+tree[id].right)>>1;
if(tree[id].cover>0)
{
tree[id*2].cover=tree[id].cover;
tree[id*2+1].cover=tree[id].cover;
}
tree[id].cover=0;
if(R<=mid)
insert(id*2,L,R,col);
else if(L>mid)
insert(id*2+1,L,R,col);
else
{
insert(id*2,L,mid,col);
insert(id*2+1,mid+1,R,col);
}
}
}
void cnt(int L,int R,int id)
{
if(tree[id].cover>0)
{
svalue+=tree[id].cover*(tree[id].right-tree[id].left+1);
return;
}
if(tree[id].left<tree[id].right)
{
int mid=(tree[id].left+tree[id].right)>>1;
if(R<=mid)
cnt(L,R,id*2);
else if(L>mid)
cnt(L,R,id*2+1);
else
{
cnt(L,mid,id*2);
cnt(mid+1,R,id*2+1);
}
}
}
int main()
{
int cases,N,Q;
int i,l,r,z;
int num=1;
cin>>cases;
while(cases--)
{
scanf("%d%d",&N,&Q);
built(1,N,1);
for (i=1;i<=Q;i++)
{
scanf("%d %d %d",&l,&r,&z);
insert(1,l,r,z);
}
svalue=0;
cnt(1,N,1);
printf("Case %d: The total value of the hook is %d.\n",num++,svalue);
}
return 0;
}