zoukankan      html  css  js  c++  java
  • hdu 1698 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
    The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
    For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
    Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
     
    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.

    题意:

    输入一个n表示一段长度为n的区间,有n个编号为1~n的点,初始值全部为1。 有q个操作, 每个操作有3个数:l,r,v表示将区间l~r的所有元素修改为v。

    题解:

    因为修改很多值, 如果还是按照原来的更新方法, 每个结点更新一次的话,速度实在太慢。 一个点一个点的更新之所以慢 , 是因为每个被该点影响的点我们都需要更新。   为了能”顺便“更新, 我们在每个结点上多维护一个信息, 表示上次该区间修改的值是多少,然后然后每次向下更新之前将标记更新到儿子结点。

    代码:

    #include <stdio.h>
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    #include <deque>
    #include <iomanip>
    #include <iostream>
    #include <list>
    #include <map>
    #include <queue>
    #include <set>
    #include <utility>
    #include <vector>
    #define mem(arr, num) memset(arr, 0, sizeof(arr))
    #define _for(i, a, b) for (int i = a; i <= b; i++)
    #define __for(i, a, b) for (int i = a; i >= b; i--)
    #define IO                     
      ios::sync_with_stdio(false); 
      cin.tie(0);                  
      cout.tie(0);
    using namespace std;
    typedef long long ll;
    const ll inf = 0x3f3f3f3f;
    const double EPS = 1e-10;
    const ll mod = 1000000007LL;
    const int N = 1 << 19;
    ll dat[N],mark[N];
    void build(int l, int r, int k) {
      dat[k] = mark[k] = 0;
      if(l==r) {
        dat[k] = 1; mark[k] = 0; return ;
      }
      build(l, (l+r)/2, k<<1);
      build((l+r)/2+1, r, k<<1|1);
      dat[k] = dat[k<<1] + dat[k<<1|1];
    }
    void down(int k,int len) {
      if(mark[k]) {
        dat[k<<1] = mark[k] * (len - len/2);
        dat[k<<1|1] = mark[k] *(len/2);
        mark[k<<1] = mark[k<<1|1] = mark[k];
        mark[k] = 0;
      }
    }
    void update(int a, int b, int k, int l, int r, int c) {
      if(a <= l && b >= r) {
        dat[k] = c * (r-l+1);
        mark[k] = c;
      }
      else if(a <= r && b >= l){
        down(k,r-l+1);
        update(a,b,k<<1,l,(l+r)/2,c);
        update(a,b,k<<1|1,(l+r)/2+1,r,c);
        dat[k] = dat[k<<1] + dat[k<<1|1];
      }
    }
    int main() {
      int T,n,q;
      scanf("%d",&T);
      _for(k, 1, T) {
        scanf("%d%d",&n,&q);
        build(1,n,1);
        int a,b,c;
        _for(i, 1, q) {
          scanf("%d%d%d",&a,&b,&c);
          update(a,b,1,1,n,c);
        }
        printf("Case %d: The total value of the hook is %lld.
    ",k,dat[1]);
      }
      return 0;
    }
     
    宝剑锋从磨砺出 梅花香自苦寒来
  • 相关阅读:
    谷歌Google Chrome浏览器打开新的标签页设置指定固定网址
    Vue子组件和父组件、子组件调用父组件的方法、父组件调用子组件方法、子组件与父组件间的传值
    查询Linux服务器出口IP、curl命令查询Linux公网出口IP、Windows服务器查询出口IP
    mysql查询是对字段进行补0操作,可用于树状结构整体排序
    mysql批量update更新,mybatis中批量更新操作
    CentOS 6.8下网卡配置、桥接模式和NAT连接模式、VMware虚拟机克隆网卡配置
    杂七杂八
    解决SpringMVC拦截器中Request数据只能读取一次的问题
    Redis安装教程及可视化工具RedisDesktopManager下载安装
    JAVA获取客户端请求的当前网络ip地址(附:Nginx反向代理后获取客户端请求的真实IP)
  • 原文地址:https://www.cnblogs.com/GHzcx/p/8921575.html
Copyright © 2011-2022 走看看