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

HDU 1698 Just a Hook(线段树成段更新)

时间:2014-08-19 09:24:44      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:style   color   os   io   for   代码   amp   size   

题意很简单:1-n个钩子初始价值是1,然后题目给出Q个操作,x y z,将x->y的钩子价值改为z,最后输出n个钩子的总价值。

知识点:线段树的成段更新。


lazy操作,在代码中的主要实现就是pudown操作了。当你要用的时候,就更新值,不然就仅仅标记它。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#define MAXN 111111
#define MOD  1000000007
#define INF  0x7fffffff
#define EPS  1e-8
#define PI acos(-1.0)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug(a) cout<<"bug---->"<<a<<endl;
#define FIN             freopen("datain.txt","r",stdin);
#define FOUT            freopen("dataout.txt","w",stdout);
#define mem(a,b)        memset(a,b,sizeof(a))
//#pragma comment         (linker,"/STACK:102400000,102400000")

typedef long long LL;
typedef unsigned long long ULL;

int col[MAXN<<2];
int sum[MAXN<<2];

void pushup(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}

void pushdown(int rt,int len)
{
    if(col[rt])
    {
        col[rt<<1]=col[rt<<1|1]=col[rt];
        sum[rt<<1]=(len-(len>>1))*col[rt];
        sum[rt<<1|1]=(len>>1)*col[rt];
        col[rt]=0;
    }
}


void build(int l,int r,int rt)
{
    col[rt]=0;
    sum[rt]=1;
    if(l==r) return;
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}

void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        col[rt]=c;
        sum[rt]=c*(r-l+1);
        return;
    }
    pushdown(rt,r-l+1);
    int m=(l+r)>>1;
    if(L<=m) update(L,R,c,lson);
    if(R>m) update(L,R,c,rson);
    pushup(rt);
}

int main()
{
    int T;
    scanf("%d",&T);
    for(int t=1;t<=T;t++)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        build(1,n,1);
        for(int i=0;i<m;i++)
        {
            int L,R,c;
            scanf("%d%d%d",&L,&R,&c);
            update(L,R,c,1,n,1);
        }
        printf("Case %d: The total value of the hook is %d.\n",t,sum[1]);
    }

    return 0;
}









HDU 1698 Just a Hook(线段树成段更新),布布扣,bubuko.com

HDU 1698 Just a Hook(线段树成段更新)

标签:style   color   os   io   for   代码   amp   size   

原文地址:http://blog.csdn.net/code_or_code/article/details/38676969

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