HDU 5861 Road 线段树区间更新单点查询
题目链接:
http://acm.split.hdu.edu.cn/showproblem.php?pid=5861
Road
Time Limit: 12000/6000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)
问题描述
There are n villages along a high way, and divided the high way into n-1 segments. Each segment would charge a certain amount of money for being open for one day, and you can open or close an arbitrary segment in an arbitrary day, but you can open or close the segment for just one time, because the workers would be angry if you told them to work multiple period.
We know the transport plan in the next m days, each day there is one cargo need to transport from village ai to village bi, and you need to guarantee that the segments between ai and bi are open in the i-th day. Your boss wants to minimize the total cost of the next m days, and you need to tell him the charge for each day.
(At the beginning, all the segments are closed.)
输入
Multiple test case. For each test case, begins with two integers n, m(1<=n,m<=200000), next line contains n-1 integers. The i-th integer wi(1<=wi<=1000) indicates the charge for the segment between village i and village i+1 being open for one day. Next m lines, each line contains two integers ai,bi(1≤ai,bi<=n,ai!=bi).
输出
For each test case, output m lines, each line contains the charge for the i-th day.
样例
sample input
4 3
1 2 3
1 3
3 4
2 4sample output
3
5
5
题意
有n个村庄排成一行,中间n-1条路,每条路开启的花费是a[i]每天,你可以选择每条路的开启时间和关闭时间,但是路关闭之后就不能再开启。现在告诉你接下来的m天的行程,每天会有一辆货车从u到v,你需要保证货车经过的道路必须是开启的。问每天的最小花费。
题解
用线段树求出使用到每条道路的最早时间和最晚时间(这就是我们需要设置的开启时间和关闭时间了)。然后以天数作为数组,再建一颗线段树,根据没条道路的开放时间[mi,ma]去区间更新,然后单点查询求每一天的花费是多少。
代码
这里用的是一种不需要打懒惰标记的方法,并且由于是单点查询,所以只需要往下更新,不用回溯回来。
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=2e5+10;
int road[maxn];
//这里maxv,minv是一颗线段树,sumv是另一颗线段树,偷懒写在一起了
int maxv[maxn<<2],minv[maxn<<2];
int sumv[maxn<<2];
int ql,qr,_v,_v2;
void update(int o,int l,int r,int type) {
if(ql<=l&&r<=qr) {
if(type==-1) {
maxv[o]=max(maxv[o],_v);
minv[o]=min(minv[o],_v);
} else {
sumv[o]+=_v2;
}
} else {
if(ql<=mid) update(lson,l,mid,type);
if(qr>mid) update(rson,mid+1,r,type);
}
}
int _p,_ma,_mi,_sum;
void query(int o,int l,int r,int add,int ma,int mi) {
if(l==r) {
_ma=max(ma,maxv[o]);
_mi=min(mi,minv[o]);
_sum=add+sumv[o];
} else {
if(_p<=mid) query(lson,l,mid,add+sumv[o],max(ma,maxv[o]),min(mi,minv[o]));
else query(rson,mid+1,r,add+sumv[o],max(ma,maxv[o]),min(mi,minv[o]));
}
}
void init() {
clr(sumv,0);
clr(maxv,-1);
clr(minv,0x3f);
}
int main() {
int n,m;
while(scanf("%d%d",&n,&m)==2&&n) {
init();
for(int i=1; i<=n-1; i++) scanf("%d",&road[i]);
///求每条路开放的最早和最迟时间
for(int i=1; i<=m; i++) {
int u,v;
scanf("%d%d",&u,&v);
if(u>v) swap(u,v);
v--;
ql=u,qr=v,_v=i;
update(1,1,n-1,-1);
}
for(int i=1; i<=n-1; i++) {
_p=i,_ma=-1,_mi=INF;
query(1,1,n-1,0,_ma,_mi);
// printf("%d,%d\n",_mi,_ma);
if(_mi>_ma) continue;
ql=_mi,qr=_ma,_v2=road[i];
update(1,1,m,1);
}
///求每天的花费是多少
for(int i=1; i<=m; i++) {
_p=i,_sum=0;
query(1,1,m,0,_ma,_mi);
printf("%d\n",_sum);
}
}
return 0;
}
//end-----------------------------------------------------------------------
HDU 5861 Road 线段树区间更新单点查询的更多相关文章
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】
任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 ...
- hdu 4031 attack 线段树区间更新
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Subm ...
- 【POJ 2777】 Count Color(线段树区间更新与查询)
[POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4094 ...
- HDU 5861 Road(线段树 区间修改 单点查询)
Road Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- HDU 3308 LCIS 线段树区间更新
最近开始线段树一段时间了,也发现了不少大牛的博客比如HH大牛 ,小媛姐.这个题目是我在看HH大牛的线段树专题是给出的习题,(可以去他博客找找,真心推荐)原本例题是POJ3667 Hotel 这个题目 ...
- hdu 1698(线段树区间更新)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- D - Mayor's posters POJ - 2528 离散化+线段树 区间修改单点查询
题意 贴海报 最后可以看到多少海报 思路 :离散化大区间 其中[1,4] [5,6]不能离散化成[1,2] [2,3]因为这样破坏了他们的非相邻关系 每次离散化区间 [x,y]时 把y+1点也加入 ...
- HDU 1556【线段树区间更新】
这篇lazy讲的很棒: https://www.douban.com/note/273509745/ if(tree[rt].l == l && r == tree[rt].r) 这里 ...
随机推荐
- html5手机浏览器启动微信客户端支付实例
html5手机浏览器启动微信客户端支付实例,外部浏览器html5微信支付技术,如何在手机浏览器微信支付,在微信客户端外的移动端网页使用微信支付 首先在微信支付官网https://pay.weixin. ...
- Redis之Redis持久化
Redis(Remote Dictionary Server)是一个可持久化的内存.Key-Value数据库. 作为内存数据库,为了防止因服务器断电或系统宕机而引起的数据丢失问题,Redis自带了持久 ...
- 使同一个server上不同port的django应用可在同一个浏览器上打开
如果我们有两个django应用site1和site2同时跑在同一个server的不同端口,同时我们在同一个浏览器的不同tab登录.那么这时就出出现这种情况,当我们登录site2时就会将site1上登录 ...
- Spark SQL join的三种实现方式
引言 join是SQL中的常用操作,良好的表结构能够将数据分散到不同的表中,使其符合某种规范(mysql三大范式),可以最大程度的减少数据冗余,更新容错等,而建立表和表之间关系的最佳方式就是join操 ...
- 基于OMAPL138的Linux字符驱动_GPIO驱动AD9833(一)之miscdevice和ioctl
基于OMAPL138的Linux字符驱动_GPIO驱动AD9833(一)之miscdevice和ioctl 0. 导语 在嵌入式的道路上寻寻觅觅很久,进入嵌入式这个行业也有几年的时间了,从2011年后 ...
- 用gogs搭建git服务器
Gogs 是一款极易搭建的自助 Git 服务. Gogs 的目标是打造一个最简单.最快速和最轻松的方式搭建自助 Git 服务.使用 Go 语言开发使得 Gogs 能够通过独立的二进制分发,并且支持 G ...
- SSM-CRUD入门项目——新增与校验
新增 分析: 在Index.jsp页面点击新增,弹出对话框(模态框) 数据库查询部门列表显示在模态框中 用户输入数据完成操作 我们先把模态框构建出来,并对新增按钮添加事件,点击按钮弹出模态框: < ...
- 20155218 《Java程序设计》实验五(网络编程与安全)实验报告
20155218 <Java程序设计>实验五(网络编程与安全)实验报告 一.实验内容及步骤 (一) 编写MyBC.java实现中缀表达式转后缀表达式的功能 编写MyDC.java实现从上面 ...
- 20155222卢梓杰 《Java程序设计》第1周学习总结
20155222 <Java程序设计>第1周学习总结 教材学习内容总结 JDK是一个工具程序,包括了JAVA程序语言,工具程序与JRE,JRE包括了部署技术,JAVA SE API 与 J ...
- 20155320 2016-2017-2《Java程序设计》课程总结
20155320 2016-2017-2<Java程序设计>课程总结 (按顺序)每周作业链接汇总 预备作业1:第一次写随笔,回答了老师的一些问题,写下了期望和目标 预备作业2:总结了一下自 ...