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 ...
随机推荐
- 【20180807模拟测试】T2 box
[问题描述] 有个桌子长 R 宽 C,被分为 R*C 个小方格.其中,一些方格上有箱子,一些方格上有按 钮,一些方格上有障碍物,一些方格上是空地.现在有个任务,需要把所有箱子推到这些按 钮上面.箱子有 ...
- NodeJs学习笔记01-你好Node
如果你对NodeJs略知一二,不禁会感叹,使用JS的语法和代码习惯就能开发一个网站的后台,实现复杂的数据交互,牛! 对于学习java和php就夹生的小码农来说,简直就是靡靡之音呐~~~ 今晚带着忐忑的 ...
- Java Web开发框架Spring+Hibernate整合效果介绍(附源码)(已过期,有更好的)
最近花了一些时间整合了一个SpringMVC+springAOP+spring security+Hibernate的一套框架,之前只专注于.NET的软件架构设计,并没有接触过Java EE,好在有经 ...
- Java学习笔记-11.运行期间类型鉴定
1.Class对象的getClasses()方法获取的是该类中所有的公共的内部类,以及从父类,父接口继承来的内部类.getinterfaces()方法返回类继承的所有接口. import javax. ...
- Daily Scrum 10
今天我们小组开会内容分为以下部分: part 1: 经过反复思考,对于上次组会确定的在系统中加入娱乐版块进行了更进一步的商讨; part 2:继续探讨算法实现: part 3:进行明日的任务分配; ◆ ...
- php5.4以上运行yii框架出现问题的解决方法
Ubuntu Server 下安装 Mcrypt Php Extension http://blog.archean.me/2013/10/22/install-mcrypt-php-extensio ...
- Ubuntu 配置 Android 开发 环境
. 果断换Ubuntu了, Ubuntu的截图效果不好, 不能设置阴影 ... 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article ...
- iOS- 网络访问两种常用方式【GET & POST】实现的几个主要步骤
1.前言 上次,在博客里谈谈了[GET & POST]的区别,这次准备主要是分享一下自己对[GET & POST]的理解和实现的主要步骤. 在这就不多废话了,直接进主题,有什么不足的欢 ...
- iOS开发热更新JSPatch
JSPatch,只需在项目中引入极小的引擎,就可以使用JavaScript调用任何Objective-C的原生接口,获得脚本语言的能力:动态更新APP,替换项目原生代码修复bug. 是否有过这样的经历 ...
- 如何设置windows 2003的最大远程连接数
在Windows 2003系统上的远程桌面实际上就是终端服务,虽然远程桌面最初在Windows XP上就已经存在,但由于Windows XP的远程桌面功能,只能提供一个用户使用计算机,因此使用率并不高 ...