1698-Just a Hook 线段树(区间替换)
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 32141 Accepted Submission(s):
15804
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.
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.
the total value of the hook after the operations. Use the format in the
example.
#include<cstdio>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int MAXN = ;
int sum[MAXN<<];
int col[MAXN<<];
int t,n,q;
void putup(int rt)
{
sum[rt] = sum[rt<<]+sum[rt<<|];
}
void putdown(int rt,int m)
{
if (col[rt])
{
col[rt<<] = col[rt<<|] = col[rt];
sum[rt<<] = (m-(m>>))*col[rt];
sum[rt<<|] = (m>>)*col[rt];
col[rt] = ;
}
}
void build(int l,int r,int rt)
{
col[rt] = ;
sum[rt] = ;
if (l==r) return ;
int m = (l+r)>>;
build(lson);
build(rson);
putup(rt);
}
void update(int l,int r,int rt,int L,int R,int c)
{
if (L<=l && r<=R)
{
col[rt] = c;
sum[rt] = c*(r-l+);
return ;
}
putdown(rt,r-l+);
int m = (l+r)>>;
if (L<=m) update(lson,L,R,c);
if (R>m) update(rson,L,R,c);
putup(rt);
}
int main()
{
scanf("%d",&t);
for (int i=; i<=t; ++i)
{
scanf("%d%d",&n,&q);
build(,n,);
while (q--)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
update(,n,,x,y,z);
}
printf("Case %d: The total value of the hook is %d.\n",i,sum[]);
}
return ;
}
1698-Just a Hook 线段树(区间替换)的更多相关文章
- 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)Total ...
- HDU 1698 Just a Hook(线段树区间替换)
题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- (简单) 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(线段树区间更新查询)
描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...
- HDU 1698 Just a Hook 线段树区间更新、
来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...
- hdu1698(线段树区间替换模板)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1698 题意: 第一行输入 t 表 t 组测试数据, 对于每组测试数据, 第一行输入一个 n , 表示 ...
- poj2528(线段树区间替换&离散化)
题目链接: http://poj.org/problem?id=2528 题意: 第一行输入一个 t 表 t 组输入, 对于每组输入: 第一行 n 表接下来有 n 行形如 l, r 的输入, 表在区 ...
- Just a Hook 线段树 区间更新
Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...
随机推荐
- selenium使用js进行点击
WebElement button = driver.findElement(By.xpath("/html/body/div[1]/div[3]/h2/div[2]")); Ja ...
- 【BZOJ3784】树上路径
题目大意 给定一个\(N\)个结点的树,结点用正整数\(1..N\)编号.每条边有一个正整数权值.用\(d(a,b)\)表示从结点\(a\)到结点\(b\)路边上经过边的权值.其中要求\(a < ...
- 【洛谷5309】[Ynoi2012] D1T1(分块)
点此看题面 大致题意: 两种操作,区间求和,将形如\(ax+y\)的位置的元素值加\(z\). 分块 这种题目显然就是按照\(x\)与\(\sqrt n\)的大小关系来分块. 对于\(x>\sq ...
- 最短路算法——SPFA
用途: 单源最短路径,不可以处理含负权边的图但可以用来判断是否存在负权回路: 复杂度O(kE) [k <= 2, E 为边数]: 算法核心: Bellman-Ford 算法的优化,实质与前算法一 ...
- 【转】Uint8Array 转为 string,解决中文乱码
来源: <http://stackoverflow.com/questions/8936984/uint8array-to-string-in-javascript/22373197> / ...
- java中String、StringBuffer和StringBuilder的区别(简单介绍)
简单介绍 java中用于处理字符串常用的有三个类: 1.java.lang.String 2.java.lang.StringBuffer 3.java.lang.StrungBuilder 三者共同 ...
- 解决div+img布局下img下端出现空白的bug
1.将图片转换为块级对象 即设置img为“display:block;”.在本例中添加一组CSS代码:“#sub img {display:block;}”. 2.设置图片的垂直对齐方式 即设置图片的 ...
- GoBelieve JS IM SDK接入备忘
类IMService 构造函数 参数说明: * `observer` 回调对象(可选) 设置当前用户的access token 属性名:String accessToken 功能:在调用start之前 ...
- spring(三)-事务管理
1. Spring事务管理 事务管理:本质是使用spring管理事务,完成数据库对于事务的支持. 事务:一系列对数据库操作的集合,一个操作错误,所有都必须回滚,其特点是acid. (1)事务并发存在问 ...
- kali linux 中文出现乱码问题的解决
确定locales已经安装,用”apt-get install locales”命令:之后可用”locale -a”查看当前系统支持的字符集. 1. 在命令行输入”dpkg-reconfigure l ...