个人做过的除了某 AC 自动机和大胃王以外题解最差的题,一堆题解水分大没思路没代码,剩下只有一个用 set 维护的 ODT,而且写法奇特,让人看的难受,另一篇讲的不错的还用的是 map……
虽然没写出来,但是还是先写一下某些细节:
- 先按顺序插入 set 后按时间排序,正确性已知,然后按依次枚举,如果 到 的路径上没有其他点,即特殊性质 B,就挨个枚举计算,大了就输出 No。再加上特殊性质 A,瞎写能得 40 分,如果有其他点阻挡,我们就像推土机直接往前推,这样 就推到 ,其他也是同理,所以就能得出柿子:
我们发现, 是严格的限制,我们可以利用 的方法让它变得不严格: 这样会更好做,然后我们定义 指第 到 个箱子,它们当前位置是 ,那么我们只需要找到 的 使它们的当前位置为 即 ,然后花费的时间就是 我们可以在枚举 set 时计算出 最后再算出总的再减去,或者挨个挨个累加,这里用后者。
当然最重要的是一个新发现:end() 返回的最后一个的后一个迭代器,但 begin() 返回的是第一个迭代器。而这道题要分类讨论,一个往右推,较好处理,一个往左推,不好处理。另外由于 erase(ls,rs) 不删 rs,所以往左推删除要用 next(rs) 跳到下一个迭代器。
其他的等我写完再说。
好了,我糖丸了,按 排序导致错一大堆,代码:
//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;
}
但是这串代码有很多不理解的地方:
- 为什么分裂要用 upper_bound()?
- 为什么从右往左删除要 upper_bound()?如果防止 re,那 re 的原因是什么?
只能下周来解决了。
OK,I’m back! 问题 1 得到了解决,其实从左往右不需要 upper_bound() 这需要用正常的 split,但是从右往左的分裂要有,具体看此图:
我们可以看到,按点序号搜索先搜比它大的最小值的区间,在往前一位就是该点所在的区间,然后分裂就刑了。 所以核心代码也可以写成:
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));
}
}当然如果要对称也可以这么写:
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。
