题目
森森开了一家快递公司,叫森森快递。因为公司刚刚开张,所以业务路线很简单,可以认为是一条直线上的N个城市,这些城市从左到右依次从0到(N−1)编号。由于道路限制,第i号城市(i=0,⋯,N−2)与第(i+1)号城市中间往返的运输货物重量在同一时刻不能超过Ci公斤。
公司开张后很快接到了Q张订单,其中j张订单描述了某些指定的货物要从Sj号城市运输到Tj号城市。这里我们简单地假设所有货物都有无限货源,森森会不定时地挑选其中一部分货物进行运输。安全起见,这些货物不会在中途卸货。
为了让公司整体效益更佳,森森想知道如何安排订单的运输,能使得运输的货物重量最大且符合道路的限制?要注意的是,发货时间有可能是任何时刻,所以我们安排订单的时候,必须保证共用同一条道路的所有货车的总重量不超载。例如我们安排1号城市到4号城市以及2号城市到4号城市两张订单的运输,则这两张订单的运输同时受2-3以及3-4两条道路的限制,因为两张订单的货物可能会同时在这些道路上运输。
输入格式
输出格式
输入样例
10 6
0 7 8 5 2 3 1 9 10
0 9
1 8
2 7
6 3
4 5
4 2
输出样例
7
样例提示
我们选择执行最后两张订单,
即把5公斤货从城市4运到城市2,
并且把2公斤货从城市4运到城市5,
就可以得到最大运输量7公斤。
思路: 最开始想暴力写的,但是!!!想想暴力估计没希望,于是想到线段树维护最小值,看到网上题解又知道了要先贪心排列区间,然后再线段树维护最小值,得到最大的可用。
/*
线段树 + 贪心
查找区间最小值(利用线段树),然后先贪心排序(按照区间前后)
*/
/*
_ooOoo_
o8888888o
88" . "88
(| -_- |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . __
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `-. \_ __\ /__ _/ .-` / /
======`-.____`-.___\_____/___.-`____.-'======
'=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
buddha blesses never get BUG
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
#define ls(x) x << 1
#define rs(x) x << 1 | 1
typedef long long ll;
const int M = 1e5 + 5;
typedef struct {
int left, right, lazy;
ll val;//最小值
}Node;
typedef struct point {
point(int xx, int yy) { x = xx; y = yy; }
int x, y;
}Point;
Node node[M * 4];
vector<Point>vec;
int num[M];
void push_up(int root)
{
node[root].val = min(node[ls(root)].val, node[rs(root)].val);
}
void push_down(int root)//标记下传
{
if (node[root].lazy)
{
node[ls(root)].lazy += node[root].lazy;
node[rs(root)].lazy += node[root].lazy;
node[ls(root)].val += node[root].lazy;
node[rs(root)].val += node[root].lazy;
node[root].lazy = 0;
}
}
void build(int root, int left, int right)//建树
{
node[root].left = left;
node[root].right = right;
node[root].lazy = 0;
if (left == right)
{
node[root].val = num[left];
return;
}
int mid = (left + right) / 2;
build(ls(root), left, mid);
build(rs(root), mid + 1, right);
push_up(root);
}
ll query(int root, int left, int right)//查询
{
if (left <= node[root].left && right >= node[root].right)
return node[root].val;
if (node[root].lazy) push_down(root);
push_up(root);
int mid = (node[root].left + node[root].right) / 2;
ll ans;
if (right <= mid) ans = query(ls(root), left, right);
else if (left > mid) ans = query(rs(root), left, right);
else ans = min(query(rs(root), mid + 1, right), query(ls(root), left, mid));
return ans;
}
void update(int root, int left, int right, ll val)//修改
{
if (left <= node[root].left && node[root].right <= right)
{
node[root].lazy += val;
node[root].val += val;
return;
}
if (node[root].lazy) push_down(root);
int mid = (node[root].left + node[root].right) / 2;
if (right <= mid) update(ls(root), left, right, val);
else if (left > mid) update(rs(root), left, right, val);
else
{
update(rs(root), mid + 1, right, val);
update(ls(root), left, mid, val);
}
push_up(root);
}
bool cmp(Point a, Point b)
{
if (a.y != b.y) return a.y < b.y;
else a.x < b.x;
}
int main()
{
int n, q;
scanf("%d%d", &n, &q);
for (int i = 1; i < n; ++i)
scanf("%d", &num[i]);
build(1, 1, n - 1);
int a, b;
ll ans = 0;
for (int i = 0; i < q; ++i)
{
scanf("%d %d", &a, &b);
if (a > b) swap(a, b);
vec.push_back(Point(a, b));
}
sort(vec.begin(), vec.end(), cmp);
for (int i = 0; i < q; ++i)
{
a = vec[i].x;
b = vec[i].y;
if (a == b) continue;
ll cur = query(1, a + 1, b);
ans += cur;
update(1, a + 1, b, -cur);
}
printf("%lld\n", ans);
return 0;
}