hdu1698或HRBUST 1820

in the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.

Now Pudge wants to do some operations on the hook.

Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.

Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.
题目大意;
有一根棍子,起初都是铜的,价值为1,现在我们可以将任意连续的一段换成银的(价值为2)或金的(价值为3),求q次操作后,棍子的价值

区间修改,区间查询

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=100000;

struct node
{int l,r,sum;
  int mid;

}tree[4*N];
int yan[4*N];//因为每次修改我们不可能等找到了叶子节点再进行修改,故我们需要一个延迟节点
void  build(int l,int r,int i)
{tree[i].l=l;
 tree[i].r=r;
 tree[i].mid=(l+r)/2;
 if(l==r)//叶子节点
 {
   tree[i].sum=1;
   return ;
 }

 build(l,tree[i].mid,i*2);
 build(tree[i].mid+1,r,i*2+1);
 tree[i].sum=tree[i*2].sum+tree[i*2+1].sum;


}

void pushdown(int i)//因为每个节点的权值取决于他的长度故需要一个 len变量
{  if(yan[i]>0)//存在延迟
     {yan[2*i]=yan[i];
      yan[2*i+1]=yan[i];//把延迟传个2个儿子;
      tree[2*i].sum=yan[i]*(tree[2*i].r-tree[2*i].l+1);
      tree[2*i+1].sum=yan[i]*(tree[2*i+1].r-tree[2*i+1].l+1);//对2个儿子进行更新
       yan[i]=0;
       

     }


}



void updata(int l,int r,int x,int i)
{  if(tree[i].r==r&&tree[i].l==l)
    {
        tree[i].sum=x*(tree[i].r-tree[i].l+1);
        yan[i]=x;
        return ;
    }
      pushdown(i);
  if(tree[2*i].l<=l&&r<=tree[2*i].r)//全部在左儿子
    updata(l,r,x,2*i);
  else if(tree[2*i+1].l<=l&&r<=tree[2*i+1].r)//全部在右儿子
    updata(l,r,x,2*i+1);
  else//脚踏2只船
                  

    {updata(l,tree[i].mid,x,2*i);
     updata(tree[i].mid+1,r,x,2*i+1);


    }

 tree[i].sum=tree[2*i].sum+tree[2*i+1].sum;//因为他的儿子的权值更新,所以他的权值也要更新

}



int main()
{ int t;
  cin>>t;
  int u=0;
  while(t--)
  {int n,q;
   scanf("%d%d",&n,&q);
    memset(yan,0,sizeof(yan));
    build(1,n,1);

   int x,y,z;
   for(int j=1;j<=q;j++)
        {scanf("%d%d%d",&x,&y,&z);
         updata(x,y,z,1);//从根节点开始找

        }

      cout<<"Case "<<++u<<": The total value of the hook is "<<tree[1].sum<<"."<<endl;
  }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值