跳到正文

题解

题解:AT_abc360_f [ABC360F] InterSections

FXT1110011010OI约 8 分钟
原文
文章信息与版本

文章信息

发布
2024年7月1日
收录
2026年11月4日
更新
2026年11月13日
篇幅
约 8 分钟
获赞
12

版本

  1. v1当前

分析

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

  1. Li<l<Ri<rL_i < l < R_i < r,可以得出 Li+1≤l≤Ri−1,Ri+1≤r≤109L_i + 1 \le l \le R_i - 1, R_i + 1 \le r \le 10 ^ 9;
  2. l<Li<r<Ril < L_i < r < R_i,可以得出 0≤l≤Li−1,Li+1≤r≤Ri−10 \le l \le L_i - 1, L_i + 1 \le r \le R_i - 1。

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

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

upd 2024.07.02

被 hack 了。hack 数据:

输入

代码
3
2 10
8 10
5 8

输出

代码
0 6

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

代码已修复。

代码

C++
#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;
}
原文
存档评论9 条抓取于