Just a Hook(线段树)
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 29310 Accepted Submission(s): 14492
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
Sample Output
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <bitset>
# include <sstream>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define MX 100005
struct Node
{
int l,r;
int lazy,sum;
}tree[MX*]; int n, q; void build(int l,int r,int k)
{
tree[k]=(Node){l,r,,};
if (l==r)
{
tree[k].sum = ;
return;
}
int mid = (l+r)>>;
build(l,mid,*k); build(mid+,r,*k+);
tree[k].sum = tree[*k].sum + tree[*k+].sum;
} void push_down(int k)
{
int l=tree[k].l, r=tree[k].r;
int mid = (l+r)>>, z = tree[k].lazy;
if (l==r||z==) return;
tree[*k].lazy=z; tree[*k].sum=z*(mid-l+);
tree[*k+].lazy=z; tree[*k+].sum=z*(r-mid);
tree[k].lazy=;
} void update(int l,int r,int k,int v)
{
if (l==tree[k].l&&r==tree[k].r)
{
tree[k].lazy=v;
tree[k].sum=v*(r-l+);
return;
}
push_down(k);
int mid = (tree[k].l+tree[k].r)>>;
if (r<=mid) update(l,r,*k,v);
else if (l>mid) update(l,r,*k+,v);
else update(l,mid,*k,v), update(mid+,r,*k+,v);
tree[k].sum = tree[*k].sum+tree[*k+].sum;
} int inqy(int l, int r, int k)
{
if (l==tree[k].l&&r==tree[k].r)
{
return tree[k].sum;
}
push_down(k);
int mid = (tree[k].l+tree[k].r)>>;
if (r<=mid) return inqy(l,r,*k);
else if (l>mid) return inqy(l,r,*k+);
return inqy(l,mid,*k) + inqy(mid+,r,*k+);
} int main()
{
int t;
scanf("%d",&t);
for (int cas=;cas<=t;cas++)
{
scanf("%d%d",&n,&q);
build(,n,);
while(q--)
{
int l, r, v;
scanf("%d%d%d",&l,&r,&v);
update(l,r,,v);
}
printf("Case %d: The total value of the hook is %d.\n",cas,inqy(,n,));
}
return ;
}
Just a Hook(线段树)的更多相关文章
- hdu_1698Just a Hook(线段树)
hdu_1698Just a Hook(线段树) 标签: 线段树 题目链接 题意: 一个英雄的技能是发射一个长度为n的金属链,初始的金属链都是铁做的,标记为1,我们可以对于某个区间修改它的金属材质,如 ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- HDU-1698 JUST A HOOK 线段树
最近刚学线段树,做了些经典题目来练手 Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- HDU 1698 Just a Hook(线段树成段更新)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 1698 Just a Hook (线段树 成段更新 lazy-tag思想)
题目链接 题意: n个挂钩,q次询问,每个挂钩可能的值为1 2 3, 初始值为1,每次询问 把从x到Y区间内的值改变为z.求最后的总的值. 分析:用val记录这一个区间的值,val == -1表示这 ...
- [HDU] 1698 Just a Hook [线段树区间替换]
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu1698 Just a Hook 线段树:成段替换,总区间求和
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 Problem ...
- HDU1698_Just a Hook(线段树/成段更新)
解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
随机推荐
- 在linux下玩转usb摄像头
硬件平台:PC机一台 .usb摄像头 操作系统:Linux3.0.8 交叉编译环境:arm-none-Linux-gnueabi-gcc 4.5.1 调试步骤: 一.linux 内核解压 1.1使用 ...
- python良好的编程习惯
良好的编程习惯 2.1 在程序中是用丰富的注释,注释有助于其他程序员理解程序,有助于程序调试(发现和排除程序中的错误),并列出有用的信息.以后修改或更新代码时,注释还有助于理解当初自己编写的程序 2. ...
- 新装系统(CentOS7.4)环境初始化配置笔记
新装系统(CentOS7.4)环境初始化配置笔记 一.概述 设备详情: Dell R730 服务器 (四个网卡,一根网线插在第2个网卡上) CentOS 7.4 x64 最小安装环境 二.网络环境配置 ...
- zabbix监控php-fpm
1.启用php-fpm的状态功能 [root@web01 ~]# vim /etc/php-fpm.d/www.conf 121 pm.status_path = /php_status [root@ ...
- HTTP请求和响应2:方法(Method)
方法表明了client希望server对资源运行的动作.经常使用的方法包含:GET.HEAD.POST.PUT.TRACE.OPTIONS和DELETE,每一个server能够实现这些方法中的部分或者 ...
- Laravel之HTTP相应
一.基本相应示例 1.返回简单字符串 Route::get('/', function () { return 'Hello World'; }); 给定的字符串会被框架自动转化为 HTTP 响应 2 ...
- Java-帮助文档的制作
Java-帮助文档的制作 1,public修饰的类才干够用bin/javadoc生成文档 2.java的说明书是通过文档的凝视来完毕的,所以在敲代码的时候.凝视是非常有必要的 使用文档凝视法,才干够生 ...
- &&与&符号区别
http://topic.csdn.net/u/20080915/16/f5125300-f69f-4da8-9c3a-a7458590553f.html && 与 &区别: ...
- JAVA静态导入(inport static)详解
在Java 5中,import语句得到了增强,以便提供甚至更加强大的减少击键次数功能,虽然一些人争议说这是以可读性为代价的.这种新的特性成为静态导入. 当你想使用static成员时,可以使用静态导入( ...
- android开发游记:meterial design 5.0 开源控件整套合集 及使用demo
android 的5.0公布不光google官方给出了一些新控件,同一时候还给出了一套符合material design风格的设计标准,这套标准将未来将覆盖google全部产品包括pc端,站点,移动端 ...