Watching the Kangaroo

Watching the Kangaroo

这里写图片描述
这里写图片描述

题意:给出n段区间(l,r),给出m给位置x,求对于所有(l <= x <= r)区间,求min(x-l,r-x)的最大值。
解法:二分加前缀。对于一个区间,分成两半排序(考虑奇偶),对于x属于左半段的,二分出一个最接近x的右边,假设位置为p,那么在p~n都满足右边大于等于x,那么只有找到p~n左边值的最小值min,答案就是x-min。同理右边

#include <iostream>
using namespace std;
#include <stdio.h>

const int maxn = 200010;

int lef[maxn][2], righ[maxn][2], prelef[maxn], prerigh[maxn];
int n, m, x, y, l, r, mid, ans;

int max(int x, int y) {
    if (x > y) return x;
    return y;
}

int min(int x, int y) {
    if (x < y) return x;
    return y;
}

void swap(int &x, int &y) {
    int temp = x;
    x = y;
    y = temp;
}

void sortlef(int i, int j) {
    int p = i, q = j;
    int mid1 = lef[(p+q)/2][1];
    int mid2 = lef[(p+q)/2][0];
    while (p <= q) {
        while (lef[p][1] < mid1 || (lef[p][1] == mid1 && lef[p][0] < mid2)) p++;
        while (lef[q][1] > mid1 || (lef[q][1] == mid1 && lef[q][0] > mid2)) q--;
        if (p <= q) {
            for (int k = 0; k <= 1; k++) {
                int  temp = lef[p][k];
                lef[p][k] = lef[q][k];
                lef[q][k] = temp;
            }
            p++; q--;
        }
    }
    if (i < q) sortlef(i, q);
    if (p < j) sortlef(p, j);
}

void sortrigh(int i, int j) {
    int p = i, q = j;
    int mid1 = righ[(p+q)/2][0];
    int mid2 = righ[(p+q)/2][1];
    while (p <= q) {
        while (righ[p][0] < mid1 || (righ[p][0] == mid1 && righ[p][1] < mid2)) p++;
        while (righ[q][0] > mid1 || (righ[q][0] == mid1 && righ[q][1] > mid2)) q--;
        if (p <= q) {
            for (int k = 0; k <= 1; k++) {
                int  temp = righ[p][k];
                righ[p][k] = righ[q][k];
                righ[q][k] = temp;
            }
            p++; q--;
        }
    }
    if (i < q) sortrigh(i, q);
    if (p < j) sortrigh(p, j);
}

int main() {
    int tt, cases = 0;
    scanf("%d", &tt);
    while (++cases <= tt) {
        scanf("%d %d", &n, &m);
        for (int i = 1; i <= n; i++) {
            scanf("%d %d", &x, &y);
            if (x > y) swap(x, y);
            mid = (x+y)/2;
            lef[i][0] = x;
            lef[i][1] = mid;
            righ[i][0] = y-(mid-x);
            righ[i][1] = y;
        }

        sortlef(1, n);
        sortrigh(1, n);

        prelef[n] = lef[n][0];      
        for (int i = n-1; i >= 1; i--) prelef[i] = min(prelef[i+1], lef[i][0]);
        prerigh[1] = righ[1][1];
        for (int i = 2; i <= n; i++) prerigh[i] = max(prerigh[i-1], righ[i][1]);

        printf("Case %d:\n", cases);
        for (int q = 1; q <= m; q++) {
            scanf("%d", &x);
            l = 1; r = n;
            ans = 0;
            while (l <= r) {
                mid = (l+r)/2;
                if (lef[mid][1] >= x) {
                    ans = max(ans, x-prelef[mid]);
                    r = mid-1;
                } else l = mid+1;
            }
            l = 1; r = n;
            while (l <= r) {
                mid = (l+r)/2;
                if (righ[mid][0] <= x) {
                    ans = max(ans, prerigh[mid]-x);
                    l = mid+1;
                } else r = mid-1;
            }
            cout << ans << endl;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值