HDU5861-Road
来源:互联网 发布:hadoop python接口 编辑:程序博客网 时间:2024/06/05 00:10
Road
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1287 Accepted Submission(s): 363
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 villageai 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.)
We know the transport plan in the next m days, each day there is one cargo need to transport from village
(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 31 2 31 33 42 4
Sample Output
355
Author
BUPT
Source
2016 Multi-University Training Contest 10
Recommend
wange2014
题意:n个点将一条线段分成n-1份,点的编号从左往右1~n,每条路你只能打开一次,打开后每天都收费,当然打开后你也可以选择关上,但是关上后这条路就再也不能打开了。每条线段有一个cost值,有q次询问,每次询问给出两个点U,V,表示从U到V,你必须保证U->V上的线段都打开才能过去,问在保证能过去的情况下,每天的最小花费
解题思路:线段树,预处理出每段路的打开时间和关闭时间即可
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <map>#include <cmath>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;int n, m;LL w[200009], sum[200009];int a[200009], b[200009];int l[200009], r[200009];int x[200009 << 2];void update(int k, int l, int r, int ll, int rr, int val){if (l >= ll&&r <= rr) { x[k] = val; return; }int mid = (l + r) >> 1;if (x[k] != -1) x[k << 1] = x[k << 1 | 1] = x[k], x[k] = -1;if (ll <= mid) update(k << 1, l, mid, ll, rr, val);if (rr>mid) update(k << 1 | 1, mid + 1, r, ll, rr, val);}int query(int k, int l, int r,int p){if (l == r || x[k] != -1) return x[k];int mid = (l + r) >> 1;if(mid>=p) return query(k << 1, l, mid, p);else return query(k << 1 | 1, mid + 1, r, p);}int main(){while (~scanf("%d%d", &n, &m)){for (int i = 1; i<n; i++) scanf("%lld", &w[i]);memset(sum, 0, sizeof sum);memset(a, -1, sizeof a);memset(b, -1, sizeof b);memset(x, -1, sizeof x);for (int i = 1; i <= m; i++){scanf("%d%d", &l[i], &r[i]);if (l[i]>r[i]) swap(l[i], r[i]);}for (int i = m; i >= 1; i--) update(1, 1, n-1, l[i], r[i] - 1, i);for (int i = 1; i <= n; i++){int k=query(1, 1, n - 1, i);if (k != -1) a[i] = k;}memset(x, -1, sizeof x);for (int i = 1; i <= m; i++) update(1, 1, n - 1, l[i], r[i] - 1, i);for (int i = 1; i <= n; i++){int k = query(1, 1, n - 1, i);if (k != -1) b[i] = k + 1;}for (int i = 1; i<n; i++){if (a[i] == -1) continue;sum[a[i]] += w[i];sum[b[i]] -= w[i];}for (int i = 1; i <= m; i++){sum[i] += sum[i - 1];printf("%lld\n", sum[i]);}}return 0;}
阅读全文
0 0
- HDU5861-Road
- hdu5861 Road【线段树】
- hdu5861 Road,线段树,然后扫描
- hdu5861 Road(线段树成段更新+优先队)
- 2016多校训练Contest10: 1005 Road hdu5861
- hdu5861【线段树】
- HDU5861(线段树好题)
- road
- Road
- Road
- Road
- Road
- road
- hdu5861 维护最大最小值线段树
- HDU5861 (线段树区间更新+单点查询)
- Road trip
- Road Layer
- Linear Road
- D
- Android Studio Set of source 代码源集
- [NOI 2003] Editor
- mybatis绑定错误
- [SDUT](3324)顺序表应用1:多余元素删除之移位算法 ---顺序存储(线性表)
- HDU5861-Road
- Java IO相关
- Spark四种运行模式
- C语言运算符优先级问题
- 将两个各自有序的链表,合成一个有序的链表
- 面试常见基本题目总结及php实现(第一部分:排序算法)
- LinearLayout
- elasticsearch5.4.0 查询记录
- git 仓库中文件的可执行权限