Road

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1132    Accepted Submission(s): 309

Problem Description
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.)
Input
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).
Output
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
Author
BUPT
Source
【题意】n个点将一条线段分成n-1份,点的编号从左往右1~n,每条路你只能打开一次,打开后每天都收费,当然打开后你也可以选择关上,但是关上后这条路就再也不能打开了。每条线段有一个Cost值,然后Q次询问,没次询问给出两个点U,V,表示从U,到V,你必须保证U->V上的线段都打开才能过去,然后问你在保证你能过去的情况下,每天的最小花费。
【分析】要想避免无用的花费,那么我们可以对于每条路,他第一次使用就把它打开,他最后一条使用完过后就把它关了,因为这条路再也不用了。那么我们可以先找到每条路的打开时间和关闭时间,这个类似于区间覆盖,所以我们可以用线段树来维护,复杂度NlogN。然后就是要找到当前天,有多少路是打开的,这个我们可以用一个类似于莫队的做法,假设对于前一天我们已经知道了答案,那么对于今天,我们只要知道有多少条路是今天打开的和关闭的,那么我们今天就可以根据前一天的来推,复杂度O(M);
  1. #include <bits/stdc++.h>
  2. #define mp make_pair
  3. #define pb push_back
  4. #define met(a,b) memset(a,b,sizeof a)
  5. #define inf 10000000
  6. using namespace std;
  7. typedef long long ll;
  8. typedef pair<int,int>pii;
  9. const int N = 4e5+;
  10. const double eps = 1e-;
  11. int n,sum[*N],m;
  12. int lazy[*N],a[N],mi[N*],ma[N*];
  13. vector<int>st[N],en[N];
  14. struct man{
  15. int u,v;
  16. }q[N];
  17. void init(){
  18. for(int i=;i<N;i++){
  19. st[i].clear();
  20. en[i].clear();
  21. }
  22. }
  23. void pushDown(int pos){
  24. if(mi[pos]!=inf){
  25. mi[pos*]=min(mi[pos*],mi[pos]);
  26. mi[pos*+]=min(mi[pos*+],mi[pos]);
  27. }
  28. if(ma[pos]!=){
  29. ma[pos*]=max(ma[pos*],ma[pos]);
  30. ma[pos*+]=max(ma[pos*+],ma[pos]);
  31. }
  32. return;
  33. }
  34.  
  35. void update(int L,int R,int val,int l,int r,int pos) {
  36. if(l>=L&&r<=R) {
  37. mi[pos]=min(mi[pos],val);
  38. ma[pos]=max(ma[pos],val);
  39. return;
  40. }
  41. int mid=(l+r)>>;
  42. pushDown(pos);
  43. if(L<=mid) update(L,R,val,l,mid,pos<<);
  44. if(mid<R)update(L,R,val,mid+,r,pos<<|);
  45. }
  46. void query(int l,int r,int pos) {
  47. if(l==r){
  48. if(mi[pos]!=inf)st[mi[pos]].pb(l);
  49. if(ma[pos]!=)en[ma[pos]+].pb(l);
  50. return;
  51. }
  52. int mid=(l+r)>>;
  53. pushDown(pos);
  54. query(l,mid,pos<<);
  55. query(mid+,r,pos<<|);
  56. return;
  57. }
  58. void build(int l,int r,int pos){
  59. mi[pos]=inf;
  60. ma[pos]=;
  61. if(l==r){
  62. return;
  63. }
  64. int mid=(l+r)/;
  65. build(l,mid,pos*);
  66. build(mid+,r,pos*+);
  67. }
  68. int main() {
  69. int ll,rr,cnt=;
  70. while(~scanf("%d%d",&n,&m)){
  71. init();
  72. build(,n-,);
  73. for(int i=;i<n;i++){
  74. scanf("%d",&a[i]);
  75. }
  76. for(int i=;i<=m;i++){
  77. scanf("%d%d",&q[i].u,&q[i].v);
  78. if(q[i].u>q[i].v)swap(q[i].u,q[i].v);
  79. update(q[i].u,q[i].v-,i,,n-,);
  80. }
  81. query(,n-,);
  82. int ans=;
  83. for(int i=;i<=m;i++){
  84. for(int x:st[i]){
  85. ans+=a[x];
  86. }
  87. for(int x:en[i]){
  88. ans-=a[x];
  89. }
  90. printf("%d\n",ans);
  91. }
  92. }
  93. return ;
  94. }

HDU 5861 Road(线段树 区间修改 单点查询)的更多相关文章

  1. HDU 5861 Road 线段树区间更新单点查询

    题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5861 Road Time Limit: 12000/6000 MS (Java/Othe ...

  2. D - Mayor's posters POJ - 2528 离散化+线段树 区间修改单点查询

    题意 贴海报 最后可以看到多少海报 思路 :离散化大区间  其中[1,4] [5,6]不能离散化成[1,2] [2,3]因为这样破坏了他们的非相邻关系 每次离散化区间 [x,y]时  把y+1点也加入 ...

  3. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  4. ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】

    任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 ...

  5. hdu 1166 敌兵布阵 线段树区间修改、查询、单点修改 板子题

    题目链接:敌兵布阵 题目: C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视 ...

  6. POJ 3468 A Simple Problem with Integers(线段树区间修改及查询)

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  7. ZOJ 1610 Count the Colors(线段树,区间覆盖,单点查询)

    Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on ...

  8. POJ 2528 Mayor's posters(线段树,区间覆盖,单点查询)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45703   Accepted: 13239 ...

  9. 【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers

    http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状 ...

随机推荐

  1. Enterprise Architect13 : 去掉UML图页面右侧那一道竖线

    我们在使用Enterprise Architect 中,画用例图,时序图时,页面右侧有一条竖线,见下图: 如果页面元素太多,会超出竖线的范围,显得很不协调. 如果像去掉竖线,只需选择主菜单的Layou ...

  2. Sass 基本函数

    Sass 中的常用函数 一.字符串函数 1. unquote($string): 删除字符串前后的引号,删除一对引号,如果这个字符串没有带有引号,将返回原始的字符串. 示例: .text1 { con ...

  3. 【51NOD-0】1130 N的阶乘的长度 V2(斯特林近似)

    [算法]数学 [题解]斯特林公式: #include<cstdio> #include<algorithm> #include<cmath> using names ...

  4. PACM Team(牛客第三场多校赛+dp+卡内存+打印路径)

    题目链接(貌似未报名的不能进去):https://www.nowcoder.com/acm/contest/141/A 题目: 题意:背包题意,并打印路径. 思路:正常背包思路,不过五维的dp很容易爆 ...

  5. ie6下双边距的问题

    1.ie6双边距情况 <div class="red"></div> <div class="blue"></div& ...

  6. vue中的图片加载与显示默认图片

    HTML: <div class="content-show-img"> <div class="show-img"> <img ...

  7. ES6 新增的一些东西

    一.常量 不允许重复定义 const a='HELLO' const a='world'//报错Uncaught SyntaxError: Identifier 'a' has already bee ...

  8. 浅析MongoDB用户管理

    浅析MongoDB用户管理 http://www.jb51.net/article/53830.htm mongodb3.03开启认证 http://21jhf.iteye.com/blog/2216 ...

  9. ServerSocket和Socket通信

    服务器端: 1.服务器端建立通信ServerSocket对象,并设置端口号 2.服务器建立Socket接收客户端连接 3.建立IO输入流读取客户端发送的数据 4.建立IO输出流向客户端输出数据 客户端 ...

  10. awk常见操作整理(更新)

    awk的基本结构 awk 'BEGIN{} pattern {} END {}' #pattern {} 部分是针对每行进行循环处理的,有pattern表示对匹配到的行处理,没有pattern表示对所 ...