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 ...
随机推荐
- MockServer的测试思想与实现
转载:http://blog.csdn.net/shen1936/article/details/50298901 背景 什么是MOCK Mock的定义 Mock框架简介 Mock在单测中的应用 De ...
- Angular 学习笔记——ng-Resource1
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...
- Gizmos 辅助线框
Gizmos are used to give visual debugging or setup aids in the scene view. Gizmos是用于在场景视图可视化调试或辅助设置. ...
- mybatis like写法
name like concat(concat('%',#{name}),'%') name like concat('%',#{name},'%')
- BufferedReader 使用 readLine() 读取 UTF-8 格式的文本第一行第一个字符是空字符的解决办法
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(ksmgVo.getFiledata( ...
- J2EE之字符编码输出
1. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcepti ...
- nginx 做前端代理时proxy参数配置
1.后台可登录: proxy_connect_timeout 300s; proxy_send_timeout ; proxy_read_timeout ; proxy_buffer_size 256 ...
- Convert to a source folder or rename it.
从GitHub上恢复之前的版本 eclipse 报错: Convert to a source folder or rename it. 网上找了答案 : 找到了这个 1. 删除gen文件. 2.选 ...
- Atitit.ioc 动态配置文件guice 设计原理
Atitit.ioc 动态配置文件guice 设计原理 1. Bat启动时注入配置文件1 2. ioc调用1 3. Ioc 分发器 配合 apche MethodUtils.invokeStatic ...
- servlet文件下载2(单文件下载和批量下载)
使用servlet完毕单文件下载和批量文件下载.批量下载的原理是先将文件打包成zip , 然后再下载. 之前也转载过一篇文件下载的博客,地址:http://blog.csdn.net/ch717828 ...