And wrong of rehabilitation training -bzoj5253: loj2479: p4384: [2018 several provinces exam] Hu channeling system

Subject to the effect

Given a string \ (S \) , length \ (n-\) ( \ (n-\ Leq. 5 ^ 10 \) ), \ (S [L: R & lt] \) represents \ (S_l, S_ {l + 1} ..., S_r \) of this string. There \ (m \) ( \ (m \ Leq. 3 \ ^ 10. 5 Times \) ) interrogation times, each time given query \ (L, R & lt \) , asked how many of the \ ((i, j) \ ) ( \ (. 1 \ Leq I <I +. 1 <J \ Leq n-\) ), so that the \ (S [l: r] \) essentially the same sub-strings found in \ (S [1: i] \) of or \ (S [i + 1: j-1] \) or \ (S [j: n] \) in.

answer

Q is asked how many equivalent program \ (S \) in two to find a slit cut twice, twice so as not to overlap, and the presence of a \ (S [l: r] \) essentially the same substring is not cut.
Consider the more difficult, it can be converted into the (n-1 \) \ slits total two programs selected number - all \ (S [l: r] \) essentially the same sub-strings are cut scheme number.
All \ (S [l: r] \) essentially the same sub-strings are cut in the number of programs considered the following cases:
a, \ (S \) there are three in the \ (S [l: r] \) essentially the same substring, and wherein each of the two intersection size less than 1
will find how this time are cut twice incomplete cut three strings, the program number 0.
Two, \ (S \) does not exist in the three \ (S [l: r] \) essentially the same substring, and wherein each of the two intersection size not more than 1: Assuming that all such leftmost substring the left point \ (LL \) , the left-most right point \ (LR \) , the right-most left point \ (RL \) , the right-most right point \ (rr \) , \ (S [ l: r] \) of length \ (len \) ,\ (r_i \) represents the right set \ (I \) a.
\ (\ space \ space \ space \ Space \) 1. \ (S \) in all \ (S [l: r] \) essentially the same substring intersection is not empty
\ (\ space \ space \ space \ Space \) \ (\ Space \ Space \ Space \ Space \) (. 1) do not cut into the front of the knife and \ (S [l: r] \) essentially the same substring: another knife must cut all such substring intersection \ ([rl, lr] \ ) on. This part of the answer = \ (LL \ Times (LR-RL) \) . Note that only the knife slit cut in, it is \ ((rl-lr) \ ) instead of \ ((rl is an LR-+. 1) \) .
\ (\ Space \ Space \ Space \ Space \) \ (\ Space \ Space \ Space \ Space \) (2) forward across the board to some of \ (S [l: r] \) essentially the same substring, but did not cut at all \ (S [l: r] \) on essentially the same substring intersection: sure to find the second blade in the first cutting blade to cut no substring intersection. So for the two solutions across the board when they are of the same string, the second blade feasible values in the same range. The first two programs are knife\ ([r_i-len + 1 , r_ {i + 1} -len + 1) \) , they are cut string \ ([1, i] \ ) in this interval, which second blade is taken value ranges are \ ([rl is an,. 1 + R_ {I}] \) . That is, this part of the answer = \ (\ SUM (I + R_ {} -r_i. 1) \ Times (+ R_ {I} -rl. 1) \)
\ (\ Space \ Space \ Space \ Space \) \ ( \ Space \ Space \ Space \ Space \) (. 3) to the front of the one size fits all \ (S [l: r] \) on essentially the same substring intersection: in fact, this part (1) is merged with the " present all across the board \ (S [l: r] \) where essentially the same substring of intersection, but not the front of the knife (2). " Number of programs are cut twice on the intersection is \ (LR-rl is an C_ {2} ^ \) . For the case where only the intersection across the board, wherein the knife can not be equivalent to ([lr-len + 1, lr] \) \ , the other knife must be \ ([lr, rl] \ ) a, so this part is \ ((n--len) \ Times (LR-rl is an) \) . That is to say (1) and (3) a total of \ (LR-rl is an C_ {} ^ 2 + (n--len) \ Times (LR-rl is an) \) .
\ (\ Space \ Space \ Space \ Space \) 2. \ (S \)In all \ (S [l: r] \) intersection is essentially the same substring empty: twice and must be cut to the (S [l: r] \ ) \ essentially the same sub-strings are not cut, and the substring to the intersection, and the like case (2). However, first knife may not cut the leftmost string, \ ([rl is an,. 1 + R_ {I}] \) may also be the empty set. It found that when \ (r_i-len + 1 < lr \) and \ (r_ {i + 1} > rl \) , the section across the board \ ([r_i, r_ {i + 1}) \) are legitimate. The answer in this case is \ (\ sum \ limits_ {r_ {i + 1}> rl} ^ {r_i-len + 1 <lr} (r_ {i + 1} -r_i) \ times (r_ {i + 1 -rl}) \) . There may also be some \ ([r_i, + R_ {I}. 1) \) , satisfies \ (lr \ in [r_i- len + 1, r_ {i + 1} -len + 1) \) i.e. \ ((LR . 1-len +) \ in [r_i, + R_ {I}. 1) \) , this section more forward place \ ([r_i-len + 1 , lr] \) can be cut, can not be cut by the . Calculating this case request \ (lr + len-1 \ ) element left of the first set of right \ (R_P \) and a right side \ (R_S \) . This part of the answer\ ((LR-R_P + len-1) \ Times (R_S-RL) \) , it should be noted that, when the \ (r_s \ leq rl \) when this part of the answer is zero.

We need to maintain a set of right and \ (\ SUM (I + R_ {} -r_i. 1) \ Times (+ R_ {I} -rl. 1) \) , can be split into \ (s_1 (l, r) = \ sum (r_ {i + 1} -r_i) \ ) and \ (S_2 (L, R & lt) = \ SUM (I + R_ {} -r_i. 1) \ Times. 1} + R_ {I \) , so that maintenance intervals may segment tree right set leftmost and right merge \ (s1 (l, r) = s1 (l, mid) + s1 (mid + 1, r) + min (mid + 1, r) -max (l, mid ) \) , \ (S2 (L, R & lt) = S2 (L, MID) + S2 (MID +. 1, R & lt) + (min (MID +. 1, R & lt) -max (L, MID)) \ Times min ( +. 1 MID, R & lt) \) .
Seek right set of line segment tree merge. It is said that if not offline, "the sustainability of the segment tree merger" will MLE.

Code
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define rep(i,x,y) for(register int i=(x);i<=(y);++i)
#define dwn(i,x,y) for(register int i=(x);i>=(y);--i)
#define view(u,k) for(int k=fir[u];~k;k=nxt[k])
#define LL long long
#define maxn 100007
#define maxnd 200007
#define maxq 300007
#define maxk 6400007
#define ls son[u][0]
#define rs son[u][1]
#define mi (l+r>>1)
#define Ls(u) son[u][0]
#define Rs(u) son[u][1]
#define pb push_back
#define int long long
using namespace std;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)&&ch!='-')ch=getchar();
    if(ch=='-')f=-1,ch=getchar();
    while(isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    return x*f;
}
void write(LL x)
{
    if(x==0){putchar('0'),putchar('\n');return;}
    int f=0;char ch[20];
    if(x<0)putchar('-'),x=-x;
    while(x)ch[++f]=x%10+'0',x/=10;
    while(f)putchar(ch[f--]);
    putchar('\n');
    return;
}
int n,ch[maxnd][10],dis[maxnd],rt[maxnd],fa[maxnd],pt[maxn],lst,cnt,rot;
int son[maxk][2],cntnd,m,c[maxn],ord[maxnd],anc[maxnd][20];
char s[maxn];
LL ans[maxq];
struct node{int mx,mn;LL sum[2];}tr[maxk];
struct quest{int l,r,id;};
vector<quest>qt[maxnd];
void gt(node & x,node y){x.mx=y.mx,x.mn=y.mn,x.sum[0]=y.sum[0],x.sum[1]=y.sum[1];return;}
void pu(node & x,node ld,node rd)
{
    x.mx=rd.mx,x.mn=ld.mn,x.sum[0]=ld.sum[0]+rd.sum[0]+(LL)(rd.mn-ld.mx),x.sum[1]=ld.sum[1]+rd.sum[1]+(LL)(rd.mn-ld.mx)*rd.mn; 
    return;
}
int build(int u,int l,int r,int x)
{
    if(!u)u=++cntnd;
    if(x<=l&&r<=x){tr[u].mx=tr[u].mn=x,tr[u].sum[0]=tr[u].sum[1]=0;return u;}
    if(x<=mi)ls=build(ls,l,mi,x);
    else rs=build(rs,mi+1,r,x);
    if(!ls||!rs)gt(tr[u],tr[ls|rs]);
    else pu(tr[u],tr[ls],tr[rs]);return u;
}
int merge(int ua,int ub,int l,int r)
{
    if(!ua||!ub)return ua|ub;
    Ls(ua)=merge(Ls(ua),Ls(ub),l,mi);
    Rs(ua)=merge(Rs(ua),Rs(ub),mi+1,r);
    if(!Ls(ua)||!Rs(ua))gt(tr[ua],tr[Ls(ua)|Rs(ua)]);
    else pu(tr[ua],tr[Ls(ua)],tr[Rs(ua)]);return ua;
}
void ask(int u,int l,int r,int x,int y,node & k)
{
    if(!u||x>y)return;
    if(x<=l&&r<=y){if(k.mx==0)gt(k,tr[u]);else pu(k,k,tr[u]);return;}
    if(x<=mi&&ls)ask(ls,l,mi,x,y,k);
    if(y>mi&&rs)ask(rs,mi+1,r,x,y,k);
    return;
}
int pref(int u,int x){node kk;kk.mx=kk.mn=0,ask(u,1,n,1,x,kk);return kk.mx;}
int sufx(int u,int x){node kk;kk.mx=kk.mn=0,ask(u,1,n,x,n,kk);return kk.mn;}
int gx(char c){return c-'0';}
void ext(int i)
{
    int p=lst,v=gx(s[i]),np=++cnt;dis[np]=i,lst=pt[i]=np;rt[np]=build(rt[np],1,n,i);
    for(;p&&!ch[p][v];p=fa[p])ch[p][v]=np;
    if(!p)fa[np]=rot;
    else
    {
        int q=ch[p][v];
        if(dis[p]+1==dis[q])fa[np]=q;
        else
        {
            int nq=++cnt;
            dis[nq]=dis[p]+1;memcpy(ch[nq],ch[q],sizeof(ch[q]));
            fa[nq]=fa[q],fa[q]=fa[np]=nq;
            for(;p&&ch[p][v]==q;p=fa[p])ch[p][v]=nq;
        }
    }
}
int tp[maxq];
LL c2(int x){return (LL)x*(x-1)/2ll;}
signed main()
{
    rot=lst=cnt=1;
    scanf("%d%d%s",&n,&m,s+1);
    rep(i,1,n)ext(i);
    rep(i,1,cnt)c[dis[i]]++;
    rep(i,1,n)c[i]+=c[i-1];
    rep(i,1,cnt)ord[c[dis[i]]--]=i;
    rep(j,1,cnt)
    {
        int i=ord[j];
        anc[i][0]=fa[i];
        rep(k,1,19)anc[i][k]=anc[anc[i][k-1]][k-1];
    }
    rep(i,1,m)
    {
        int l=read(),r=read(),nd=pt[r];
        dwn(j,19,0)if(dis[anc[nd][j]]>=r-l+1)nd=anc[nd][j];
        quest q;q.l=l,q.r=r,q.id=i;qt[nd].pb(q);
    }
    dwn(j,cnt,1)
    {
        int i=ord[j],sz=qt[i].size(),rr=tr[rt[i]].mx,lr=tr[rt[i]].mn;
        rep(k,0,sz-1)
        {
            int len=qt[i][k].r-qt[i][k].l+1,rl=rr-len+1,nd3=0;
            if(lr+len-1<=n)nd3=sufx(rt[i],lr+len-1);
            if(nd3&&nd3<=rl){tp[qt[i][k].id]=3;continue;}
            if(lr>rl){tp[qt[i][k].id]=1;ans[qt[i][k].id]=-tr[rt[i]].sum[0]*((LL)rl)+tr[rt[i]].sum[1]+c2(lr-rl)+(LL)(lr-rl)*(n-len);}
            else
            {
                int sec,fst=pref(rt[i],rl);
                node kk;kk.mx=kk.mn=0;if(fst)ask(rt[i],1,n,fst,lr+len-1,kk);
                if(kk.mx)ans[qt[i][k].id]=-(LL)rl*kk.sum[0]+kk.sum[1];
                fst=sec=0;
                if(lr+len-1<=n)fst=pref(rt[i],lr+len-1),sec=sufx(rt[i],lr+len-1);
                if(sec&&fst&&sec>rl)ans[qt[i][k].id]+=(LL)(lr-fst+len-1)*(sec-rl);
                tp[qt[i][k].id]=2;
            }
        }
        rt[fa[i]]=merge(rt[fa[i]],rt[i],1,n);
    } 
    rep(i,1,m){write(c2(n-1)-ans[i]);}
    return 0;
}
Some thoughts

A write down of one year from closing without spending a penny

Guess you like

Origin www.cnblogs.com/xzyf/p/11559743.html