//注意要成段更新,更新到段,不能到点
//===========================================
//segment tree
//final version
//by kevin_samuel(fenice)
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
#define MAXN 100000
#define INF 0x3fffffff
int A[MAXN];
//int max;
//int min;
int T;
int N;
int Q;
struct node
{
int left;
int right;
int sum;
int color;
}Tree[MAXN<<2];
void maintain(int root)
{
int LC = root<<1;
int RC = (root<<1)+1;
Tree[root].sum = Tree[LC].sum + Tree[RC].sum;
}
void Build(int root,int start,int end)
{
Tree[root].left = start;
Tree[root].right = end;
Tree[root].color = 1;
Tree[root].sum = end - start + 1;
if(start == end)
{
return;
}
int mid = (start + end)>>1;
Build(root<<1,start,mid);
Build((root<<1)+1,mid+1,end);
maintain(root);
}
void update(int root,int start,int end,int value)
{
if(Tree[root].color == value)
return;
if(Tree[root].left == start && Tree[root].right == end)
{
Tree[root].color = value;
Tree[root].sum = (end - start + 1)*value;
return;
}
if(Tree[root].color > 0)
{
Tree[root<<1].color = Tree[root].color;
Tree[root<<1].sum = (Tree[root<<1].right - Tree[root<<1].left + 1)*Tree[root<<1].color;
Tree[(root<<1)+1].color = Tree[root].color;
Tree[(root<<1)+1].sum = (Tree[(root<<1)+1].right - Tree[(root<<1)+1].left + 1)*Tree[(root<<1)+1].color;
Tree[root].color = 0;
}
int mid = (Tree[root].left + Tree[root].right)>>1;
if(end <= mid)
update(root<<1,start,end,value);
else if(start > mid)
update((root<<1)+1,start,end,value);
else
{
update(root<<1,start,mid,value);
update((root<<1)+1,mid+1,end,value);
}
maintain(root);
}
int Query(int root,int start,int end)
{
if(start == Tree[root].left && Tree[root].right == end)
{
return Tree[root].sum;
}
int mid = (Tree[root].left + Tree[root].right)>>1;
int ret = 0;
if(end <= mid)
ret += Query(root<<1,start,end);
else if(start >= mid+1)
ret += Query((root<<1)+1,start,end);
else
{
ret += Query(root<<1,start,mid);
ret += Query((root<<1)+1,mid+1,end);
}
return ret;
}
int main()
{
cin>>T;
int count = 0;
while(T--)
{
count++;
cin>>N;
cin>>Q;
int X,Y,Z;
Build(1,1,N);
while(Q--)
{
cin>>X>>Y>>Z;
update(1,X,Y,Z);
}
//int ans = Query(1,1,N);
cout<<"Case "<<count<<": The total value of the hook is "<<Tree[1].sum<<"."<<endl;
}
return 0;
}
hdu1698线段树
最新推荐文章于 2021-10-06 21:42:49 发布