zoukankan      html  css  js  c++  java
  • HDU 1689 Just a Hook (线段树区间更新+求和)

    Just a Hook

    Problem Description

    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.

    Input

    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.

    Output

    For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.

    Sample Input

    输入样例:

    1 10 2 1 5 2 5 9 3
    

    Sample Output

    Case 1: The total value of the hook is 24.
    

    思路

    初始所有节点值为1 , 更新[l,r]为2 或 3,再求区间和,涉及懒标记,直接套模板= =

    代码

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn =  1e5+10  ;
    const int inf =	0x3f3f3f3f;
    
    struct node{
        int l,r;
        int add;
        int sum;
    }tree[maxn<<2];
    
    int kase=0;
    int n,m,t;
    int a,b,c;
    int val = 1;
    int ans = 0;
    void pushup(int k)
    {
        tree[k].sum = tree[k<<1].sum+tree[k<<1|1].sum;
    }
    void pushdown(int k)
    {
        if(tree[k].add)
        {
            tree[k<<1].sum = (tree[k<<1].r-tree[k<<1].l+1)*tree[k].add;
            tree[k<<1|1].sum = (tree[k<<1|1].r-tree[k<<1|1].l+1)*tree[k].add;
    
            tree[k<<1].add = tree[k].add;
            tree[k<<1|1].add = tree[k].add;
    
            tree[k].add = 0;
        }
    }
    void build(int l,int r,int k)
    {
        tree[k].l = l;  tree[k].r = r;  tree[k].add = 0;//刚开始一定要清0
        if(l == r){  tree[k].sum=1;  return ;  }
        int mid = (l+r)>>1;
        build(l,mid,k<<1);
        build(mid+1,r,k<<1|1);
        pushup(k);
    }
    void updata(int k)
    {
        if(a <= tree[k].l && b >= tree[k].r)
        {
            tree[k].sum = (tree[k].r-tree[k].l+1)*val;
            tree[k].add = val;
            return ;
        }
        pushdown(k);
        int mid = (tree[k].l+tree[k].r)>>1;
        if(a<=mid){  updata(k<<1); }
        if(b>mid){  updata(k<<1|1);  }
        pushup(k);
    }
    void query(int k)
    {
        if(a <= tree[k].l && b >= tree[k].r)
        {
          ans +=tree[k].sum ;
          return ;
        }
        pushdown(k);
        int mid = (tree[k].l+tree[k].r)>>1;
        if(a <= mid){ query(k<<1);}
        if(b > mid){ query(k<<1|1);}
    }
    int main()
    {
      scanf("%d",&t);
      while(t--)
      {
        scanf("%d",&n);
        scanf("%d",&m);
        build(1,n,1);
        while(m--)
        {
          scanf("%d%d%d",&a,&b,&val);
          updata(1);
        }
        ans=0;a=1;b=n;//这里需初始化
        query(1);
        printf("Case %d: The total value of the hook is %d.
    ",++kase,ans);
      }
    }
    
  • 相关阅读:
    Vue 之 mixin的用法
    react 初学之 jsx ,prop。state
    前端清除缓存的集中方法
    关于event被废弃后的新实用方法
    JS获取字符串的字节长度
    getPopupContainer解决ant-design-vue select组件下拉框偏移错位
    frameset标签使用
    浏览器内核
    两种多关键字排序代码
    两种多关键字排序策略比较
  • 原文地址:https://www.cnblogs.com/llke/p/10780588.html
Copyright © 2011-2022 走看看