题目链接:

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 4

sample 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-----------------------------------------------------------------------

转载于:https://www.cnblogs.com/fenice/p/5785322.html

HDU 5861 Road 线段树区间更新单点查询相关推荐

  1. Assign the task HDU - 3974(线段树+dfs建树+单点查询+区间修改)

    题意: 染色问题:给一个固定结构的树,现在有两个操作: (1) y 将结点x及其所有后代结点染成颜色y: (2)查询结点x当前的颜色. 其实就是区间染色问题,不过需要dfs预处理, 题目: There ...

  2. 1631 小鲨鱼在51nod小学(线段树区间修改+单点查询:不用下传lazy的区间修改)

    题目描述: 1631 小鲨鱼在51nod小学 鲨鱼巨巨2.0(以下简称小鲨鱼)以优异的成绩考入了51nod小学.并依靠算法方面的特长,在班里担任了许多职务. 每一个职务都有一个起始时间A和结束时间B, ...

  3. hdu.3308 LCIS(线段树,区间合并+单点更新)

    按照傻崽大神的线段树修炼路线,自己做的第二道区间合并的题. 问题比较简单明了,区间求最长连续上升子序列,但是是需要单点更新的 n个数, m组操作 Q A B 询问[A,B]区间的最长连续上升子序列: ...

  4. hdu 3577(线段树区间更新)

     题意:输入一个t,表示有t组测试数据: 接下来一行,输入两个数,k,m,其中k表示这个辆车最多可以坐这么多人,m表示有m次询问能否上车: 每一次询问,输入两个数a,b,表示该乘客能否在a站台上车 ...

  5. HDU 1556【线段树区间更新】

    这篇lazy讲的很棒: https://www.douban.com/note/273509745/ if(tree[rt].l == l && r == tree[rt].r) 这里 ...

  6. hdu 3966(树链剖分+线段树区间更新)

    传送门:Problem 3966 https://www.cnblogs.com/violet-acmer/p/9711441.html 学习资料: [1]线段树区间更新:https://blog.c ...

  7. hdu 5692 Snacks(dfs序+线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5692 解题思路:这道题是树节点的点权更新,而且涉及到子树,常用的思路是利用dfs序,用线段树来对区间进 ...

  8. POJ 2777 ZOJ 1610 HDU 1698 --线段树--区间更新

    直接将这3题 放一起了  今天在做线段树的东西 这3个都是区间更新的 查询方式互相不同 反正都可以放到一起吧 直接先上链接了 touch me touch me touch me 关于涉及到区间的修改 ...

  9. hdu 1698(线段树区间更新)

    解题思路:线段树区间更新水题. #include<iostream> #include<cstdio> #include<cstring> using namesp ...

最新文章

  1. BZOJ1202: [HNOI2005]狡猾的商人
  2. 使用命令创建mysql_用命令创建MySQL数据库
  3. python全局变量的声明和使用_Python二级(07)——函数和代码复用
  4. CGI、FastCGI和php-fpm的概念和区别
  5. 使用print写文件
  6. java jxl读取xlsx_Java添加、读取Excel公式
  7. WinForm如何输出中文星期几?
  8. 蓝懿iOS培训日志22 图册
  9. ECSHOP 商品详情页相关属性商品 由新到旧排序
  10. js手机号批量滚动抽奖代码实现
  11. MySQL 到底能不能放到 Docker 里跑? 1
  12. SSM-MyBatis框架学习笔记
  13. 39个让你得到设计灵感的站点
  14. 各个音阶的频率_各音源的频率范围表
  15. 什么是SDK? SDK是什么意思?
  16. linux手机E680的几个概念
  17. cookie跨域,实现单点登录
  18. CAS单点登录(SSO)介绍及部署
  19. 【目标跟踪】基于迭代扩展卡尔曼滤波算法实现目标滤波跟踪(IEKF)附Matlab代码
  20. 用HTML+CSS写一个请假条

热门文章

  1. vlan网络下的设置
  2. 好程序员web前端分享数组及排序、去重和随机点名
  3. 自动化运维平台OMserver部署过程中解决的问题1
  4. salt-ssh 安装salt-minion 笔记
  5. 安卓首页图片轮播效果(淘宝、京东首页广告效果)
  6. DHTML【2】--HTML
  7. Routeros双adsl线路基于ip分段策略路由
  8. Outlook通过RPC或RPC over HTTPS访问Exchane邮箱:Exchange2003系列之四
  9. FTP之‘基础连接已关闭:服务器提交了协议冲突’错误探析
  10. 后盾网lavarel视频项目---图片上传