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;
    }
     
    宝剑锋从磨砺出 梅花香自苦寒来
  • 相关阅读:
    C++细节3
    C++细节2
    C++细节1
    连通域标记方法
    dll动态链接库入门2
    UnixShell编程(第三版)
    Xcode 快捷键
    mysql在linux上的一点操作
    mysql 语句
    开机自动启动
  • 原文地址:https://www.cnblogs.com/GHzcx/p/8921575.html
Copyright © 2011-2022 走看看