Luogu P9100 [PA 2020] Miny 题解
模拟赛考到这个题了,场切了,发现做法跟大部分题解有点不同,写一篇题解。
题意概括
有 枚地雷,地雷按照位置从左到右编号依次为 ~ ,地雷的爆炸位置和爆炸半径分别为 。
手动引爆若干地雷后,爆炸会不断传播。需要统计最终爆炸地雷集合有多少种。
答案对 取模。
思路
任意一个爆炸结果都可以被划分为唯一的若干互不相邻的连续段,例如:
对应两个连续爆炸段 。
由于我们划分的连续段是一个完整的段,所以对于一个连续段内的每个地雷,我们都不能影响到其他的地雷。
所以我们考虑 dp。我们设 表示只考虑前 个地雷,** 其他的都不做限制的情况下(与其他做法不同的地方)** 的总方案数:
-
如果地雷 不爆炸,我们有 。
-
如果地雷 爆炸。我们每次去枚举上一个连续段 ,因为地雷编号为 的地雷发生了爆炸,要使这个连续段合法,必须有地雷 不能爆炸。
所以我们有转移 。
所以我们的转移方程为:
现在的问题变成,对于每个右端点 ,我们怎么快速找到所有的合法起点 。
快速转移
1. 如何判定连续爆炸段是否合法
对于一个连续爆炸段 ,我们设地雷 可以左右经过连锁反应引爆的地雷区间为 。
显然,一个区间合法当且仅当:
2. 快速求出每个合法的左端点
考虑条件 1:
性质 1:一个左端点 合法当且仅当 。
证明:如果 ,那么 被引爆后,区间 也会被引爆,两个区间发生相交。
所以我们扫描到 ,当且仅当 ,我们才把它当做候选的左端点。
那么什么时候这个左端点 不合法呢?
显然,当我们引爆的区间会引爆 左边的地雷时, 会不合法。
即扫描到位置 时, 时, 在之后的转移中都不会合法。
因为加入地雷的编号是单调递增的,所以,对于条件 1,我们可以使用单调栈快速维护。
考虑条件 2:
我们设 , 为集合 中最大的数。
人话, 为能影响到引爆 地雷右边的地雷中最靠右的地雷。
显然,我们的区间不能包含这个地雷 。
我们我们只需要我们的左端点 满足 ,就一定满足条件 2。
所以,我们需要查询单调栈中所有满足 ,即 的和,这个值就是我们转移方程的后面那一坨。
对于求和,我们可以使用树状数组维护;对于求 ,我们可以使用优先队列维护。
3. 如何快速求出 和
我们可以先二分求出直接影响的 ,然后直接线段树优化建图求 SCC。
这样做的时间复杂度和空间复杂度都是 的。
ac code
无。
为什么我不会写这种做法呢,因为这个做法要写线段树优化建图。
我们重新思考一下 。
求他们有必要吗,如果直接影响的区间 超出了范围,那么 也一定超出了范围。
感性理解一下,其实上面的所有做法用直接影响的区间 去做是等价的。
这样做我们可以将空间复杂度降为 。
ac code
#include<bits/stdc++.h>
#define endl '\n'
#define pii pair<int, int>
#define fr first
#define sc second
#define debug "-------------------\n"
#define rep(I, A, B) for (int I = (A); I <= (B); I++)
#define dwn(I, A, B) for (int I = (A); I >= (B); I--)
#define CleanIce __int128
#define bit_count(a) __builtin_popcount(a)
#define int long long
using namespace std;
const int INF = 0x3f3f3f3f3f3f3f3f, mod = 998244353;
int add(int a, int b){ if(a >= mod) a %= mod; if(b >= mod) b %= mod; a += b; if(a >= mod) a %= mod; return a;}
int sub(int a, int b){ a -= b; if(a < 0) a += mod; return a;}
int mul(int a, int b){ if(b >= mod) b %= mod; if(a >= mod) a %= mod; a *= b; if(a >= mod) a %= mod; return a;}
void Min(int &a, int b){ if(b < a) a = b;}
void Max(int &a, int b){ if(b > a) a = b;}
template <typename T>
inline void read(T &x) {
x = 0; char c = getchar();
int f = 0;
for (; !isdigit(c); c = getchar()) f |= c == '-';
for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
if (f) x = -x;
}
template <typename T, typename ...Args>
inline void read(T &x, Args &...args) {
read(x), read(args...);
}
template <typename T>
inline void write(T x, char ch) {
if (x < 0) x = -x, putchar('-');
static short st[70], tp;
do st[++tp] = x % 10, x /= 10; while (x);
while (tp) putchar(st[tp--] | 48);
putchar(ch);
}
template <typename T, typename ...Args>
inline void write(T x, char ch, Args ...args) {
write(x, ch), write(args...);
}
const int N = 300000 + 5, P = 1000000007;
int a[N], r[N], l[N], rr[N];
int dp[N], bit[N];
int st[N], w[N], tp;
priority_queue<int> q;
void upd(int x, int v){
if(v < 0) v += P;
for(; x < N; x += x & -x){
bit[x] += v;
if(bit[x] >= P) bit[x] -= P;
}
}
int ask(int x){
int res = 0;
for(; x; x -= x & -x){
res += bit[x];
if(res >= P) res -= P;
}
return res;
}
int ask(int l, int r){
int res = ask(r) - ask(l - 1);
if(res < 0) res += P;
return res;
}
void solve(){
int n;
read(n);
rep(i, 1, n) read(a[i], r[i]);
rep(i, 1, n){
l[i] = lower_bound(a + 1, a + n + 1, a[i] - r[i]) - a;
rr[i] = upper_bound(a + 1, a + n + 1, a[i] + r[i]) - a - 1;
}
dp[0] = 1;
rep(i, 1, n){
while(tp && st[tp] > l[i]){
upd(st[tp], -w[tp]);
--tp;
}
if(l[i] == i){
int v = dp[max(0LL, i - 2)];
st[++tp] = i;
w[tp] = v;
upd(i, v);
}
if(rr[i] > i) q.push(i);
while(!q.empty() && rr[q.top()] <= i) q.pop();
int p = q.empty() ? 1 : q.top() + 1;
dp[i] = dp[i - 1] + ask(p, i);
if(dp[i] >= P) dp[i] -= P;
}
write(dp[n], '\n');
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// ios::sync_with_stdio(0);
// cin.tie(0);
int t_ = 1;
while(t_--) solve();
return 0;
}
/*
g++ "-Wl,--stack,536870912" -O2 -std=c++14 -static -Wall "xxx.cpp"
*/