传送门: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. ACM2066

    题目原址:http://acm.hdu.edu.cn/showproblem.php?pid=2066 大神必须飘过,我在这个题目里面学到了太多太多了.我提交了十六次,错了十二次,反复了这么久才解决内 ...

  2. YAML schema reference

    YAML schema reference 10/30/2018 14 minutes to read Azure Pipelines Here's a detailed reference guid ...

  3. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  4. Eclipse中 将java Gradle项目转换为web项目

    1.找到项目工作空间目录,打开.project文件,并修改文件, 修改如下:      找到:<natures> </natures>代码段,在代码段中加入如下内容并保存:   ...

  5. Item 11 谨慎地覆盖Clone

    1.进行浅拷贝时,只是复制原始数据类型的值,则可以通过覆盖Clone方法来达到.另外,在进行浅拷贝的时候,还要注意,成员对象中不应该要有引用类型,如果有引用类型,那么,进行了浅拷贝之后,两个对象将会共 ...

  6. 不可思议的OOM

    本文发现了一类OOM(OutOfMemoryError),这类OOM的特点是崩溃时java堆内存和设备物理内存都充足,下文将带你探索并解释这类OOM抛出的原因. 关键词:  OutOfMemoryEr ...

  7. Intersecting Lines (计算几何基础+判断两直线的位置关系)

    题目链接:http://poj.org/problem?id=1269 题面: Description We all know that a pair of distinct points on a ...

  8. python初步学习-python数据类型-字典(dict)

    字典 字典类似于你通过联系人名字查找地址和联系人详细情况的地址簿,即,我们把键(名字)和值(详细情况)联系在一起.注意,键必须是唯一的,就像如果有两个人恰巧同名的话,你无法找到正确的信息. 注意,你只 ...

  9. MSSQL 基础知识001

    数据库概述: DBMS(Database Management System,数据库管理系统)和数据库. 平时谈到“数据库”可能有两种含义:MSSQLServer.Oracle等某种DBMS:存放一堆 ...

  10. ImageView设置边框 以及内部图片居中显示 在AndroidStudio中添加shape.xml文件

    效果如图 边框设置:shape文件 <shape xmlns:android="http://schemas.android.com/apk/res/android"> ...