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 ...
随机推荐
- HTML5的测试总结
HTML5其实也是web的一种,所以基本的web测试的一些重点,HTML5上都要过一遍,不过也有其特殊之处. [需求设计测试] 需求是否合理.是否有更好的实现方法或者功能的遗漏,以及原型图测试,包括用 ...
- 【Hadoop】Flink VS Spark?Drill VS Presto?
参考资料: drill 官网:http://drill.apache.org/ drill安装使用:https://segmentfault.com/a/1190000002652348 drill简 ...
- 【Hive】Hive 安装&使用基础
2 安装 2.1 参考 2.1.1 下载 2.1.1.1 https://mirrors.tuna.tsinghua.edu.cn/apache/hive/stable-2/ 2.1.2 安装指导 2 ...
- How to Clear setInterval() without Knowing the ID
ProblemDeclaring a setInterval() without keeping a reference to it (which is returned from the funct ...
- iOS小技巧 - 为按钮设置不同状态下的背景色
我们知道直接在Storyboard中设置按钮的背景色是不能根据不同状态来更改的,那问题来了,如果我们需要在不同的状态下(比如按钮没有被按下或者被按下),使得按钮呈现不同的背景色怎么办? 比如上图左边是 ...
- URL相对路径和URL绝对路径
经常在页面中引用图片,html页面等,自己常常弄错相对路径和绝对路径,今天写下此文总结一下. 直接举例说明吧. 在 D:\例子\html下有这么几个文件和文件夹 1.若引用的资源和本身在 ...
- 怎样在QML应用中调用系统设置中的页面来设置我们的系统
我们在QML应用中有时须要调用系统设置(system settings)来完毕我们的一些设置.比方,我们在使用GPS来定位时,可能GPS并没有打开,假设在我们的应用中直接打开系统中的GPS设置页面,这 ...
- 初学css个人笔记
1.css类选择器中的类名的第一个字符不能是数字,无法再Mozilla或Firefox中起作用. 2.css中id选择器中的属性只能在每个html文档中出现一次. 3.css样式表中不需要在属性值与单 ...
- matplotlib表面三维图
1.basic numpy.meshgrid 由一维数组到二维数组,用于生成网格数据 matplotlib python绘图库 2.code In [88]: from mpl_toolkits.mp ...
- shader之旅-7-平面阴影(planar shadow)
根据<real-time shadow>这本书第二章中的推导,实现了最简单的阴影技术. planar shadow通过一个投影矩阵将被灯光照射的物体的顶点沿着光线方向投影到接受阴影的平面. ...