HDU 1698-Just a Hook

来源:互联网 发布:长兴人民法院淘宝拍卖 编辑:程序博客网 时间:2024/06/07 23:49

题意:
题意:一段线段由n条单位长度线段组成,每次操作把一个区间内的线段变成金银铜之一(金的价值为3,银为2,铜为1),最初可当做全为铜;最后求这条线段的总价值。
题外话:当初刚刚学线段树,老是想着弄个线段树总模板来一劳永逸,结果喜闻乐见的崩了,线段树的强大是我无法想象的,不能一味单纯的模板,重要的是自己的思想,今天再做他,终于想出来了

#include<stdio.h>#include<string.h>#define lson root<<1,l,mid#define rson root<<1|1,mid+1,r const int MAXN=100000+7;int t[MAXN<<2],a[MAXN],add[MAXN<<2];void build(int root,int l,int r){    add[root]=0;    if(l==r)    t[root]=a[l];    else{        int mid=(l+r)/2;        build(lson);        build(rson);        t[root]=t[root<<1]+t[root<<1|1];    }}void pushdown(int root,int m){    if(add[root])    {        add[root<<1]=add[root];        add[root<<1|1]=add[root];        t[root<<1]=add[root]*(m-(m>>1));//这里直接用要变成什么金属去覆盖之前的金属就可以了,而不是加上去         t[root<<1|1]=add[root]*(m>>1);        add[root]=0;    }}void update(int root,int l,int r,int L,int R,int x){    if(r<L||l>R)    return ;    if(L<=l&&r<=R)    {        t[root]=(r-l+1)*x;        add[root]=x;        return ;    }    pushdown(root,r-l+1);    int mid=(l+r)/2;    if(L<=mid)    update(lson,L,R,x);    if(mid<R)    update(rson,L,R,x);    t[root]=t[root<<1]+t[root<<1|1];}int query(int root,int l,int r,int L,int R){    if(r<L||l>R)    return 0;    if(L<=l&&r<=R)      return t[root];    int mid=(l+r)/2;    pushdown(root,r-l+1);    return query(lson,L,R)+query(rson,L,R); }int main(){    int T,n,m,i,k=1;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        for(i=1;i<=n;i++)        a[i]=1;        scanf("%d",&m);        build(1,1,n);        while(m--)        {            int x,y,z;            scanf("%d%d%d",&x,&y,&z);            update(1,1,n,x,y,z);        }        printf("Case %d: The total value of the hook is %d.\n",k++,query(1,1,n,1,n));    }}