HDU4791(线段树+二分)

来源:互联网 发布:上海口腔网络咨询招聘 编辑:程序博客网 时间:2024/05/16 16:13
#include <cstdio>#include <cstring>#include <cmath>#include <iostream>#include <algorithm>using namespace std;#define maxn 111111#define INF 1000000009#define pl c<<1#define pr (c<<1)|1#define lson tree[c].l,tree[c].mid,c<<1#define rson tree[c].mid+1,tree[c].r,(c<<1)|1int n, m;long long s[maxn], p[maxn];long long q;struct node {    int l, r, mid;    long long num;}tree[maxn<<4];void build_tree (int l, int r, int c) {    tree[c].l = l, tree[c].r = r, tree[c].mid = (l+r)>>1;    if (l == r) {        tree[c].num = s[r+1]*p[r+1];        return ;    }    build_tree (lson);    build_tree (rson);    tree[c].num = min (tree[pl].num, tree[pr].num);}long long query (int l, int r, int c, int x, int y) {    long long ans = 0;    if (x == l && y == r) {        ans = tree[c].num;        return ans;    }    else if (y <= tree[c].mid) {        return query (lson, x, y);    }    else if (x > tree[c].mid) {        return query (rson, x, y);    }    else return min (query (lson, x, tree[c].mid), query (rson, tree[c].mid+1, y));}int main () {    int t;    scanf ("%d", &t);    while (t--) {        scanf ("%d%d", &n, &m);        for (int i = 1; i <= n; i++) {            scanf ("%lld%lld", &s[i], &p[i]);        }        if (n > 1)            build_tree (1, n-1, 1);        for (int i = 1; i <= m; i++) {            scanf ("%lld", &q);            int pos = upper_bound (s+1, s+1+n, q) - s;            pos--;            if (pos == n) {                printf ("%lld\n", p[pos]*q);                continue;            }            else                printf ("%lld\n", min (p[pos]*q, query (1, n-1, 1, pos, n-1)));        }    }    return 0;}/*103 100 20 100 10 200 155 100 5*/

0 0
原创粉丝点击