HDU 1698 Just a Hook

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=6315

線段樹區間更新模板題(還少了query)。。

#include<bits/stdc++.h>
using namespace std;
#define LL long long int
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
int T,n,q;
int x,y,z;
int sum[100100<<2];
int lazy[100100<<2];
void pushup(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void pushdown(int rt,int m)
{
    if(lazy[rt])
    {
        lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
        sum[rt<<1]=(m-(m>>1))*lazy[rt];
        sum[rt<<1|1]=(m>>1)*lazy[rt];
        lazy[rt]=0;
    }
}
void build(int rt,int l,int r)
{
    lazy[rt]=0;

    if(l==r)
    {
        sum[rt]=1;
        return;
    }
    int m=l+r>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int L,int R,int x,int rt,int l,int r)
{
    if(L<=l&&r<=R)
    {
        lazy[rt]=x;
        sum[rt]=x*(r-l+1);
        return ;
    }
    pushdown(rt,r-l+1);
    int m=l+r>>1;
    if(L<=m) update(L,R,x,lson);
    if(R>m) update(L,R,x,rson);
    pushup(rt);
}
int main()
{
    scanf("%d",&T);
    int cs=0;
    while(T--)
    {
        cs++;
        scanf("%d%d",&n,&q);
        build(1,1,n);
        while(q--)
        {
            scanf("%d%d%d",&x,&y,&z);
            update(x,y,z,1,1,n);
        }
        printf("Case %d: The total value of the hook is %d.\n",cs,sum[1]);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章