ICPCCamp 2016 Day 2 - ZhejiangU Contest(Easy When You Know How-并查集+rmq)

题意:给一个长度为n的字符串和m个事实,每个事实是 xi,yi,li 表示

sxi,sxi+1,,sxi+li1=syi,syi+1,,syi+li1

已知这个字符串只有小写字母,问可能的字符串个数。
1n,m200000

答案是 26t 是个人都看得出

暴力 O(nm)
还是建立并查集,
把询问 (xi,yi,li) 拆成 (xi,yi,2k),(xi+li2k,yi+li2k,2k)
从大到小遍历k
并查集上的点i表示substring (xi,2k) ,最多处理2m次合并,
然后计算出k-1的情况下的并查集,处理剩余询问,
O(mlogn)

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <functional>
#include <cstdlib>
#include <queue>
#include <stack>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p]) 
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007LL)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
typedef long long ll;
typedef unsigned long long ull;
#define MAXN (200000+10)
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
int n,m,x[MAXN],y[MAXN],l[MAXN];
int fa[MAXN],fa2[MAXN];
void init() {
    For(i,n) fa[i]=i;
}
int get(int x,int *Fa=fa) {
    if (Fa[x]==x) return x;
    return Fa[x]=get(Fa[x],Fa);
}
int uni(int x,int y,int *Fa = fa) {
    if (get(x,Fa)==get(y,Fa)) return 0;
    Fa[Fa[y]]=Fa[Fa[x]]; return 1;
}
class ask{
public:
    int x,y,l;
    ask(){}
    ask(int _x,int _y,int _l):x(_x),y(_y),l(_l){}
}a[MAXN];
int cmp1(ask a,ask b) {
    if (a.l==b.l) return a.x<b.x;
    return a.l<b.l;
}
int main() {
    //freopen("B.in","r",stdin);
    while(cin>>n>>m) {
        For(i,m) {
            x[i]=read(); y[i]=read(); l[i]=read(); 
            a[i]=ask(x[i],y[i],l[i]);
        }
        int t=1;while (t*2<=n) t*=2;
        init(); 
        sort(a+1,a+1+m,cmp1);
        while(t) {
            int p=1;
            ForD(i,m) {
                if (a[i].l>=t) {
                    uni(a[i].x,a[i].y);
                    if (a[i].l>t) uni( a[i].x+a[i].l-t  ,a[i].y+a[i].l-t );
                } else {
                    p=i+1;break;
                }
            }
            m=p-1;

            For(i,n) fa2[i]=fa[i];
            For(i,n-t+1) if (get(i)!=i) {
                uni(fa[i]+t/2,i+t/2,fa2);
            }
            For(i,n) fa[i]=fa2[i];
            t/=2;
        }
        ll ans=1;
        For(i,n) if (fa[i]==i) ans=(ans*26)%F;
        cout<<ans<<endl;
    }
    return 0;
}

你可能感兴趣的:(ICPCCamp 2016 Day 2 - ZhejiangU Contest(Easy When You Know How-并查集+rmq))