题解:P10785 [NOI2024] 集合
rzh123
·
·
题解
对于一组询问 [l,r],x\in [1,m],设“出现位置集合” \text{posa}_{l,r,x}=\{i\in[l,r]\vert x\in A_i \}, \text{posb}_{l,r,x}=\{i\in[l,r]\vert x\in B_i \}。
排列 p 就是把每一个数 x 匹配到 p_x,如果 \text{posa}_{l,r,x}=\text{posb}_{l,r,y} 则 x 可以匹配到 y。
对于同一个 $l$,$r$ 越大则限制越严,一定存在一个分界线 $\text{tor}_l$,使得所有 $[l,\text{tor}_l]$ 的 $r$ 对应答案是 `Yes`,$(\text{tor},n]$ 的 $r$ 答案是 `No`,可以双指针求出每个 $\text{tor}_l$,$O(1)$ 回答每个询问。
考虑双指针过程中如何判断 $[l,r]$ 是否合法,需要支持:插入一个下标,删除一个下标,查询当前两个多重集是否等价,可以用和哈希判断。定义两个函数 $f,g$,$f$ 把下标映射成随机数,$g$ 把一个随机数映射成另一个随机数,$f,g$ 最好都是非多项式函数,用 $F(\text{pos})=\sum\limits_{i\in\text{pos}} f(i) \bmod P$ 表示一个出现位置集合,$G(T)=\sum\limits_{S\in T} g(F(S)) \bmod P$ 表示出现位置集合构成的多重集,如果两个多重集的 $G$ 相等则它们很大概率是相等的,模数可以直接用 `long long unsigned` 自然溢出,可以多随机几次判断,插入删除都比较简单。
时间复杂度 $O(n+q)$。
```cpp
#include <bits/stdc++.h>
using namespace std;
constexpr int N=2e5+5,M=6e5+5;
using llu=long long unsigned;
int n,m,q,tor[N];
int a[N][3],b[N][3];
mt19937_64 rnd(chrono::system_clock::now().time_since_epoch().count());
llu mp[N];
llu ap[M],bp[M],s1,s2;
llu msk;
llu shf(llu x){
if(!x) return x;
x^=msk,x^=x<<7,x^=x>>11,x^=x<<13;
return x;
}
void add(int i,int d){
for(int j:a[i]){
s1-=shf(ap[j]);
if(d>0) ap[j]+=mp[i];
else ap[j]-=mp[i];
s1+=shf(ap[j]);
}
for(int j:b[i]){
s2-=shf(bp[j]);
if(d>0) bp[j]+=mp[i];
else bp[j]-=mp[i];
s2+=shf(bp[j]);
}
}
int main(){
// freopen("set.in","r",stdin);
// freopen("set.out","w",stdout);
cin.tie(0)->sync_with_stdio(0);
cin>>n>>m>>q;
for(int i{1};i<=n;++i){
cin>>a[i][0]>>a[i][1]>>a[i][2];
}
for(int i{1};i<=n;++i){
cin>>b[i][0]>>b[i][1]>>b[i][2];
}
for(int i{1};i<=n;++i) tor[i]=n;
int tt=10;
while(tt--){
msk=rnd();
for(int i{1};i<=n;++i) mp[i]=rnd();
add(1,1);
for(int i{1},j{1};i<=n;add(i,-1),++i){
while(j<=n&&s1==s2){
++j;
if(j<=n) add(j,1);
}
tor[i]=min(tor[i],j-1);
}
}
while(q--){
int l,r; cin>>l>>r;
if(r<=tor[l]) cout<<"Yes\n";
else cout<<"No\n";
}
return 0;
}
```
赛时能过。