HDU1698 线段树(区间更新区间查询)
Just a Hook |
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 31 Accepted Submission(s): 27 |
Problem Description
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. For each cupreous stick, the value is 1. Pudge wants to know the total value of the hook after performing the operations. |
Input
The 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. |
Output
For 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 |
Sample Output
Case 1: The total value of the hook is 24. |
Source
2008 “Sunline Cup” National Invitational Contest
|
题意:
大小为n的数组,数组元素初始值为1,有q次操作,x,y,z表示从第x到第y所有的元素的值变为z,最后问这串数的和。
代码:
//基础的线段树模板题
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn=;
int t,n,q;
ll sum[maxn*],add[maxn*];
void pushup(int rt){
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void pushdown(int rt,int len){
if(add[rt]){
add[rt<<]=add[rt];
add[rt<<|]=add[rt];
sum[rt<<]=add[rt]*(len-(len>>));
sum[rt<<|]=add[rt]*(len>>);
add[rt]=;
}
}
void build(int l,int r,int rt){
add[rt]=;
if(l==r){
//scanf("%I64d",&sum[rt]);
sum[rt]=;
return;
}
int m=(l+r)>>;
build(l,m,rt<<);
build(m+,r,rt<<|);
pushup(rt);
}
void update(int L,int R,int c,int l,int r,int rt){
if(L<=l&&R>=r){
add[rt]=c;
sum[rt]=(ll)c*(r-l+);
return;
}
pushdown(rt,r-l+);
int m=(l+r)>>;
if(L<=m) update(L,R,c,l,m,rt<<);
if(R>m) update(L,R,c,m+,r,rt<<|);
pushup(rt);
}
ll querry(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r) return sum[rt];
pushdown(rt,r-l+);
int m=(l+r)>>;
ll ans=;
if(L<=m) ans+=querry(L,R,l,m,rt<<);
if(R>m) ans+=querry(L,R,m+,r,rt<<|);
return ans;
}
int main()
{
int x,y,z;
scanf("%d",&t);
for(int cas=;cas<=t;cas++){
scanf("%d%d",&n,&q);
build(,n,);
while(q--){
scanf("%d%d%d",&x,&y,&z);
update(x,y,z,,n,);
}
printf("Case %d: The total value of the hook is %I64d.\n",cas,querry(,n,,n,));
}
}
HDU1698 线段树(区间更新区间查询)的更多相关文章
- HDU1698 线段树(区间更新区间查询)
In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...
- hdu1698 线段树区间更新
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- codevs 1690 开关灯 线段树区间更新 区间查询Lazy
题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...
- A Simple Problem with Integers 线段树 区间更新 区间查询
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 115624 Accepted: 35897 Case Time Lim ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92632 ...
- hdu1698线段树区间更新
题目链接:https://vjudge.net/contest/66989#problem/E 坑爹的线段树照着上一个线段树更新写的,结果发现有一个地方就是不对,找了半天,发现是延迟更新标记加错了!! ...
- POJ-3468(线段树+区间更新+区间查询)
A Simple Problem With Integers POJ-3468 这题是区间更新的模板题,也只是区间更新和区间查询和的简单使用. 代码中需要注意的点我都已经标注出来了,容易搞混的就是up ...
- CDOJ 1057 秋实大哥与花 线段树 区间更新+区间查询
链接: I - 秋实大哥与花 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit ...
随机推荐
- C++字符串拼接和输入
一 .char类型字符串以空字符结尾 1.以空字符结尾,空字符被写作\0,其ASCII码为0,用来标记字符串的结尾. char dog[4]={'a','b','c','d'} //不是一个字符串 ...
- Faster RCNN论文解析
Faster R-CNN由一个推荐区域的全卷积网络和Fast R-CNN组成, Fast R-CNN使用推荐区域.整个网络的结构如下: 1.1 区域推荐网络 输入是一张图片(任意大小), 输出是目标推 ...
- [leetcode-744-Find Smallest Letter Greater Than Target]
Given a list of sorted characters letters containing only lowercase letters, and given a target lett ...
- BZOJ 4557 JLOI2016 侦查守卫 树形dp
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4557 题意概述: 给出一棵树,每个点付出代价w[i]可以控制距离和它不超过d的点,现在给 ...
- POJ 3675 Telescope(简单多边形和圆的面积交)
Description Updog is watching a plane object with a telescope. The field of vision in the telescope ...
- 团队Beta阶段事后分析
团队Beta阶段事后分析 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件要解决用户的休闲娱乐问题,为用户提供好玩的模拟经营类的游戏,游戏主题 ...
- UML设计(团队作业)
UML设计 一.团队信息 1.队名 读完文章再睡觉 2.团队成员的学号与姓名 学号 姓名 211606381 吴伟华(队长) 211606369 蔺皓雯 211606340 杨池宇 211606372 ...
- c#积累之测试
初来上班,免不了看别人代码.快速搞懂别人代码是我现在受到的一大挑战.寻摸着规律,发现一边进行调试,一边进行行行注释的逻辑判断不失为一种妙招. c#调试用的是vs2012.f11键和f10和f5键的应用 ...
- OnDraw和Opanit的区别
OnPaint是WM_PAINT消息的消息处理函数,在OnPaint中调用OnDraw,一般来说,用户自己的绘图代码应放在OnDraw中. OnPaint() 是CWnd的类成员,负责响应WM_PA ...
- 网卡多ip 再看arp; arp队列也会缓存skb
[结论] 当协议失效的时候,skb会挂载arp的neigt的一个链表上,然后直接返回了,相当于数据包发下了,当arp收到数据包去修复neigh的目的地址的时候,会把之前所有的neihe中等待的skb全 ...