题解:P10573 [JRKSJ R8] C0mp0nents
题解
困难题。
考虑所有
假设我们现在要求
拓展到一般情况,如果
- 每个
x\in(s,r) 都向后至少连了一条边到(x,r] ; - 每个
x\in(l,s) 都向前至少连了一条边到[l,x) ; -
我们可以先处理前两个限制,对于最后一个判断是否有边从
现在说第一个限制的处理方式。因为我们的
最后我们还需要判断区间之间是否有边。我们转换视角,考虑每条边对哪一段区间有贡献。对于一条边
代码
void solve(int rr){
tot = n / k + (n % k >= rr); lp[1] = 1; rp[tot] = tot; sf[0] = 0; pr[tot + 1] = tot + 1;
for(int i = 1; i <= tot; ++i)pos[a[i] = i * k - k + rr] = i, d[i] = 0;
for(int u = 1; u <= tot; ++u){
l[u] = 0, r[u] = tot + 1;
for(int i = hd[a[u]], v; i; i = e[i].nxt)if((v = pos[e[i].to]) < u)l[u] = max(l[u], v); else r[u] = min(r[u], v);
}
for(int i = 2; i <= tot; ++i){lp[i] = lp[i - 1]; if(l[i - 1] < lp[i] and lp[i] ^ i - 1)lp[i] = i - 1;}
for(int i = tot - 1; i; --i){rp[i] = rp[i + 1]; if(rp[i] < r[i + 1] and rp[i] ^ i + 1)rp[i] = i + 1;}
for(int i = 1; i <= tot; ++i)for(sf[i] = sf[i - 1]; sf[i] < tot and lp[sf[i] + 1] <= i; ++sf[i]);
for(int i = tot; i; --i)for(pr[i] = pr[i + 1]; pr[i] > 1 and i <= rp[pr[i] - 1]; --pr[i]);
for(int u = 1; u <= tot; ++u)for(int i = hd[a[u]], v; i; i = e[i].nxt)if(u < (v = pos[e[i].to])){
int bg = max(u + 1, pr[v]), ed = min(v - 1, sf[u]);
if(bg <= ed)++d[bg], --d[ed + 1];
}
for(int i = 1; i <= tot; ++i){
d[i] += d[i - 1];
if(l[i] < lp[i] and rp[i] < r[i])lp[i] = rp[i] = i;
else if(! d[i])if(l[i] < lp[i])lp[i] = i; else if(rp[i] < r[i])rp[i] = i;
ans[a[i]] = rp[i] - lp[i] + 1;
}
}