跳到正文

个人记录

P11833 推箱子 做题记录

sample_mean_VVV约 12 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 12 分钟

版本

  1. v1当前

个人做过的除了某 AC 自动机和大胃王以外题解最差的题,一堆题解水分大没思路没代码,剩下只有一个用 set 维护的 ODT,而且写法奇特,让人看的难受,另一篇讲的不错的还用的是 map……

虽然没写出来,但是还是先写一下某些细节:

  1. 先按顺序插入 set 后按时间排序,正确性已知,然后按依次枚举,如果 aia_i 到 bib_i 的路径上没有其他点,即特殊性质 B,就挨个枚举计算,大了就输出 No。再加上特殊性质 A,瞎写能得 40 分,如果有其他点阻挡,我们就像推土机直接往前推,这样 i+1i+1 就推到 bi+1b_i +1,其他也是同理,所以就能得出柿子:
ai+k−>bi+kai+k−(i+k)−>bi−i\begin{aligned} a_{i+k}&->b_i +k\\ a_{i+k}-(i+k)&->b_i-i \end{aligned}

我们发现,bi<bi+1b_i<b_{i+1} 是严格的限制,我们可以利用 −i-i 的方法让它变得不严格:bi−i≤bi+1−i−1b_i-i\le b_{i+1}-i-1 这样会更好做,然后我们定义 l,r,w{l,r,w} 指第 ll 到 rr 个箱子,它们当前位置是 ww,那么我们只需要找到 ai≤w<bia_i\le w < b_i 的 l≤i≤rl\le i\le r 使它们的当前位置为 bib_i 即 w=biw=b_i,然后花费的时间就是 (r−l+1)∗(bi−w)(r-l+1)*(b_i-w) 我们可以在枚举 set 时计算出 (r−l+1)∗w(r-l+1)*w 最后再算出总的再减去,或者挨个挨个累加,这里用后者。

当然最重要的是一个新发现:end() 返回的最后一个的后一个迭代器,但 begin() 返回的是第一个迭代器。而这道题要分类讨论,一个往右推,较好处理,一个往左推,不好处理。另外由于 erase(ls,rs) 不删 rs,所以往左推删除要用 next(rs) 跳到下一个迭代器。

其他的等我写完再说。

好了,我糖丸了,按 ll 排序导致错一大堆,代码:

C++
//Just Sayori
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <vector>
#define ll long long
#define rnt register int
#define gr getchar
#define pr putchar
#define N 200005
#define M 1000000007
using namespace std;
inline ll read()
{
    ll x = 0, f = 1;
    char ch = gr();
    while (ch < '0' || ch > '9') ch == '-' ? f = -1, ch = gr() : ch = gr();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = gr();
    return x * f;
}
inline void write(ll x, bool k)
{
    static int sta[39], top = 0;
    if (x < 0) pr('-'), x = -x;
    do sta[++top] = x % 10, x /= 10;
    while (x);
    while (top) pr(sta[top--] + 48);
    k ? pr(32) : pr(10);
}
struct node
{
    ll l, r, t, id;
    bool operator <(const node o)const
    {
        return t < o.t;
    }
} a[N];
struct ODT
{
    ll l, r;
    mutable ll w;
    bool operator <(const ODT o)const
    {
        return l < o.l;
    }
    ODT(ll ls = 0, ll rs = 0, ll ws = 0)
    {
        l = ls, r = rs, w = ws;
    }
};
set<ODT> odt;
int n, m, l, r, w, c, t;
ll ans;
bool f;
void assign(ll i)
{
    int id = a[i].id;
    auto now = --odt.upper_bound(ODT(a[i].id));
    ll pos, l = now->l, r = now->r, w = now->w;
    if (a[i].r > a[i].l)
    {
        if (l ^ id)//判断是否相等
        {
            odt.erase(now), odt.insert(ODT(l, a[i].id - 1, w));
            now = odt.insert(ODT(a[i].id, r, w)).first;
        }
        auto rs = now;
        pos = id - 1;
        while (rs != odt.end() && rs->w < a[i].r)
            ans += (rs->r - rs->l + 1) * 1ll * (a[i].r - rs->w), pos = rs->r, rs++;
        odt.erase(now, rs);
        odt.insert(ODT(id, pos, a[i].r));
    }
    else
    {
        if (r ^ id)
        {
            odt.erase(now), odt.insert(ODT(a[i].id + 1, r, w));
            now = odt.insert(ODT(l, a[i].id, w)).first;
        }
        auto ls = now;
        pos = id + 1;
        while (ls->w > a[i].r)
        {
            ans += (ls->r - ls->l + 1) * 1ll * (ls->w - a[i].r), pos = ls->l;
            if (ls == odt.begin()) break;
            ls--;
        }
        odt.erase(--odt.upper_bound(ODT(pos)), next(now));
        odt.insert(ODT(pos, id, a[i].r));
    }
}
int main()
{
    c = read(), t = read();
    while (t--)
    {
        n = read(), f = 0, ans = 0, odt.clear();
        for (rnt i = 1; i <= n; i++)
            a[i] = {read() - i, read() - i, read(), i};
        for (rnt i = 1; i <= n; i++) odt.insert(ODT(i, i, a[i].l));
        sort(a + 1, a + n + 1);
        for (rnt i = 1; i <= n; i++)
        {
            assign(i);
            if (ans > a[i].t)
            {
                puts("No"), f = 1;
                break;
            }
        }
        if (!f) puts("Yes");
    }
    return 0;
}

但是这串代码有很多不理解的地方:

  1. 为什么分裂要用 upper_bound()?
  2. 为什么从右往左删除要 upper_bound()?如果防止 re,那 re 的原因是什么?

只能下周来解决了。

OK,I’m back! 问题 1 得到了解决,其实从左往右不需要 upper_bound() 这需要用正常的 split,但是从右往左的分裂要有,具体看此图:

我们可以看到,按点序号搜索先搜比它大的最小值的区间,在往前一位就是该点所在的区间,然后分裂就刑了。 所以核心代码也可以写成:

C++
auto split1(ll x)
{
    auto now = odt.lower_bound(ODT(x));
    if (now != odt.end() && now->l == x) return now;
    now--;
    if (now->r < x) return odt.end();
    ll l = now->l, r = now->r, w = now->w;
    odt.erase(now);
    odt.insert(ODT(l, x - 1, w));
    return odt.insert(ODT(x, r, w)).first;
}
auto split2(ll x)
{
    auto now = odt.upper_bound(ODT(x));
    now--;
    ll l = now->l, r = now->r, w = now->w;
    odt.erase(now);
    odt.insert(ODT(x + 1, r, w));
    return odt.insert(ODT(l, x, w)).first;
}
void assign(ll i)
{
    int id = a[i].id;
    ll pos;
    if (a[i].r > a[i].l)
    {
        auto now = split1(a[i].id);
        auto rs = now;
        pos = id - 1;
        while (rs != odt.end() && rs->w < a[i].r)
            ans += (rs->r - rs->l + 1) * 1ll * (a[i].r - rs->w), pos = rs->r, rs++;
        odt.erase(now, rs);
        odt.insert(ODT(id, pos, a[i].r));
    }
    else
    {
        auto now = split2(a[i].id);
        auto ls = now;
        pos = id + 1;
        while (ls->w > a[i].r)
        {
            ans += (ls->r - ls->l + 1) * 1ll * (ls->w - a[i].r), pos = ls->l;
            if (ls == odt.begin()) break;
            ls--;
        }
        odt.erase(--odt.upper_bound(ODT(pos)), next(now));
        odt.insert(ODT(pos, id, a[i].r));
    }
}

当然如果要对称也可以这么写:

C++
auto split2(ll x)
{
    auto now = odt.lower_bound(ODT(x));
    if (now != odt.end() && now->l == x)
    {
        ll l = now->l, r = now->r, w = now->w;
        odt.erase(now);
        odt.insert(ODT(l + 1, r, w));
        return odt.insert(ODT(l, l, w)).first;
    }
    now--;
    ll l = now->l, r = now->r, w = now->w;
    odt.erase(now);
    odt.insert(ODT(x + 1, r, w));
    return odt.insert(ODT(l, x, w)).first;
}

不过越这么写跑的越慢,belike。

原文