HDU 1698 Just a Hook (线段树模板题-区间求和)
Just a Hook
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.
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.
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio> using namespace std;
const int maxn = ;
int sum[maxn<<],add[maxn<<];
int n,q,T;
void pushup(int rt){
sum[rt] = sum[rt<<]+sum[rt<<|];
}
void pushdown(int rt, int ln, int rn)
{
if(add[rt])
{
sum[rt << ] = ln * add[rt];
sum[rt << | ] = rn * add[rt];
add[rt << ] = add[rt];
add[rt << | ] = add[rt];
add[rt] = ;
}
return;
}
void build(int l,int r,int rt){
add[rt] = ;
if(l==r){
sum[rt] = ;
return ;
}
int mid = (l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
pushup(rt);
}
void update(int l,int r,int rt,int L,int R,int C)
{
if(L<=l&&r<=R){
sum[rt] = (r-l+)*C;
add[rt] = C;
return ;
}
int mid = (l+r)>>;
pushdown(rt,mid-l+,r-mid);
if(L<=mid)update(l,mid,rt<<,L,R,C);
if(R>mid)update(mid+,r,rt<<|,L,R,C);
pushup(rt);
}
int query(int l,int r,int rt,int L,int R)
{
if(L<=l&&r<=R)return sum[rt];
int mid = (l+r)>>;
pushdown(rt,mid-l+,r-mid);
int ans = ;
if(L<=mid) ans += query(l,mid,rt<<,L,R);
if(R>mid) ans += query(mid+,r,rt<<|,L,R);
return ans;
}
int main()
{
scanf("%d",&T);
for(int t=;t<=T;t++){
scanf("%d",&n);
build(,n,);
scanf("%d",&q);
int l,r,x;
while(q--){
scanf("%d%d%d",&l,&r,&x);
update(,n,,l,r,x);
}
printf("Case %d: The total value of the hook is %d.\n",t,query(,n,,,n));
}
return ;
}
HDU 1698 Just a Hook (线段树模板题-区间求和)的更多相关文章
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- 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(线段树 区间替换)
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 (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 ...
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
- 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 ...
- 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 ...
随机推荐
- PHP学习(运算符)
PHP运算符一般分为算术运算符.赋值运算符.比较运算符.三元运算符.逻辑运算符.字符串连接运算符.错误控制运算符. 算术运算符 主要是用于进行算术运算的,例如:加法运算.减法运算.乘法运算.除法运算 ...
- 【软件安装】我喜欢的notepad插件
1.文件管理器 explorer 2.16进制查看文件工具 HEX-Editor
- Promise https://www.liaoxuefeng.com/wiki/1022910821149312/1023024413276544
在JavaScript的世界中,所有代码都是单线程执行的. 由于这个“缺陷”,导致JavaScript的所有网络操作,浏览器事件,都必须是异步执行.异步执行可以用回调函数实现: function ca ...
- 系统重装后,Mysql数据库重装加载原来数据库
相信不只我一个人因为重新装了系统后,导致mysql数据库无法使用的问题.尽管可以重新安装一个mysql服务端程序在自己的电脑上,但是要如何才能够将之前的数据库也一并重新恢复呢? 今天,我找到了解决之道 ...
- spring boot 2.X上传文件限制大小
Spring Boot 1.3.x multipart.maxFileSize multipart.maxRequestSize Spring Boot 1.4.x and 1.5.x spring. ...
- Quick BI独创千人千面的行级权限管控机制
摘要 就数据访问权限而言,阿里巴巴以“被动式授权”为主,你需要什么权限就申请什么权限.但是,在客户交流过程中,我们发现绝大多数企业都是集中式授权,尤其是面向个人的行级权限管控,管理复杂度往往呈几何增长 ...
- Oracle使用——PLSQL查询表结构并导出EXCEL
背景 有一次需要查询Oracle数据库中的所有表接口并且导出excel,方法记录如下 使用 使用PLSQL工具查询表结构,SQL语句如下 SELECT B.TABLE_NAME AS '表名', C. ...
- WPF动画之后属性值无法改变
原文:WPF动画之后属性值无法改变 前一段时间使用WPF写2048游戏的时候,遇到下面的情形:使用按键对色块进行移动时,触发位置左边X和Y属性的DoubleAnimation动画,但是 ...
- 13 -3 jquery选择器和 jquery动画
一 选择器: 1 基本选择器 例子: <!--id 类 标签--> <!DOCTYPE html> <html lang="en"> <h ...
- MySQL5.7默认打开ONLY_FULL_GROUP_BY模式问题与解决方案
MySQL5.7后将sql_mode的ONLY_FULL_GROUP_BY模式默认设置为打开状态,这样一来,很多之前的sql语句可能会出现错误,错误信息如下: Error Code: 1055. Ex ...