BZOJ 4569: [Scoi2016]萌萌噠 倍增思維並查集

Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 1110 Solved: 528

Description

一個長度爲n的大數,用S1S2S3…Sn表示,其中Si表示數的第i位,S1是數的最高位,告訴你一些限制條件,每個條
件表示爲四個數,l1,r1,l2,r2,即兩個長度相同的區間,表示子串Sl1Sl1+1Sl1+2…Sr1與Sl2Sl2+1Sl2+2…S
r2完全相同。比如n=6時,某限制條件l1=1,r1=3,l2=4,r2=6,那麼123123,351351均滿足條件,但是12012,13
1141不滿足條件,前者數的長度不爲6,後者第二位與第五位不同。問滿足以上所有條件的數有多少個。

Input

第一行兩個數n和m,分別表示大數的長度,以及限制條件的個數。接下來m行,對於第i行,有4個數li1,ri1,li2
,ri2,分別表示該限制條件對應的兩個區間。
1≤n≤10^5,1≤m≤10^5,1≤li1,ri1,li2,ri2≤n;並且保證ri1-li1=ri2-li2。

Output

一個數,表示滿足所有條件且長度爲n的大數的個數,答案可能很大,因此輸出答案模10^9+7的結果即可。

Sample Input

4 2

1 2 3 4

3 3 3 3

Sample Output

90

HINT

Source


直接倍增思維並查集就可以了,對於每個大小的塊維護並查集即可,然後最後來pushdown,然後再計數就可以了,具體的可以看這篇博客 機房模擬賽

#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
long long p;
const long long mod = 1000000007LL;
const int MAXN = 100000;
int fa[20][ MAXN * 2 ], pow[ MAXN * 2 ], n, m, L, R, l, r, len;
long long fast_pow( long long x ) {
    long long BASE = 10LL, tmp = 1LL;
    while( x ) {
        if( x & 1 ) tmp = tmp * BASE % mod;
        x >>= 1;
        BASE = BASE * BASE % mod;
    }
    return tmp;
}
int find( int M, int x ) {
    if( fa[M][x] == x ) return x;
    else                return fa[M][x] = find( M, fa[M][x] );
}
void Union( int M, int x, int y ) {
    x = find( M, x ), y = find( M, y );
    if( x != y ) fa[M][x] = fa[M][y];
}
int main( ) {
    scanf( "%d", &n );
    for( register int i = 2; i <= n; i++ ) pow[i] = pow[ i >> 1 ] + 1;
    for( register int i = pow[n]; i >= 0; i-- ) for( register int j = 1; j <= n; j++ ) fa[i][j] = j;
    scanf( "%d", &m );
    while( m-- ) {
        scanf( "%d%d%d%d", &L, &R, &l, &r );
        int M = pow[ R - L + 1 ];
        Union( M, L, l );
        Union( M, R - ( 1 << M ) + 1, r - ( 1 << M ) + 1 );
    }
    for( register int j = pow[n]; j; j-- ) {
        for( register int i = 1; i + ( 1 << j ) - 1 <= n; i++ ) {
             int fat = find( j, i );
             Union( j - 1, i, fat );
             Union( j - 1, i + ( 1 << ( j - 1 ) ), fat + ( 1 << ( j - 1 ) ) );
        }
    } int tmp = 0;
    for( register int i = 1; i <= n; i++ ) 
        if( find( 0, i ) == i ) tmp++;
    printf( "%lld\n", 9LL * fast_pow( tmp - 1 ) % mod ); 
    return 0;
}

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章