ABC462D 题解

· · 题解

也许更好的阅读体验。

赛时用树状数组水过去了,但是非常不喜欢数据结构。严肃学习结论化做法。

观察到对于时间 l,r,实际上作案的 d 时间内是不可选的,也就是说实际可选的作案时间是 l 到 r-d。

然后注意到作案时间是 10^6 以内的,这启示我们枚举作案开始的时间。设 f_i 是时刻 i 在房间里的候选人,那就可以组合数 C^2_{f_i},也就等同于 \frac{(f_i-1)\times f_i}{2}。

如何求 f_i 呢?其实只需要差分就可以了。一段时间映射到差分数组上,也就是 c_l 加一,c_{r-d+1} 减一。

::::info[另:赛时群里的批话] 如果出现人物不想出现的可以让我删除。

::::success[AC 代码]

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e6 + 10;
const int MOD = 998244353;
ll n, d, ans, c, f[N];
int main() {
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> n >> d;
    for (int i = 0, s, t; i < n; i++) {
        cin >> s >> t;
        ll r = t - d;
        if (r >= s) {
            f[s]++, f[r + 1]--;
        }
    }
    for (int x = 1; x <= 1e6; x++) {
        c += f[x];
        ans += c * (c - 1) / 2;
    }
    cout << ans;
    return 0;
}

::::

都看到这里了还不点赞,那还是人吗?