HDU-1698-----Just Hook
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.
题解:
线段树区间修改求和
AC代码为:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1e5+10;
struct node{
int l,r,sum,tag;
} tree[maxn<<2];
void build(int k,int l,int r)
{
tree[k].l=l,tree[k].r=r;
tree[k].tag=0;
if(l==r)
{
tree[k].sum=1;
return ;
}
int mid=(l+r)>>1;
build(k<<1,l,mid);
build(k<<1|1,mid+1,r);
tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum;
}
void pushup(int k)
{
tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum;
}
void pushdown(int k)
{
tree[k<<1].tag=tree[k].tag;
tree[k<<1|1].tag=tree[k].tag;
tree[k<<1].sum=(tree[k<<1].r-tree[k<<1].l+1)*tree[k].tag;
tree[k<<1|1].sum=(tree[k<<1|1].r-tree[k<<1|1].l+1)*tree[k].tag;
tree[k].tag=0;
}
void update(int k,int l,int r,int x)
{
if(tree[k].l==l&&tree[k].r==r)
{
tree[k].sum=x*(tree[k].r-tree[k].l+1);
tree[k].tag=x;
return ;
}
if(tree[k].tag) pushdown(k);
int mid=(tree[k].r+tree[k].l)>>1;
if(r<=mid) update(k<<1,l,r,x);
else if(l>=mid+1) update(k<<1|1,l,r,x);
else update(k<<1,l,mid,x),update(k<<1|1,mid+1,r,x);
pushup(k);
}
int main()
{
int t,N,Q,x,y,z,cas=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&N,&Q);
build(1,1,N);
for(int i=1;i<=Q;i++)
{
scanf("%d%d%d",&x,&y,&z);
update(1,x,y,z);
}
printf("Case %d: The total value of the hook is %d.\n",cas++,tree[1].sum);
}
return 0;
}
HDU-1698-----Just Hook的更多相关文章
- HDU 1698 Just a Hook (线段树区间更新)
题目链接 题意 : 一个有n段长的金属棍,开始都涂上铜,分段涂成别的,金的值是3,银的值是2,铜的值是1,然后问你最后这n段总共的值是多少. 思路 : 线段树的区间更新.可以理解为线段树成段更新的模板 ...
- HDU 1698 just a hook - 带有lazy标记的线段树(用结构体实现)
2017-08-30 18:54:40 writer:pprp 可以跟上一篇博客做个对比, 这种实现不是很好理解,上一篇比较好理解,但是感觉有的地方不够严密 代码如下: /* @theme:segme ...
- HDU 1698——Just a Hook——————【线段树区间替换、区间求和】
Just a Hook Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- HDU 1698 Just a Hook(线段树区间替换)
题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...
- Just a Hook (HDU 1698) 懒惰标记
Just a Hook (HDU 1698) 题链 每一次都将一个区间整体进行修改,需要用到懒惰标记,懒惰标记的核心在于在查询前才更新,比如将当前点rt标记为col[rt],那么此点的左孩子和右孩子标 ...
- HDU 1698 【线段树,区间修改 + 维护区间和】
题目链接 HDU 1698 Problem Description: In the game of DotA, Pudge’s meat hook is actually the most horri ...
- HDU 1698 Just a Hook(线段树成段更新)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1698 题目: Problem Description In the game of DotA, P ...
- 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 ...
- HDU 1698 Just a Hook(线段树:区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 题意:给出1~n的数,每个数初始为1,每次改变[a,b]的值,最后求1~n的值之和. 思路: 区间更新题目 ...
- 【区间更新区间求和】HDU 1698 Just a Hook
acm.hdu.edu.cn/showproblem.php?pid=1698 [AC] #include<cstdio> ; #define lson (i<<1) #def ...
随机推荐
- JAVA项目打包成可运行的exe程序
前言:本篇文章为原创,转载请注明地址,谢谢. 我们一些时候,可能需要需要把我们完成的java打包,打成jar文件或者exe文件.这时候就请鄙人的这篇文章. 言尽于此,Let‘s go! 一.导出jar ...
- [剑指offer]第1题,二维数组中的查找
①题目 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. ...
- 力扣(LeetCode)买卖股票的最佳时机 个人题解
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股票. 示例 ...
- RHEL7.2 安装Hadoop-2.8.2
创建三台虚拟机,IP地址为:192.168.169.101,192.168.169.102,192.168.169.103 将192.168.169.102为namenode,192.168.169. ...
- opencv 4 图像处理(2 形态学滤波:腐蚀与膨胀,开运算、闭运算、形态学梯度、顶帽、黑帽)
腐蚀与膨胀 膨胀(求局部最大值)(dilate函数) #include <opencv2/core/core.hpp> #include <opencv2/highgui/highg ...
- centos 更换用户密码
腾讯云报告了我的服务器被暴力破解了.... 因此需要更换更复杂的password, 命令为:passwd 用户名,例如下我要更换root的password [root@VM_0_4_centos ~ ...
- 2019-9-20:渗透测试,基础学习,笔记,metasploit的基础使用
使用kali下metasploit生成木马,控制windows系统 kali基于debin的数字取证系统,上面集成了很多渗透测试工具,前身为bt r3(BrackTrack) Metasploit,是 ...
- 【Luogu P3387】缩点模板(强连通分量Tarjan&拓扑排序)
Luogu P3387 强连通分量的定义如下: 有向图强连通分量:在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶 ...
- Solr搜索引擎【索引提交、事务日志、原子更新】
一.索引提交 当一个文档被添加到Solr中,但没有提交给索引之前,这个文档是无法被搜索的.换句话说,从查询的角度看,文档直到提交之后才是可见的.Solr有两种类型的提交:软提交和正常提交[也称硬提交] ...
- 第9场 E-All men are brothers(并查集)
题目链接 题意:n个人,m次操作,每次操作使得两个人(x,y)成为朋友,朋友的关系是可以传递的,计算执行每次操作后,选择四个人两两都不是朋友的不同方案的数目. 数据范围:(n <= 100000 ...