Fish in troubled waters

Title Description

Pangtou is fishing in troubled waters like a fish.

He often went to the river to catch fish, every day, he would choose a continuous period of time to catch fish, there are many different species of fish in the river, each point in time there will be just a fish appear, and will be able to touch the fish Pangtou (if he is fishing in troubled waters words).

After touching the fish, he would touch the fish chronological palpable in a row, statistical returns today to catch fish, because the fish he is blind, he can tell which fish are the same species, and will not know in the end is what kind of a fish, so he put the fish in accordance with the kind of positive integer numbers, use the same numbers of the same fish, different fish with different labels, smart Pangtou will choose the lexicographically smallest label method.

Pangtou catch fish found at different times may have the same benefits, two different gains if and only if a different number of touch or the presence of a fish $ $ i $ i such that a first touch $ different numbers of fish .

Pangtou can begin to goof off at any time, at any time of the end of fishing in troubled waters, but will touch at least one fish. He wants you to count him a number of different possible total return. (He must only select a continuous period of time fishing in troubled waters).

data range

$ 1 \ The a_i \ k \ 5 \ Times 10 ^ 4 $

answer

If we can consider each suffix with a minimum out, and then sort the answer is $ \ frac {n \ times (n + 1)} {2} - \ sum lcp (str_i, str_ {i + 1}) $ .

Consider $ the hash $ expressed as $ \ sum (next_i-i) \ times p ^ i $, where the same $ next_i $ to and $ I $ next position number, if not, it is set to $ I $, that the same as the hash $ $ crosstalk represented by two minimum which is the same can be reflected.

Consider $ hash $ substring of the value of maintenance with the President of trees, and then sort when half the $ hash $ (two Kazakh analysis) can be.

Efficiency: $ (nlog ^ 3n) O $

Code

#include <bits/stdc++.h>
#define LL long long
#define U unsigned long long
using namespace std;
const int N=5e4+5,M=2e6+5;
const U B=793999;
int n,a[N],ls[M],rs[M],T[N],t,p[N],nx[N];
LL ans; U s[M],b[N]; set<int>S[N];
#define mid ((l+r)>>1)
void upd(int &x,int y,int l,int r,int v,U w){
    x=++t;s[x]=s[y]+w;
    ls[x]=ls[y];rs[x]=rs[y];
    if (l==r) return;
    if (mid>=v) upd(ls[x],ls[y],l,mid,v,w);
    else upd(rs[x],rs[y],mid+1,r,v,w);
}
U qry(int x,int l,int r,int L,int R){
    if (L<=l && r<=R) return s[x];
    if (mid>=R) return (ls[x]?qry(ls[x],l,mid,L,R):0);
    if (mid<L) return (rs[x]?qry(rs[x],mid+1,r,L,R):0);
    return (ls[x]?qry(ls[x],l,mid,L,R):0)+(rs[x]?qry(rs[x],mid+1,r,L,R):0);
}
U hs(int l,int r){return (T[l]?qry(T[l],1,n,l,r):0);}
int lcp(int x,int y){
    int l=0,r=n-max(x,y)+1;
    while(l<r){
        int i=(l+r+1)>>1;
        if (hs(x,x+i-1)*b[x]==hs(y,y+i-1)*b[y]) l=i;
        else r=i-1;
    }
    return l;
}
int Pos(int x,int i){
    return (*S[a[i]].lower_bound(x))-x;
}
bool cmp(int x,int y){
    int l=lcp(x,y);
    if (x+l>n) return 1;if (y+l>n) return 0;
    return Pos(x,x+l)<Pos(y,y+l);
}
int main(){
    cin>>n;b[0]=1;
    for (int i=1;i<=n;i++)
        scanf("%d",&a[i]),b[i]=b[i-1]*B,
        p[i]=i,S[a[i]].insert(i);
    for (int i=n;i;i--){
        T[i]=T[i+1];
        if (nx[a[i]]) upd(T[i],T[i],1,n,
            nx[a[i]],b[n-i+1]*(nx[a[i]]-i));
        nx[a[i]]=i;
    }
    stable_sort(p+1,p+n+1,cmp);ans=1ll*n*(n+1)/2;
    for (int i=1;i<n;i++) ans-=lcp(p[i],p[i+1]);
    printf("%lld\n",ans);return 0;
}

Guess you like

Origin www.cnblogs.com/xjqxjq/p/12047248.html