传送门: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();                  \
  cout.tie();
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f;
;
const ll mod = 1000000007LL;
 << ;
ll dat[N],mark[N];
void build(int l, int r, int k) {
  dat[k] = mark[k] = ;
  if(l==r) {
    dat[k] = ; mark[k] = ; return ;
  }
  build(l, (l+r)/, k<<);
  build((l+r)/+, r, k<<|);
  dat[k] = dat[k<<] + dat[k<<|];
}
void down(int k,int len) {
  if(mark[k]) {
    dat[k<<] = mark[k] * (len - len/);
    dat[k<<|] = mark[k] *(len/);
    mark[k<<] = mark[k<<|] = mark[k];
    mark[k] = ;
  }
}
void update(int a, int b, int k, int l, int r, int c) {
  if(a <= l && b >= r) {
    dat[k] = c * (r-l+);
    mark[k] = c;
  }
  else if(a <= r && b >= l){
    down(k,r-l+);
    update(a,b,k<<,l,(l+r)/,c);
    update(a,b,k<<|,(l+r)/+,r,c);
    dat[k] = dat[k<<] + dat[k<<|];
  }
}
int main() {
  int T,n,q;
  scanf("%d",&T);
  _for(k, , T) {
    scanf("%d%d",&n,&q);
    build(,n,);
    int a,b,c;
    _for(i, , q) {
      scanf("%d%d%d",&a,&b,&c);
      update(a,b,,,n,c);
    }
    printf(]);
  }
  ;
}
 

hdu 1698 Just a Hook(线段树区间修改)的更多相关文章

  1. HDU 1698 Just a Hook(线段树 区间替换)

    Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...

  2. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

  3. HDU 1698 Just a Hook(线段树区间更新查询)

    描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...

  4. [HDU] 1698 Just a Hook [线段树区间替换]

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. HDU 1698 Just a Hook(线段树区间替换)

    题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...

  6. HDU 1698 Just a Hook 线段树区间更新、

    来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...

  7. HDU.1689 Just a Hook (线段树 区间替换 区间总和)

    HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...

  8. HDU 1698 just a hook 线段树,区间定值,求和

    Just a Hook Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1 ...

  9. HDU 1698 Just a Hook 线段树+lazy-target 区间刷新

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  10. hdu - 1689 Just a Hook (线段树区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...

随机推荐

  1. 使用 log4j 在控制台 打印 hibernate 语句参数

    log4j.rootLogger=INFO, stdoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.st ...

  2. poj 2541 Binary Witch

    Binary Witch http://poj.org/problem?id=2541 Time Limit: 1000MS   Memory Limit: 65536K       Descript ...

  3. 【C++ STL】Stack

    1.定义 class stack<> 实作出一个stack(也成为LIFO,后进先出),你可以使用push()将任意数量的元素置入stack中,也可以使用pop()将元素依次插入次序反序从 ...

  4. linux sh脚本异常:/bin/sh^M:bad interpreter: No such file or directory

    在Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory.这是不同系统编码格式引起的:在windows系统中编辑的. ...

  5. c# 设置和取消文件夹共享及执行Dos命令

    /// <summary> /// 设置文件夹共享 /// </summary> /// <param name="FolderPath">文件 ...

  6. 【BZOJ】1574: [Usaco2009 Jan]地震损坏Damage

    [算法]搜索 [题意]给定无向图,现在可能有一些点已经被删除,只给出信息是c个点不能到达结点1,求最少的不能到达结点1的个数(含已删除点). [题解] 真是一道奥妙重重的题目. 每个点不能到达结点1, ...

  7. this指针再解

     this.new.call和apply的相关问题 讲解this指针的原理是个很复杂的问题,如果我们从javascript里this的实现机制来说明this,很多朋友可能会越来越糊涂,因此本篇打算换一 ...

  8. perl 列出一个目录下的文件的大小

    use strict; use warnings; use Cwd; my $dir = 'd:\\www'; chdir($dir); opendir DIR, $dir or die " ...

  9. 动态替换Linux核心函数的原理和实现

    转载:https://www.ibm.com/developerworks/cn/linux/l-knldebug/ 动态替换Linux核心函数的原理和实现 在调试Linux核心模块时,有时需要能够实 ...

  10. android CVE 漏洞汇总

    arm exploits 技术教程: Learning Pentesting for Android Devices CVE-2015-1530 ,CVE-2015-1474 两个android整数溢 ...