火星人prefix bzoj-1014 JSOI-2004

题目大意:给定一个字符串,支持三种操作:1.查询;两个后缀之间的$LCP$;2.单点修改;3.插入一个字符。

注释:$1\le n\le 10^5$,$1\le m\le 1.5\cdot 10^5$。


想法

第一眼就是后缀数组,但是发现有单点插入操作果断$pass$。

一个序列支持单点插入肯定最少是个平衡树。

又发现$log^2n$好像能过,我们就对序列建立非旋转$Treap$然后维护子树$hash$值。

每一次查询的时候二分,然后撕出区间暴力验证即可。

时间复杂度$O(mlog^2n)$。

Code:

#include <bits/stdc++.h>
#define N 100010
using namespace std;
typedef unsigned int ull;
const ull base = 97 ;
int root,cnt; char s[N];
ull B[1000010];
#define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
char buf[100000],*p1,*p2;
inline int rd()
{
register int x=0;register char c=nc();
while(c<'0'||c>'9')c=nc();
while(c>='0'&&c<='9')x=((x+(x<<2))<<1)+(c^48),c=nc();
return x;
}
struct Node
{
int ls,rs,size,key;
ull sum,val;
}a[N<<1];
struct par {int x,y;};
inline void pushup(int x)
{
int ls=a[x].ls,rs=a[x].rs;
a[x].sum=a[x].val; a[x].size=1;
if(ls) a[x].sum-=a[x].val,a[x].sum+=a[ls].sum+a[x].val*B[a[ls].size],a[x].size+=a[ls].size;
if(rs) a[x].sum+=a[rs].sum*B[a[ls].size+1],a[x].size+=a[rs].size;
}
int merge(int x,int y)
{
if(!x||!y) return x|y;
if(a[x].key>a[y].key)
{
a[x].rs=merge(a[x].rs,y); pushup(x);
return x;
}
else
{
a[y].ls=merge(x,a[y].ls); pushup(y);
return y;
}
}
par split(int x,int k)
{
if(!k) return (par){0,x};
int ls=a[x].ls,rs=a[x].rs;
if(k==a[ls].size)
{
a[x].ls=0; pushup(x);
return (par){ls,x};
}
else if(k==a[ls].size+1)
{
a[x].rs=0; pushup(x);
return (par){x,rs};
}
else if(k<a[ls].size)
{
par t=split(ls,k);
a[x].ls=t.y; pushup(x);
return (par){t.x,x};
}
else
{
par t=split(rs,k-a[ls].size-1);
a[x].rs=t.x; pushup(x);
return (par){x,t.y};
}
}
inline int newnode(ull val)
{
int x=++cnt;
a[x].ls=a[x].rs=0; a[x].size=1;
a[x].sum=a[x].val=val; a[x].key=rand()*rand();
return x;
}
void insert(int x,ull val)
{
par t=split(root,x);
root=merge(t.x,merge(newnode(val),t.y));
}
ull query(int x,int k)
{
par t1=split(root,x-1),t2=split(t1.y,k);
ull re=a[t2.x].sum;
root=merge(t1.x,merge(t2.x,t2.y));
return re;
}
void update(int x,ull y)
{
par t1=split(root,x-1),t2=split(t1.y,1);
root=merge(t1.x,merge(newnode(y),t2.y));
}
void output(int x)
{
int ls=a[x].ls,rs=a[x].rs;
if(ls) output(ls);
printf("%lld ",a[x].val);
if(rs) output(rs);
}
int build(int l,int r)
{
if(l==r) return newnode(s[l]-'a'+1);
int mid=(l+r)>>1;
return merge(build(l,mid),build(mid+1,r));
}
int main()
{
srand(20021214);
B[0]=1;
for(int i=1;i<=1000000;i++) B[i]=B[i-1]*base;
scanf("%s",s+1); int n=strlen(s+1);
root=build(1,n);
int m=rd(); while(m--)
{
char opt=nc(); while(opt!='Q'&&opt!='R'&&opt!='I')opt=nc(); int x=rd();
if(opt=='Q')
{
int y=rd();
int r=min(n-x+1,n-y+1)+1,l=0;
// printf("%d %d\n",x,y);
while(l<r)
{
int mid=(l+r)>>1;
// printf("%d %lld %lld\n",mid,query(x,mid),query(y,mid));
if(query(x,mid)==query(y,mid)) l=mid+1;
else r=mid;
}
printf("%d\n",l-1);
// printf("djhdjhfjhfgjuhdfgiodfg=%d\n",query(x,5)==query(y,5));
// r--;
// int d,p=0;
// for(d=1;d<=r&&query(x,d)==query(y,d);d<<=1) ;
// for(d>>=1;d;d>>=1) if(p+d<=r&&query(x,p+d)==query(y,p+d)) p+=d;
// // printf("djhdjhfjhfgjuhdfgiodfg=%d\n",query(x,5)==query(y,5));
// printf("%d\n",p);
}
else if(opt=='R')
{
char d=nc();while(d<'a'&&d>'z')d=nc(); ull val=d-'a'+1;
update(x,val);
}
else
{
n++;
char d=nc();while(d<'a'&&d>'z')d=nc(); ull val=d-'a'+1;
insert(x,val);
}
}
return 0;
}

小结:卧槽这题卡常,用特判+读入优化+非旋转$Treap$的$O(n)$建树才卡过去。

[bzoj1014][JSOI2008]火星人prefix_非旋转Treap_hash_二分的更多相关文章

  1. bzoj1014: [JSOI2008]火星人prefix(splay+hash+二分)

    题目大意:一个字符串三个操作:①求两个后缀的LCP②插入一个字符③修改一个字符. 前几天刚学了hash+二分求lcp,就看到这题. 原来splay还能这么用?!原来splay模板这么好写?我以前写的s ...

  2. BZOJ1014 JSOI2008 火星人prefix 【非旋转Treap】*

    BZOJ1014 JSOI2008 火星人prefix Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符 ...

  3. [BZOJ1014][JSOI2008]火星人prefix

    [BZOJ1014][JSOI2008]火星人prefix 试题描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字 ...

  4. 2018.06.28 BZOJ1014 [JSOI2008]火星人prefix(非旋treap+hash)

    [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MB Submit: 8951 Solved: 2860 Description 火星 ...

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

    http://www.lydsy.com/JudgeOnline/problem.php?id=1014 两个后缀的最长公共前缀:二分+hash 带修改带插入:splay维护 #include< ...

  6. BZOJ1014[JSOI2008]火星人——非旋转treap+二分答案+hash

    题目描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 8 9 10 ...

  7. [BZOJ1014] [JSOI2008] 火星人prefix (splay & 二分答案)

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  8. bzoj1014: [JSOI2008]火星人prefix splay+hash+二分

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  9. BZOJ1014: [JSOI2008]火星人prefix(splay 二分 hash)

    题意 题目链接 Sol 一眼splay + 二分hash,不过区间splay怎么写来着呀 试着写了两个小时发现死活不对 看了一下yyb的代码发现自己根本就不会splay.... // luogu-ju ...

随机推荐

  1. 用jquery的.val() 给具有style="display:none;" 属性的标签写值的问题。

    今天写项目, 碰到奇怪现象, 用jquery的val()函数怎么都无法给标签赋值,而我确定是否赋值是通过浏览器控制台来看的.其实这种方式不准确,因为具有 style="display:non ...

  2. ReactJS-0-React介绍

    React介绍: React是一个库而不是一个MVC框架,因为React只负责解决MVC框架中V(View)层面的问题,React致力于创建可重用的UI组件.(React is a library f ...

  3. 掌握Spark机器学习库-07-回归算法原理

    1)机器学习模型理解 统计学习,神经网络 2)预测结果的衡量 代价函数(cost function).损失函数(loss function) 3)线性回归是监督学习

  4. jq 中.html(),.text()和.val()的总结

    html与.text的方法操作是一样,只是在具体针对处理对象不同 html处理的是元素内容,.text处理的是文本内容 html只能使用在HTML文档中,.text 在XML 和 HTML 文档中都能 ...

  5. QQ感叹号是什么鬼?原来是服务器波动,腾讯官方来辟谣了

    今天晚上很多网友在用QQ发送消息的时候发现,自己发送的消息一直是感叹号❗到底是怎么回事呢?是消息都发不出去了吗?马浩周通过手机测试后发现,其实消息是可以发出去的,而官方手机QQ出来已经通知了,是服务器 ...

  6. IE浏览器样式表限制

    原文链接:http://caibaojian.com/ie-stylesheet.html 在开发头条上发现的IE浏览器样式限制,算是一个IE浏览器的一个bug吧.主要有4点限制:· IE9及以下单个 ...

  7. 使用Gson解析Json数组遇到的泛型类型擦除问题解决方法

    谷歌Gson转换Json串有如下方法: public Object fromJson(String json, Type typeOfT);1可以使用它进行数组解析.如下,使用此方法解析Json串为类 ...

  8. error while loading shared libraries: libclntsh.so.11.1

    解决这个问题有两种方法 1.在当前用户下,添加链接库所在路径 LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH; export LD_LIBRARY_ ...

  9. JFinal项目eclipse出现the table mapping of model: com.gexin.model.scenic.Scenic not exists or the ActiveRecordPlugin not start.

    JFinal项目eclipse出现the table mapping of model: com.gexin.model.scenic.Scenic not exists or the ActiveR ...

  10. 怎样从SpringMVC返回json数据

    Srping3中配置 maven依赖pom.xml 需要jackson库的依赖 <dependency> <groupId>org.codehaus.jackson</g ...