hdu1698线段树

//注意要成段更新,更新到段,不能到点
//===========================================
//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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值