HDU1698 Just a Hook
Description
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
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
the hook after the operations. Use the format in the example.
Sample Input
10
2
1 5 2
5 9 3
Sample Output
Source
一般的线段树区间修改。但是这题有个坑处,就是区间值是直接替换而非增加值,所以pushdown的时候一定要把子节点的值也更新。←因为这个被坑了半小时青春
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1|1
using namespace std;
const int mxn=;
int n,m;
int ans=;
struct tree{
int sum;
int add;
}tr[mxn*];
void Build(int l,int r,int rt){
if(l==r){
tr[rt].sum=;
tr[rt].add=;
return;
}
int mid=(l+r)>>;
Build(ls);
Build(rs);
tr[rt].sum=tr[rt<<].sum+tr[rt<<|].sum;
return;
}
void change(int L,int R,int x,int l,int r,int rt){
if(tr[rt].add){
int mid=(l+r)>>;
tr[rt<<].sum=(mid-l+)*tr[rt].add;//更新下面的值很重要!
tr[rt<<|].sum=(r-mid)*tr[rt].add;//更新!
tr[rt<<].add=tr[rt<<|].add=tr[rt].add;
tr[rt].add=;
}
if(L<=l && r<=R){
tr[rt].sum=(r-l+)*x;
tr[rt].add=x;
return;
}
int mid=(l+r)>>;
if(L<=mid)change(L,R,x,ls);
if(R>mid)change(L,R,x,rs);
tr[rt].sum=tr[rt<<].sum+tr[rt<<|].sum;
return;
} int main(){
int T;
scanf("%d",&T);
for(int ro=;ro<=T;ro++){
memset(tr,,sizeof tr);
scanf("%d",&n);
Build(,n,);
scanf("%d",&m);
int i,j;
int x,y,z;
for(i=;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
change(x,y,z,,n,);
}
printf("Case %d: The total value of the hook is %d.\n",ro,tr[].sum); }
return ;
}
HDU1698 Just a Hook的更多相关文章
- 线段树---成段更新hdu1698 Just a Hook
hdu1698 Just a Hook 题意:O(-1) 思路:O(-1) 线段树功能:update:成段替换 (由于只query一次总区间,所以可以直接输出1结点的信息) 题意:给一组棍子染色,不同 ...
- HDU1698 Just a Hook —— 线段树 区间染色
题目链接:https://vjudge.net/problem/HDU-1698 In the game of DotA, Pudge’s meat hook is actually the most ...
- 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)Total ...
- HDU1698 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 线段树
共有Q个更新,每次更新给更新的区间一个标记,表示该区间是在哪一次被更新,最后统计答案是以最近被更新的值为答案. AC代码: #include<cstdio> const int maxn= ...
- 【原创】hdu1698 Just a Hook(线段树→区间更新,区间查询)
学习线段树第二天,这道题属于第二简单的线段树,第一简单是单点更新,这个属于区间更新. 区间更新就是lazy思想,我来按照自己浅薄的理解谈谈lazy思想: 就是在数据结构中,树形结构可以线性存储(线性表 ...
- hdu1698 Just a Hook 【区间修改】(模板题)
题目链接:https://vjudge.net/contest/182746#problem/E 题目大意: 一段线段由n条小线段组成,每次操作把一个区间的小线段变成金银铜之一(金的价值为3,银为2, ...
随机推荐
- oracle转Mysql中,varchar2(10)和number应该转换为什么类型?
一. varchar2(10)和number应该转换为什么类型? oracle转成mysql时:varchar2(10)可以转成varchar(10)number则要看oracle中存储的具体是什么类 ...
- js定时器的一些小问题
1 js中定时器分为两种:setInterval和setTimeout, 但是在代码的执行中,定时器的优先级最低,系统里其他不在执行的时候,它才开始.例子如下: 2 3 <script> ...
- 如何获取内联样式的width值
如图,如何获取内联样式的width值 不用attr 用css这样写
- poj1144
Network Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12521 Accepted: 5760 Descript ...
- PyQT制作视频播放器
Python应用03 使用PyQT制作视频播放器 作者:Vamei 出处:http://www.cnblogs.com/vamei 严禁任何形式转载. 最近研究了Python的两个GUI包,Tki ...
- Linux中查看各文件夹大小命令du -h --max-depth=1
http://blog.csdn.net/ouyang_peng/article/details/10414499 du -h --max-depth=1
- [tools]tcp/udp连通性测试
一 端口连通性测试意义 测试网络端口可达性,确保给某些使用特定端口的app做链路连通性检测.使它们能够正常的运行起来. 二 法1 使用newclient发包,彼端tcpdump抓包观察是否能收到包 ...
- C语言 const常量讲解
//const的本质 //const本质上是伪常量,无法用于数组初始化以及全局变量初始化 //原因在于const仅仅限定变量无法直接赋值,但是却可以通过指针间接赋值 //例如局部常量在栈区,而不在静态 ...
- Asp.net通过Jquery操作WebService进行Ajax读写
一说到开始,我们当然需要项目. 首先,创建一个Asp.net Web 应用,然后新增一个名称为“Web 服务”的文件,也就是后缀名为".asmx"的文件,然后需要进行配置一下,在W ...
- AS2.0大步更新 Google强势逆天
New Features in Android Studio 2.0Instant Run: Faster Build & Deploy逆天吗?你还在羡慕iOS的playground吗?And ...