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 ...
随机推荐
- 【JZOJ4922】【NOIP2017提高组模拟12.17】环
题目描述 小A有一个环,环上有n个正整数.他有特殊的能力,能将环切成k段,每段包含一个或者多个数字.对于一个切分方案,小A将以如下方式计算优美程度: 首先对于每一段,求出他们的数字和.然后对于每段的和 ...
- Android LRUCache简介
LRU Cache数据结构的介绍可以参考前面的http://www.cnblogs.com/XP-Lee/p/3441555.html. 本文以Android LRUCache来做一个简单的介绍.我们 ...
- DHCP服务器安装、测试
df:disk free df -h 查询空余磁盘 find / -name TechSungWeiXin 查询TechSungWeiXin的位置 find / -name YunyueWeixin_ ...
- oracle 数据库安全审计
Oracle的审计机制是用来监视用户对ORACLE数据库所做的各种操作. 在缺省情况下,系统的审计功能是关闭的.可以在INIT.ORA参数文件中将参数AUDIT_TRAIL设置为正整数来激活. 审计功 ...
- oracle限制一个用户空闲时间
alter system set resource_limit = true; create profile idletime limit idle_time 3; alter user outln ...
- oracle怎么捕获用户登录信息,如SID,IP地址等
可以利用登录触发器,如 CREATE OR REPLACE TRIGGER tr_login_record AFTER logon ON DATABASE DECLARE miUserSid NUMB ...
- (译)Objective-C的动态特性
这是一篇译文,原文在此,上一篇文章就是受这篇文章启发,这次干脆都翻译过来. 过去的几年中涌现了大量的Objective-C开发者.有些是从动态语言转过来的,比如Ruby或Python,有些是从强类型 ...
- oracle中常用的时间格式转换
1:取得当前日期是本月的第几周 select to_char(sysdate,'YYYYMMDD W HH24:MI:SS') from dual; TO_CHAR(SYSDATE,'YY') se ...
- sql查询报java.sql.SQLException: Column 'LC_ID' not found 的错误实际上是mysql在hibernate别名的问题
报java.sql.SQLException: Column 'LC_ID' not found 的错误实际上是mysql在hibernate别名的问题 我的查询sql是 String sql2 =& ...
- HZOJ 那一天她离我而去
一个数据水到不行的题,各路大佬用各种方法A掉了这个题(比如A*,最短路,dfs……). 这里只说一下我的暴力和被碾压的正解. 暴力AC系列: 要找过1点的最小环,那么这个环可以拆成两部分,与1相连的两 ...