Just a Hook

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.

InputThe 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. 
OutputFor 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.
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<bitset>
#include<stack>
using namespace std;
typedef long long LL;
#define MAXN 100009
#define N 25
#define INF 0x3f3f3f3f /*
线段树 区间更新!
*/
struct node
{
int l, r, data, laz;
int sum;
}T[MAXN*];
void Build(int p, int l, int r)
{
T[p].l = l, T[p].r= r, T[p].data = , T[p].laz = ;
T[p].sum = ;
if (l == r)
{
T[p].sum = ;
return;
}
int mid = (l + r) / ;
Build(p << , l, mid);
Build((p << ) | , mid + , r);
T[p].sum = T[p << ].sum + T[(p << ) | ].sum;
}
void update(int p, int l, int r, int v)
{
int mid = (T[p].l + T[p].r) / ;
if (T[p].l >= l&&T[p].r <= r)
{
T[p].data = v;
T[p].laz = ;//这一块被成块更新过
T[p].sum = (r - l + )*v;
return;
}
if (T[p].laz)//如果被更新过,那么说明这一块内的颜色会有多种
{
T[p].laz = ;
update(p << , T[p].l, mid, T[p].data);
update((p << ) | , mid + , T[p].r, T[p].data);
T[p].data = ;
}
if (r <= mid)
update(p << , l, r, v);
else if (l > mid)
update((p << ) | , l, r, v);
else
{
update(p << , l, mid, v);
update((p << ) | , mid + , r, v);
}
T[p].sum = T[p << ].sum + T[(p << ) | ].sum;
} int main()
{
int t, n, l, r, v, q;
int cas = ;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &q);
Build(, , n);
while (q--)
{
scanf("%d%d%d", &l, &r, &v);
update(, l, r, v);
}
printf("Case %d: The total value of the hook is %d.\n", cas++, T[].sum);
}
}

Just a Hook 线段树 区间更新的更多相关文章

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

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

  2. 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 ...

  3. 【原创】hdu1698 Just a Hook(线段树→区间更新,区间查询)

    学习线段树第二天,这道题属于第二简单的线段树,第一简单是单点更新,这个属于区间更新. 区间更新就是lazy思想,我来按照自己浅薄的理解谈谈lazy思想: 就是在数据结构中,树形结构可以线性存储(线性表 ...

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

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

  5. hdu1698 Just a hook 线段树区间更新

    题解: 和hdu1166敌兵布阵不同的是 这道题需要区间更新(成段更新). 单点更新不用说了比较简单,区间更新的话,如果每次都更新到底的话,有点费时间. 这里就体现了线段树的另一个重要思想:延迟标记. ...

  6. HDU1698:Just a Hook(线段树区间更新)

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

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

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

  8. hdu1698 Just a Hook (线段树区间更新 懒惰标记)

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

  9. hdu-------(1698)Just a Hook(线段树区间更新)

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

随机推荐

  1. webservice 权限控制

    webservice 如何限制访问,权限控制?1.服务器端总是要input消息必须携带用户名.密码信息 如果不用cxf框架,SOAP消息(xml片段)的生成.解析都是有程序员负责 2.拦截器 为了让程 ...

  2. N - Binomial Showdown (组合数学)

    Description In how many ways can you choose k elements out of n elements, not taking order into acco ...

  3. CSS之浮动元素

    <html> <head> <meta charset="utf-8"> <title></title> <sty ...

  4. Disruptor源码解读

    上一篇已经介绍了Disruptor是什么?简单总结了为什么这么快?下面我们直接源码搞起来,简单粗暴.高性能队列disruptor为什么这么快? 一.核心类接口 Disruptor 提供了对RingBu ...

  5. 重新学习Java——对象和类(一)

    之前通过记笔记的方法,对于<Java核心技术>这本书的前一章进行了重新的复习,感觉效果很好,比单独看书带来了更好的复习效果,了解了很多以前不是很注意的一些细节,但是在一些自己较为熟悉的地方 ...

  6. Android 网络图片查看器与网页源码查看器

    在AndroidManifest.xml里面先添加访问网络的权限: <uses-permission android:name="android.permission.INTERNET ...

  7. json-server模拟REST API

    介绍 可以开启一个服务前端使用 安装 npm i json-server -g 使用 $ json-server db.json -p 3000 // 在文件目录下执行上述命令,开放一个端口3000的 ...

  8. Python+selenium学习(一) 打开Firefox浏览器,IE浏览器和Chrome浏览器

    from selenium import webdriver # open Firefox #driver=webdriver.Firefox() # Open IE #driver=webdrive ...

  9. 并发编程学习笔记(10)----并发工具类CyclicBarrier、Semaphore和Exchanger类的使用和原理

    在jdk中,为并发编程提供了CyclicBarrier(栅栏),CountDownLatch(闭锁),Semaphore(信号量),Exchanger(数据交换)等工具类,我们在前面的学习中已经学习并 ...

  10. os下,vs code 自动编译ts

    nodejs安装ts npm install -g typescript 进入工程目录,用命令初始化ts(生成tsconfig.json) tsc --init 如果要指定生成目录,打开tsconfi ...