题解:AT_abc360_f [ABC360F] InterSections

· · 题解

分析

考虑与 [L_i, R_i] 相交的 [l, r] 需要满足什么条件:

建立平面直角坐标系,x 轴表示 l,y 轴表示 r。可以发现,上面的情况 1 转化为左下角 (L_i + 1, R_i + 1),右上角 (R_i - 1, 10 ^ 9) 的矩形,情况 2 转化为左下角 (0, L_i + 1),右上角 (L_i - 1, R_i - 1) 的矩形。那么问题变为在平面直角坐标系找到一个点,使它被最多的矩形覆盖,扫描线即可。

写代码时要注意一些边界问题。

upd 2024.07.02

被 hack 了。hack 数据:

输入

3
2 10
8 10
5 8

输出

0 6

错误原因:没有在 l 相同时比较 r 的大小。

代码已修复。

代码

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<LL, LL> PLL;
const LL N = 4e5 + 10, INF = 1e18;

struct node
{
    LL x, y, yy, op;

    bool operator < (const node &W) const
    {
        if (x != W.x) return x < W.x;
        return op < W.op;
    }
}seg[N];
LL lsh[N], tott;
struct Node
{
    LL l, r;
    LL mx, mxid, tag;
}tr[N << 2];

LL find(LL x) {return lower_bound(lsh + 1, lsh + 1 + tott, x) - lsh;}

PLL getmax(PLL x, PLL y)
{
    if (x.first > y.first) return x;
    else if (x.first < y.first) return y;
    else
    {
        if (x.second < y.second) return x;
        else return y;
    }
}

void pushup(LL u)
{
    if (tr[u << 1].mx > tr[u << 1 | 1].mx) tr[u].mx = tr[u << 1].mx, tr[u].mxid = tr[u << 1].mxid;
    else if (tr[u << 1].mx < tr[u << 1 | 1].mx) tr[u].mx = tr[u << 1 | 1].mx, tr[u].mxid = tr[u << 1 | 1].mxid;
    else tr[u].mx = tr[u << 1].mx, tr[u].mxid = min(tr[u << 1].mxid, tr[u << 1 | 1].mxid);
}

void pushdown(LL u)
{
    if (tr[u].tag)
    {
        tr[u << 1].mx += tr[u].tag, tr[u << 1].tag += tr[u].tag;
        tr[u << 1 | 1].mx += tr[u].tag, tr[u << 1 | 1].tag += tr[u].tag;
        tr[u].tag = 0;
    }
}

void build(LL u, LL l, LL r)
{
    tr[u] = {l, r, 0, l, 0};
    if (l == r) return;
    LL mid = l + r >> 1;
    build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
}

void modify(LL u, LL l, LL r, LL v)
{
    if (l <= tr[u].l && r >= tr[u].r)
    {
        tr[u].mx += v, tr[u].tag += v;
        return;
    }
    pushdown(u);
    LL mid = tr[u].l + tr[u].r >> 1;
    if (l <= mid) modify(u << 1, l, r, v);
    if (r > mid) modify(u << 1 | 1, l, r, v);
    pushup(u);
}

PLL query(LL u, LL l, LL r)
{
    if (l <= tr[u].l && r >= tr[u].r) return {tr[u].mx, tr[u].mxid};
    pushdown(u);
    LL mid = tr[u].l + tr[u].r >> 1;
    if (r <= mid) return query(u << 1, l, r);
    else if (mid < l) return query(u << 1 | 1, l, r);
    else
    {
        PLL t1 = query(u << 1, l, r), t2 = query(u << 1 | 1, l, r);
        return getmax(t1, t2);
    }
}

int main()
{
    LL n; scanf("%lld", &n);
    LL tot = 0;
    for (LL i = 1, l, r; i <= n; i ++ )
    {
        scanf("%lld%lld", &l, &r);
        LL x = l + 1, xx = r - 1, y = r + 1, yy = 1e9;
        if (x <= xx && y <= yy)
        {
            seg[ ++ tot] = {x, y, yy + 1, 1};
            seg[ ++ tot] = {xx + 1, y, yy + 1, -1};
            lsh[ ++ tott] = y, lsh[ ++ tott] = yy + 1;
        }
        x = 0, xx = l - 1, y = l + 1, yy = r - 1;
        if (x <= xx && y <= yy)
        {
            seg[ ++ tot] = {x, y, yy + 1, 1};
            seg[ ++ tot] = {xx + 1, y, yy + 1, -1};
            lsh[ ++ tott] = y, lsh[ ++ tott] = yy + 1;
        }
    }
    sort(seg + 1, seg + 1 + tot);
    sort(lsh + 1, lsh + 1 + tott);
    tott = unique(lsh + 1, lsh + 1 + tott) - lsh - 1;
    if (!tott)
    {
        printf("0 1\n");
        return 0;
    }
    build(1, 1, tott);
    PLL res = {-INF, 0}; LL resid = 0;
    for (LL i = 1; i <= tot; i ++ )
    {
        modify(1, find(seg[i].y), find(seg[i].yy) - 1, seg[i].op);
        PLL now = query(1, 1, tott);
        if (now.first > res.first) res = now, resid = i;
        else if (now.first == res.first && now.second < res.second && (!resid || seg[resid].x == seg[i].x)) res = now, resid = i;
    }
    printf("%lld %lld\n", seg[resid].x, lsh[res.second]);
    return 0;
}