bzoj千题计划106:bzoj1014 [JSOI2008]火星人prefix

http://www.lydsy.com/JudgeOnline/problem.php?id=1014

两个后缀的最长公共前缀:二分+hash

带修改带插入:splay维护

#include<cstdio>
#include<cstring>
#include<iostream>

#define L 100001

typedef unsigned long long ULL;

using namespace std;

char s[L+4];

int tot,root;
int fa[L+4],ch[L+4][2],siz[L+4],key[L+4];
ULL has[L+4];

ULL bit[L+4];

int len;

void read(int &x)
{
    x=0; char c=getchar();
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) { x=x*10+c-‘0‘; c=getchar();}
}

void update(int x)
{
    siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
    has[x]=has[ch[x][0]]*bit[siz[ch[x][1]]+1]+key[x]*bit[siz[ch[x][1]]]+has[ch[x][1]];
}

void build(int l,int r,int f)
{
    if(l>r) return;
    int mid=l+r>>1;
    ch[f][mid>f]=mid;
    fa[mid]=f;
    siz[mid]=1; key[mid]=s[mid]-‘a‘+1;
    build(l,mid-1,mid);
    build(mid+1,r,mid);
    update(mid);
}

int find(int x)
{
    int now=root;
    while(1)
    {
        if(siz[ch[now][0]]>=x) now=ch[now][0];
        else
        {
            x-=siz[ch[now][0]];
            if(x==1) return now;
            x--; now=ch[now][1];
        }
    }
}

int findsuf(int x)
{
    int now=ch[x][1];
    while(ch[now][0]) now=ch[now][0];
    return now;
}

void rotate(int x,int &goal)
{
    int y=fa[x],z=fa[y],k=ch[y][1]==x;
    if(y!=goal) ch[z][ch[z][1]==y]=x;
    else goal=x;
    ch[y][k]=ch[x][k^1]; ch[x][k^1]=y;
    fa[x]=z; fa[y]=x; fa[ch[y][k]]=y;
    update(y);
}

bool getson(int x)
{
    return ch[fa[x]][1]==x;
}

void splay(int x,int &goal)
{
    int y;
    while(x!=goal)
    {
        y=fa[x];
        if(y==goal) rotate(x,goal);
        else
        {
            if(getson(x)^getson(y)) rotate(x,goal);
            else rotate(y,goal);
            rotate(x,goal);
        }
        update(x);
    }

}

ULL query(int l,int r)
{
    if(r==len+1)
    {
        int t1=find(l-1);
        splay(t1,root);
        return has[ch[root][1]];
    }
    int tl=find(l-1),tr=find(r+1);
    splay(tl,root);
    splay(tr,ch[root][1]);
    return has[ch[tr][0]];
}

int main()
{
    //freopen("bzoj_1014.in","r",stdin);
    //freopen("bzoj_1014.out","w",stdout);
    scanf("%s",s+2); len=strlen(s+2);
    int m; read(m);
    bit[0]=1;  for(int i=1;i<L;++i) bit[i]=bit[i-1]*27;
    build(1,len+1,0);
    root=len+2>>1;
    char c[3]; int x,y;
    int l,r,mid,ans;
     tot=len+1;
    while(m--)
    {
        scanf("%s",c);
        if(c[0]==‘Q‘)
        {
            read(x); read(y);
            x++; y++;
            ans=0; l=0; r=min(len+1-x+1,len+1-y+1);
            while(l<=r)
            {
                mid=l+r>>1;
                if(query(x,x+mid-1)!=query(y,y+mid-1)) r=mid-1;
                else l=mid+1,ans=mid;
            }
            cout<<ans<<‘\n‘;
        }
        else if(c[0]==‘R‘)
        {
            read(x); scanf("%s",c);
            x++;
            x=find(x);
            splay(x,root);
            key[x]=c[0]-‘a‘+1;
        }
        else
        {
            read(x); scanf("%s",c);
            x++;
            x=find(x); splay(x,root);
            y=findsuf(x);
            if(y)
            {
                splay(y,ch[x][1]);
                ch[y][0]=++tot; fa[tot]=y;
                key[tot]=has[tot]=c[0]-‘a‘+1;
                siz[tot]=1;
                update(y); update(x);
            }
            else
            {
                ch[x][1]=++tot; fa[tot]=x;
                key[tot]=has[tot]=c[0]-‘a‘+1;
                siz[tot]=1;
                update(x);
            }
            len++;
        }
    }
}

1014: [JSOI2008]火星人prefix

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 8125  Solved: 2572
[Submit][Status][Discuss]

Description

  火星人最近研究了一种操作:求一个字串两个后缀的公共前缀。比方说,有这样一个字符串:madamimadam,
我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 8 9 10 11 字符 m a d a m i m a d a m 现在,
火星人定义了一个函数LCQ(x, y),表示:该字符串中第x个字符开始的字串,与该字符串中第y个字符开始的字串
,两个字串的公共前缀的长度。比方说,LCQ(1, 7) = 5, LCQ(2, 10) = 1, LCQ(4, 7) = 0 在研究LCQ函数的过程
中,火星人发现了这样的一个关联:如果把该字符串的所有后缀排好序,就可以很快地求出LCQ函数的值;同样,
如果求出了LCQ函数的值,也可以很快地将该字符串的后缀排好序。 尽管火星人聪明地找到了求取LCQ函数的快速
算法,但不甘心认输的地球人又给火星人出了个难题:在求取LCQ函数的同时,还可以改变字符串本身。具体地说
,可以更改字符串中某一个字符的值,也可以在字符串中的某一个位置插入一个字符。地球人想考验一下,在如此
复杂的问题中,火星人是否还能够做到很快地求取LCQ函数的值。

Input

  第一行给出初始的字符串。第二行是一个非负整数M,表示操作的个数。接下来的M行,每行描述一个操作。操
作有3种,如下所示
1、询问。语法:Qxy,x,y均为正整数。功能:计算LCQ(x,y)限制:1<=x,y<=当前字符串长度。
2、修改。语法:Rxd,x是正整数,d是字符。功能:将字符串中第x个数修改为字符d。限制:x不超过当前字
符串长度。
3、插入:语法:Ixd,x是非负整数,d是字符。功能:在字符串第x个字符之后插入字符d,如果x=0,则在字
符串开头插入。限制:x不超过当前字符串长度

Output

  对于输入文件中每一个询问操作,你都应该输出对应的答案。一个答案一行。

Sample Input

madamimadam
7
Q 1 7
Q 4 8
Q 10 11
R 3 a
Q 1 7
I 10 a
Q 2 11

Sample Output

5
1
0
2
1

HINT

1、所有字符串自始至终都只有小写字母构成。

2、M<=150,000

3、字符串长度L自始至终都满足L<=100,000

4、询问操作的个数不超过10,000个。

对于第1,2个数据,字符串长度自始至终都不超过1,000

对于第3,4,5个数据,没有插入操作。

时间: 2024-08-05 03:33:29

bzoj千题计划106:bzoj1014 [JSOI2008]火星人prefix的相关文章

bzoj千题计划104:bzoj1013: [JSOI2008]球形空间产生器sphere

http://www.lydsy.com/JudgeOnline/problem.php?id=1013 设球心(x1,x2,x3……) 已知点的坐标为t[i][j] 那么 对于每个i满足 Σ (t[i][j]-x[j])^2 = Σ (t[0][j]-x[j])^2 化简开就是 2*(t[0][j]-t[i][j])*x[j] = t[0][j]^2-t[i][j]^2 n个方程n个未知数 高斯消元 #include<cmath> #include<cstdio> #includ

bzoj千题计划219:bzoj1568: [JSOI2008]Blue Mary开公司

http://www.lydsy.com/JudgeOnline/problem.php?id=1568 写多了就觉着水了... #include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define N 100001 double a[N<<2],b[N<<2]; bool have[N<<2]; long long ans; void

bzoj千题计划126:bzoj1038: [ZJOI2008]瞭望塔

http://www.lydsy.com/JudgeOnline/problem.php?id=1038 本题可以使用三分法 将点按横坐标排好序后 对于任意相邻两个点连成的线段,瞭望塔的高度 是单峰函数,而且是下凸函数 感性理解单峰就是 瞭望塔建的靠左,为了能看到右边的,要高一点 瞭望塔建的靠右,为了能看到左边的,要高一点 所以 枚举所有线段,三分线段上建造瞭望塔的位置,所有线段上的瞭望塔高度取最小 #include<cmath> #include<cstdio> #include

bzoj千题计划185:bzoj1260: [CQOI2007]涂色paint

http://www.lydsy.com/JudgeOnline/problem.php?id=1260 区间DP模型 dp[l][r] 表示涂完区间[l,r]所需的最少次数 从小到大们枚举区间[l,r] 如果col[l]==col[r] dp[l][r]=min(dp[l+1][r],dp[l][r-1],dp[l+1][r-1]+1) 否则 dp[l][r]=min(dp[l][k]+dp[k+1][r]) 我还是辣鸡啊~~~~(>_<)~~~~,这种题都不能秒 #include<c

bzoj千题计划292:bzoj2244: [SDOI2011]拦截导弹

http://www.lydsy.com/JudgeOnline/problem.php?id=2244 每枚导弹成功拦截的概率 = 包含它的最长上升子序列个数/最长上升子序列总个数 pre_len [i] 表示以i结尾的最长不下降子序列的长度 pre_sum[i] 表示对应长度下的方案数 suf_len[i] 表示以i开头的最长不下降子序列长度 suf_sum[i] 表示对应长度下的方案数 若已有了这4个数组 设最长上升子序列长度=mx 那么 如果pre_len[i]+suf_len[i] -

bzoj千题计划304:bzoj3676: [Apio2014]回文串

https://www.lydsy.com/JudgeOnline/problem.php?id=3676 回文自动机模板题 4年前的APIO如今竟沦为模板,,,╮(╯▽╰)╭,唉 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define N 300001 char ss[N]; int s[N]; int tot=1,last; int fail[N],len

bzoj千题计划108:bzoj1018: [SHOI2008]堵塞的交通traffic

http://www.lydsy.com/JudgeOnline/problem.php?id=1018 关键点在于只有两行 所以一个2*m矩形连通情况只有6种 编号即对应代码中的a数组 线段树维护 用b数组表示 节点第0/1行的最右一列是否连接了右边 来 辅助 节点的合并 查询 对两个点位于矩形的位置分4种情况讨论 两点是否联通,要考虑四种情况 (以两个位置是矩形左上角和右上角为例) 1.直接联通,线段树的节点包含了这种情况,直接判断 2. 3. 4. 后三种情况需要再查询[1,l]和[r,n

bzoj千题计划109:bzoj1019: [SHOI2008]汉诺塔

http://www.lydsy.com/JudgeOnline/problem.php?id=1019 题目中问步骤数,没说最少 可以大胆猜测移动方案唯一 (真的是唯一但不会证) 设f[i][j] 表示 从i号柱子 上把j个盘子移到 g[i][j] 柱子上的步数 初始化:f[0][1]=1,g[0][1] 根据优先级决定 设三根柱子分别为0,1,2 对于每一个f[x][i], 把前i-1个移走,把第i个移走,把前i-1个移回 令y=g[x][i-1],则k=0+1+2-x-y 我们希望 把i-

bzoj千题计划111:bzoj1021: [SHOI2008]Debt 循环的债务

http://www.lydsy.com/JudgeOnline/problem.php?id=1021 如果A收到了B的1张10元,那么A绝对不会把这张10元再给C 因为这样不如B直接给C优 由此可以推出 若A欠B20元,B欠C 30元, 那么A还C20元,B还C10元最优 所以一共只有 A->BC   B->AC  C->AB AB->C  BC->A  AC->B 这6种转移情况 根据输入,我们可以知道三人最终手中有多少钱ea.eb.ec,一共有多少钱sum 设f