URAL 1841|Cableways|...

27 篇文章 0 订阅
21 篇文章 0 订阅

二维空间中存在若干条连续的折线,折线方向从左下到右上或者右下到左上。一个人沿着这个折线走从下方走到上方。每条折线上可能会有可以直达上方最近折线的线,这种线只能通往最近上方折线,而且走这条线只是y坐标增加。走折线需要一定时间,走直上的线也需要一定的时间。求最少时间到达折线y坐标最大的点。

求第K大。。偷懒并没有写平衡树又写了二分+树状数组。。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define FOR(i,j,k) for(i=j;i<=k;++i)
#define rep(i,j,k) for(i=j;i<k;++i)
const int N = 300005;
typedef long long ll;
struct Data {
    int x, y, v, t;
    Data() { }
    Data(int a, int b, int c, int d) : x(a), y(b), v(c), t(d) { }
} p[N];
int c[N], ax[N], x[N], id[N], n;
ll ans[N];
int sum(int i) {
    int s = 0;
    for (; i; i -= i & -i) s += c[i];
    return s;
}
void add(int i, int x) {
    for (; i <= n; i += i & -i) c[i] += x;
}
int binary(int k) {
    int l = 0, r = n, ans = 0;
    while (l <= r) {
        int mid = l + r >> 1;
        if (sum(mid) < k) l = mid + 1;
        else  r = mid - 1, ans = l;
    }
    return ans;
}
int main() {
    int i, j, k, pos, cnt, v, y, xx, t, l, r, g, gg; ll pre, mx;
    while (scanf("%d", &n) != EOF) {
        FOR(i,0,n) scanf("%d", &x[i]);
        cnt = y = 0;
        rep(i,0,n) {
            scanf("%d%d", &v, &j);
            p[cnt++] = Data(x[i], ++y, v, -1);
            while (j--) {
                scanf("%d%d", &xx, &t);
                p[cnt++] = Data(xx, ++y, v, t);
            }
        }
        p[cnt++] = Data(x[n], ++y, 0, -1);
        n = 0;
        rep(i,0,cnt) {
            if (p[i].t != -1 && p[i + 1].t == -1 && p[i].x == p[i + 1].x)
                swap(p[i], p[i + 1]);
            ax[++n] = p[i].x;
        }
        sort(ax + 1, ax + n + 1);
        n = unique(ax + 1, ax + n + 1) - (ax + 1);
        pre = 0;
        memset(c, 0, sizeof c);
        rep(i,0,cnt-1) {
            ans[i] = pre;
            gg = l = lower_bound(ax + 1, ax + n + 1, p[i].x) - ax;
            g  = r = lower_bound(ax + 1, ax + n + 1, p[i + 1].x) - ax;
            if (l > r) swap(l, r);
            mx = pre + (ll) abs(ax[r] - ax[l]) * p[i].v;
            while (sum(r) - sum(l - 1)) {
                k = binary(sum(l - 1) + 1);
                add(k, -1);
                pos = ax[k];
                k = id[k];
                mx = min(mx, ans[k] + p[k].t + (ll)abs(ax[g] - pos) * p[i].v);
            }
            if (p[i].t != -1)
                add(gg, 1), id[gg] = i;
            pre = mx;
        }
        printf("%I64d\n", pre);
    }
    return 0;
}

Cableways

Description

Vova bought a new wide-angle lens for his camera a few days ago. Now he wants to take a panoramic photo with this lens, and that is the reason why he decided to climb the nearest hill.
There is a single narrow serpentine path leading from base of the hill to its top. Vova introduced coordinate axes in such a way that OY is directed along the hill side from base to top and OX is orthogonal to it. The path is represented as a polyline with n segments, vertices of which follow in the order of strictly increasing y-coordinate. For each segment of the path, Vova knows the time required to change x -coordinate by one while moving along this segment.
Moreover, Vova can use cableways. They start at some point of the path, run strictly up the hill parallel to OY axis and end at the next intersection with the path. Vova knows the time required to use each cableway.
Help Vova climb the hill before it gets dark! Note that Vova is so determined that he will refuse to go down the hill even if this action helps him to climb the hill faster.

Input

The first line contains a number of segments in the path n(1n105) . The next line contains n+1 integers which are x-coordinates of polyline vertices in the order from base to top. No two consecutive vertices have the same x -coordinate. The i-th of the following n lines contains integers vi and mi which are time required to change x-coordinate by one while moving along the i -th segment and a number of cableways starting at the i-th segment, respectively. The rest of this line contains mi pairs of integers describing the cableways. Each cableway is described with x -coordinate of its start point and time required to use this cableway. The cableways are described in the order from base to top. No two cableways start at the same point. If a cableway starts at the common point of two path segments, it encounters in the description of only one segment. It is guaranteed that each cableway ends somewhere on the path. All numbers in lines are separated by spaces. All coordinates don’t exceed 106 in their absolute value. All times are positive and don’t exceed 106 . The total number of cableways doesn’t exceed 105 .

Output

Output the minimal time required to climb the hill.

Sample

input

4
0 5 15 10 0
1 1 1 100
1 1 7 1 
1 0
1 0

output

15

input

3
0 10 0 10
10 0
10 1 10 1
10 0

output

101
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值