hdu_1698Just a Hook(线段树)

hdu_1698Just a Hook(线段树)

标签: 线段树


题目链接

题意:

一个英雄的技能是发射一个长度为n的金属链,初始的金属链都是铁做的,标记为1,我们可以对于某个区间修改它的金属材质,如果修改为银的标号为2,金的标号为3,现在问你经过一系列的修改后整条链子上的标记和为多少

题解:

很容易想到这个题和区间染色的问题很像

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 100005;
int col[N<<2];
void PushDown(int rt)
{
    col[rt<<1] = col[rt];
    col[rt<<1|1] = col[rt];
    col[rt] = -1;
}
void Update(int L, int R, int c, int l, int r, int rt)
{
    if(L<=l&&R>=r) {
        col[rt] = c;
        return;
    }
    if(~col[rt]) PushDown(rt);
    int m = (l+r)>>1;
    if(L<=m) Update(L,R,c,l,m,rt<<1);
    if(R>m) Update(L,R,c,m+1,r,rt<<1|1);
}
int query(int l, int r, int rt)
{
    int tm1 = 0;
    int tm2 = 0;
    if(l==r){
        //printf("%d  %d  ",l,col[rt]);
        return col[rt];
    }
    if(~col[rt]) PushDown(rt);
    int m = (l+r)>>1;
    tm1 = query(l,m,rt<<1);
    tm2 = query(m+1,r,rt<<1|1);
    return tm1+tm2;
}
int main()
{
    int t;
    int n,m;
    scanf("%d",&t);
    int l,r,w;
    int cnt = 0;
    while(t--)
    {
        cnt++;
        scanf("%d%d",&n,&m);
        for(int i = 1; i <= n; i++) col[i] = 1;
        for(int i = 1; i <= m; i++)
        {
            scanf("%d %d %d",&l,&r,&w);
            Update(l,r,w,1,n,1);
        }
        int ans = query(1,n,1);
        printf("Case %d: The total value of the hook is %d.\n",cnt,ans);
    }
    return 0;
}

posted on 2016-07-27 09:13  若流芳千古  阅读(163)  评论(0)    收藏  举报

导航