码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1698 Just a Hook

时间:2016-05-07 06:59:42      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

递归更新父节点,然后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;
}

hdu 1698 Just a Hook

标签:

原文地址:http://blog.csdn.net/qq_32995183/article/details/51335633

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!