Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18378    Accepted Submission(s): 9213

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.
 
Source
 
Recommend
wangye

线段树功能:update:成段替换 (由于只query一次总区间,所以可以直接输出1结点的信息)

 #include<cstdio>
#include<algorithm> #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 const int maxn=1e5+;
using namespace std; int sum[maxn<<],Lazy[maxn<<]; void PushUp(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
} void build(int l,int r,int rt)
{
int m;
Lazy[rt]=;
sum[rt]=; if(l==r) {
return ;
} m=(l+r)>>;
build(lson);
build(rson);
PushUp(rt);
} void PushDown(int rt,int m)
{
if(Lazy[rt]) {
Lazy[rt<<]=Lazy[rt<<|]=Lazy[rt];
sum[rt<<]=(m-(m>>))*Lazy[rt];
sum[rt<<|]=(m>>)*Lazy[rt];
Lazy[rt]=;
}
} void Updata(int L,int R,int c,int l,int r,int rt)
{
int m; if(L<=l && r<=R) {
Lazy[rt]=c;
sum[rt]=(r-l+)*c;
return;
} PushDown(rt,r-l+);
m=(l+r)>>;
if(L<=m) Updata(L,R,c,lson);
if(R>m) Updata(L,R,c,rson);
PushUp(rt);
} int main()
{
int cas,T,Q,x,y,z,n; scanf("%d",&T);
for(int cas=;cas<=T;cas++) {
scanf("%d",&n);
build(,n,);
scanf("%d",&Q);
while(Q--) {
scanf("%d%d%d",&x,&y,&z);
Updata(x,y,z,,n,); } printf("Case %d: The total value of the hook is %d.\n",cas,sum[]);
} return ;
}

[HDU] 1698 Just a Hook [线段树区间替换]的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. 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. JQuery的插件

    最近需要修改ftl文件,使用一般的freemarker插件不能有效处理里边的部分JQuery内容,所以特地下载了一个Spket插件, 地址为 http://www.agpad.com/update 再 ...

  2. Leetcode_ Best Time to Buy and Sell Stock II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  3. 桌面上嵌入窗口(桌面日历)原理探索(将该窗口的Owner设置成桌面的Shell 窗口,可使用SetWindowLong更改窗口的GWL_HWNDPARENT,还要使用SetWindowPos设置Z-Order)

    今天在QQ群里有人问怎样实现将自己的窗口嵌入桌面,让它和桌面融为一体,就像很多桌面日历软件那样. 我当时想到的就是建立一个Child  Window,将他的父窗口设置成桌面Shell窗口就可以了.但是 ...

  4. Cmake 脚本对项目输出路径和输出头文件的路径定义

    对Lib项目的统一输出路径以下时解决方案: set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/Lib)set(CMAKE_LIBRARY_O ...

  5. 开始使用Logstash

    开始使用Logstash 本节将指导处理安装Logstash 和确认一切是运行正常的, 后来的章节处理增加负载的配置来处理选择的使用案例. 这个章节包含下面的主题: Installing Logsta ...

  6. bzoj1756 Vijos1083 小白逛公园

    Description 小新经常陪小白去公园玩,也就是所谓的遛狗啦-在小新家附近有一条"公园路",路的一边从南到北依次排着n个公园,小白早就看花了眼,自己也不清楚该去哪些公园玩了. ...

  7. HTTP请求的TCP瓶颈分析

    这篇文章基本是对<Web性能权威指南>第一章和第二章的读书笔记,另外加一些扩展内容,这本书确实赞,推荐 针对三次握手.流量控制(接收窗口).慢启动(cwnd,拥塞窗口).队首阻塞等方面看下 ...

  8. c#、vb 自动属性

    vb示例: Public Property Name() As String = "Bob" 等效于 Private _name As String = "Bob&quo ...

  9. Localdb Attach Problem

    在进行code first的迁移时,update-database后默认在App_data文件夹下会新建数据库,如果删除了在使用update-database命令会出现以下错误: Cannot att ...

  10. (转).net程序员转战android第二篇---牛刀小试

    上篇说道如何搭建android的开发环境,这一篇我们将牛刀小试一下, 完成我们第一个android APP应用. 我就从新建项目说起吧. 首先打开Eclipse,选择顶部的File(文件)——new( ...