这是一个比较清楚严谨,思维不跳跃,不过略为繁琐的做法。
对所有值离散化。
先考虑只有 。
我们可以想到这样的 dp,令 表示前 个人,山的高度为 时,最大的已爬上人数。
当然若 不为 就是直接继承。
简单地,我们可以把 直接滚动掉。
然后,里面的这个 max 也可以优化,我们可以在任何时候把当前山的高度增加。因为我们可以搞一些不对答案产生贡献的人来爬山,他们的 和 是任意的。所以令 表示山的高度不超过 时的答案。 可以如下转移。
其实就是这样一个 DAG,每个高度作为一个节点, 向 连贡献为 的边, 向 连贡献为 的边。在这个 DAG 上面求最长路。
现在我们把 加入考虑。
显然,如果当前山的高度在 内,立刻让这个人去爬肯定是对的。而山又是可以随时增高的,所以可以视作只让这些人在这个高度区间去爬山。 用 DAG 来理解,就是先走若干零边走到区间内,然后获得一点贡献。
所以,从 转移向 时,如果 处于某个区间,而 不处于,就让这一种转移路径获得额外一点贡献。
表示满足 的 对的数量。这个东西是可以使用二维数点来做。
下面是代码:
#include <bits/stdc++.h>
#define cyn cin
using namespace std;
const int N = 500010;
int a[N], s[N], w[2 * N];
vector <int> to[2 * N];
int dp[2 * N], it, cu, cnt;
unordered_map <int, int> mp;
class ECH
{
public:
int x, y;
friend bool operator < (ECH a, ECH b)
{
return a.y > b.y;
}
} e[N];
class QRY
{
public:
int l, r, id, ans;
QRY () {}
QRY (int il, int ir, int cu)
{
l = il;
r = ir;
id = cu;
}
friend bool operator < (QRY a, QRY b)
{
return a.id < b.id;
}
} q[2 * N + N];
bool cmp(QRY x, QRY y)
{
return x.r > y.r;
}
int sum[2 * N];
void add(int x)
{
for (; x <= cnt; x += x & -x)
sum[x]++;
}
int ans(int x)
{
int r = 0;
for (; x; x -= x & -x)
r += sum[x];
return r;
}
void slv()
{
sort(e + 1, e + 1 + it);
sort(q + 1, q + cu, cmp);
int ce = 0;
for (int i = 1; i <= cu; i++)
{
while (ce < it && e[ce + 1].y >= q[i].r)
ce++,
add(e[ce].x);
q[i].ans = ans(q[i].r) - ans(q[i].l);
}
sort(q + 1, q + cu);
}
int main()
{
ios::sync_with_stdio(0);
cyn.tie(0);
cout.tie(0);
int n, d;
cyn >> n >> d;
w[0] = d;
for (int i = 1; i <= n; i++)
{
cyn >> s[i] >> a[i];
if (s[i] < d)
{
n--;
i--;
continue;
}
if (a[i] < d)
a[i] = d;
w[2 * i - 1] = s[i];
w[2 * i] = a[i];
}
sort(w, w + 1 + n + n);
mp[w[0]] = 1;
cnt = 1;
for (int i = 1; i <= 2 * n; i++)
if (w[i] > w[i - 1])
mp[w[i]] = ++cnt;
for (int i = 1; i <= n; i++)
s[i] = mp[s[i]],
a[i] = mp[a[i]];
for (int i = 1; i <= n; i++)
if (s[i] < a[i])
to[a[i]].push_back(s[i]);
else
it++,
e[it].x = a[i],
e[it].y = s[i];
cu = 1;
q[cu] = QRY(0, 1, cu);
cu++;
for (int i = 2; i <= cnt; i++)
{
for (auto& j : to[i])
q[cu] = QRY(j, i, cu),
cu++;
q[cu] = QRY(i - 1, i, cu);
cu++;
}
slv();
cu = 1;
dp[1] = q[cu].ans;
cu++;
for (int i = 2; i <= cnt; i++)
{
for (auto& j : to[i])
dp[i] = max(dp[i], dp[j] + 1 + q[cu].ans),
cu++;
dp[i] = max(dp[i], dp[i - 1] + q[cu].ans);
cu++;
}
cout << dp[cnt] << endl;
return 0;
}