题解:P5360 [SDOI2019] 世界地图
高级。
一个想法是维护前后缀列的 MST,每次询问是利用
考虑减少一些边/点数。注意到所有前后缀状态下的 MST 只有最左和最右的两列在全过程中可能被合并到,因此我们考虑对这
因此考虑只保留所有的关键点,及两两关键点之间的最大边。这样的边数是
此时每一个前缀和后缀的 MST 上点边数均为
最后注意一条边不被保留并不意味他在答案中不出现,因此需要维护不在等效 MST 中的生成树边权和。
总复杂度
#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define repp(i,j,k) for(int i=j;i>=k;i--)
#define pii pair<int,int>
#define mp make_pair
#define fir first
#define sec second
#define ls(x) (x<<1)
#define rs(x) ((x<<1)|1)
#define lowbit(i) (i&-i)
#define qingbai qwq
using namespace std;
typedef long long ll;
const int N=105,M=10005,mo=998244353,inf=(ll)1e18+7;
const double PI=acos(-1);
unsigned int SA, SB, SC;int lim;
int getweight() {
SA ^= SA << 16;
SA ^= SA >> 5;
SA ^= SA << 1;
unsigned int t = SA;
SA = SB;
SB = SC;
SC ^= t ^ SA;
return SC % lim + 1;
}
int n,m,q,r[N][M],c[N][M];
void gen() {
scanf("%d%d%u%u%u%d", &n, &m, &SA, &SB, &SC, &lim);
int i, j, w;
for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++) {
r[i][j] = getweight();
}
for(i = 1; i < n; i++)
for(j = 1; j <= m; j++) {
c[i][j] = getweight();
}
}
#define int long long
void read(int &a){
int x=0,w=1;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-')w=-1;
ch=getchar();
}
while(isdigit(ch)){
x=(x<<3)+(x<<1)+ch-'0';
ch=getchar();
}
a=x*w;
}
struct edge{
int x,y,w;
friend bool operator<(edge x,edge y){
return x.w<y.w;
}
};
vector<edge>pre[M],suf[M],e;
int pres[M],sufs[M];
int getid(int x,int y){
return (x-1)*m+y;
}
struct bcj{
int fa[N*M];
void init(){
rep(i,1,n*m)
fa[i]=i;
}
int find(int x){
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
bool merge(int x,int y){
x=find(x),y=find(y);
if(x==y)return 0;
fa[x]=y;
return 1;
}
}B;
bool targ[N*M];
vector<edge>nwe,rese;
int mstmerge(){//返回没用的边权和
sort(nwe.begin(),nwe.end()),rese.clear();
int res=0;
for(auto j:nwe){
int x=j.x,y=j.y,w=j.w;
x=B.find(x),y=B.find(y);
if(x!=y){
if(targ[x]&&targ[y])rese.push_back((edge){x,y,w});
if(targ[x])swap(x,y);
B.merge(x,y);
}
else res+=w;
}
return res;
}
signed main(){
gen();
rep(j,1,m){//pre
nwe.clear();
rep(i,1,n)
B.fa[getid(i,j)]=getid(i,j),targ[getid(i,j)]=1,B.fa[getid(i,1)]=getid(i,1),targ[getid(i,1)]=1;
if(j>2){
rep(i,1,n)
B.fa[getid(i,j-1)]=getid(i,j-1),targ[getid(i,j-1)]=0;
}
rep(i,1,n){
if(j!=1)nwe.push_back((edge){getid(i,j-1),getid(i,j),r[i][j-1]}),pres[j]+=r[i][j-1];
if(i!=n)nwe.push_back((edge){getid(i,j),getid(i+1,j),c[i][j]}),pres[j]+=c[i][j];
}
for(auto i:pre[j-1])
nwe.push_back(i);
pres[j]+=pres[j-1]-mstmerge(),pre[j]=rese;
}
repp(j,m,1){//suf
nwe.clear();
rep(i,1,n)
B.fa[getid(i,j)]=getid(i,j),targ[getid(i,j)]=1,B.fa[getid(i,m)]=getid(i,m),targ[getid(i,m)]=1;
if(j<m-1){
rep(i,1,n)
B.fa[getid(i,j+1)]=getid(i,j+1),targ[getid(i,j+1)]=0;
}
rep(i,1,n){
if(j!=m)nwe.push_back((edge){getid(i,j),getid(i,j+1),r[i][j]}),sufs[j]+=r[i][j];
if(i!=n)nwe.push_back((edge){getid(i,j),getid(i+1,j),c[i][j]}),sufs[j]+=c[i][j];
}
for(auto i:suf[j+1])
nwe.push_back(i);
sufs[j]+=sufs[j+1]-mstmerge(),suf[j]=rese;
}
cin>>q;
while(q--){
int le,ri;
read(le),read(ri);
int ans=pres[le-1]+sufs[ri+1];
nwe.clear();
rep(i,1,n){
B.fa[getid(i,1)]=getid(i,1),B.fa[getid(i,m)]=getid(i,m),targ[getid(i,1)]=0,targ[getid(i,m)]=0;
if(le-1!=1)B.fa[getid(i,le-1)]=getid(i,le-1),targ[getid(i,le-1)]=0;
if(ri+1!=m)B.fa[getid(i,ri+1)]=getid(i,ri+1),targ[getid(i,ri+1)]=0;
}
rep(i,1,n)
ans+=r[i][m],nwe.push_back((edge){getid(i,1),getid(i,m),r[i][m]});
for(auto j:suf[ri+1])
nwe.push_back(j);
for(auto j:pre[le-1])
nwe.push_back(j);
ans-=mstmerge();
printf("%lld\n",ans);
}
return 0;
}