标签:
递归更新父节点,然后lazy一下子节点
#include<cstdio>
#include<iostream>
using namespace std;
const int maxn=100010;
struct mytree
{
int lazy;
int me;
}tree[maxn*4];
void build(int node,int left,int right)
{
tree[node].lazy=0;
if(left==right)
{
tree[node].me=1;
return ;
}
else
{
int mid=(left+right)>>1;
build((node<<1),left,mid);
build((node<<1)+1,mid+1,right);
tree[node].me=tree[(node<<1)].me+tree[(node<<1)+1].me;
}
}
void change(int node,int left,int right,int x,int y,int v)
{
if(x<=left&&right<=y)
{
tree[node].me+=((right-left+1)*v-tree[node].me);
tree[node].lazy=v;
return ;
}
int mid=(left+right)/2;
if(tree[node].lazy)
{
tree[(node<<1)].me+=((mid-left+1)*tree[node].lazy-tree[node<<1].me);
tree[(node<<1)].lazy=tree[node].lazy;
tree[(node<<1)+1].me+=((right-(mid+1)+1)*tree[node].lazy-tree[(node<<1)+1].me);
tree[(node<<1)+1].lazy=tree[node].lazy;
tree[node].lazy=0;
}
if(y<=mid)
{
change((node<<1),left,mid,x,y,v);
}
else if(mid<x)
{
change((node<<1)+1,mid+1,right,x,y,v);
}
else
{
change((node<<1),left,mid,x,y,v);
change((node<<1)+1,mid+1,right,x,y,v);
}
tree[node].me=tree[(node<<1)].me+tree[(node<<1)+1].me;
}
int main()
{
int T;
scanf("%d",&T);
int cnt=0;
while(T--)
{
cnt++;
int n;
scanf("%d",&n);
build(1,1,n);
int m;
scanf("%d",&m);
int x;
int y;
int v;
while(m--)
{
scanf("%d%d%d",&x,&y,&v);
if(x>y)
swap(x,y);
change(1,1,n,x,y,v);
}
printf("Case %d: The total value of the hook is %d.\n",cnt,tree[1].me);
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/qq_32995183/article/details/51335633