[SCOI2016] pyridazin solving report Mengmeng

[SCOI2016] pyridazin Mengmeng

The meaning of problems

Has a length \ (n-\) large numbers,

There \ (m \) th shaped like a l1,r1,l2,r2limit, representing the interval \ ([l1, r1] \ ) and \ ([l2, r2] \ ) exactly equal,

To meet these requirements the number of number of limitations, not leading zeroes.

\ ((1 \ n, m \ 10 ^ 5) \)


Thinking

Violence: Direct \ (O (nm) \) to equal the number of synthesis a disjoint-set, and finally if the number of disjoint-set to \ (K \) , the answer is \ (9 \ times 10 ^ { k-1} \) . (can not have leading zeros).


Consider optimization.

First, think of tree line optimization even edge, but found a tree line, it can not determine the correspondence between the interval.


In order to still be able to determine the edge even after correspondence between the interval, we need a storage structure for each has a point to it as a starting point.

So, we can get a similar thing ST table, but understanding manner similar to the tree line.

The entire sequence is divided into \ (\ log n \) layer, even for the edge of each layer.

In the given l1,r1,l2,r2time, with the operation similar to the ST table to find the best value, the \ ([l1, r1] \ ) and \ ([l2, r2] \ ) were divided into two length \ (2 \) of several square section, and then between these edges even intervals.

After all sides are connected, from the top down one layer at a downstream side even reflected in the code it is actually a merger of disjoint-set .


Note that, when the next pass, the current layer is provided as two sections connected \ (A, B \) , we also need to consider \ (A \) and \ (B \) subinterval, i.e., the next layer the four segments \ (A_1, A_2 \) and \ (B_1, B_2 \) of the connection,

If \ (A_1, A_2 \) is connected, put \ (fa [B_1], fa [B_2] \) is set to \ (FA [A_1] \) ,

Otherwise, put \ (fa [A_1], fa [A_2] \) is set to \ (FA [B_1] \) .


Code

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int _=1e5+7;
const int L=20;
const ll mod=1e9+7;
int n,m,fa[_][L+7],Log[_];
bool vis[_];
ll ans;
void init(){
  cin>>n>>m;
  int t=0;
  for(int i=1;i<=n;i++){
    if(i==1<<(t+1)) t++;
    Log[i]=t;
    for(int k=0;k<=L;k++)
      fa[i][k]=i;
  }
}
int find(int i,int k){ return fa[i][k]==i ?i :fa[i][k]=find(fa[i][k],k); }
void run(){
  int l1,r1,l2,r2;
  int maxn=0;
  for(int i=1;i<=m;i++){
    scanf("%d%d%d%d",&l1,&r1,&l2,&r2);
    maxn=max(maxn,max(r1,r2));
    int t=Log[r1-l1+1];
    int f1=find(l1,t),f2=find(l2,t);
    fa[f1][t]=f2;
    f1=find(r1-(1<<t)+1,t),f2=find(r2-(1<<t)+1,t);
    fa[f1][t]=f2;
  }

  for(int k=L;k>=1;k--){
    for(int i=1;i+(1<<k)-1<=n;i++){
      int t=find(i,k),t1=find(i,k-1),t2=find(i+(1<<(k-1)),k-1);
      if(t1==t2){ fa[find(t,k-1)][k-1]=fa[find(t+(1<<(k-1)),k-1)][k-1]=t1; }
      else{ fa[t1][k-1]=find(t,k-1); fa[t2][k-1]=find(t+(1<<(k-1)),k-1); }
    
    }
  }
  int j=find(1,0);
  ans=9; 
  for(int i=2;i<=n;i++)
    if(i!=j&&find(i,0)==i)
      ans=ans*10%mod;
}
int main(){
#ifndef ONLINE_JUDGE
  freopen("x.in","r",stdin);
#endif
  init();
  run();
  printf("%lld\n",ans);
  return 0;
}

Guess you like

Origin www.cnblogs.com/brucew-07/p/12194438.html