AC日记——[JSOI2008]火星人prefix bzoj 1014
思路:
平衡树+二分答案+hash;
好了懂了吧。
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 2000005 int ch[maxn][],key[maxn],f[maxn],root,size[maxn];
int n,m,tot; long long ha[maxn],mi[maxn]; char ai[maxn]; inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} inline bool getson(int now)
{
return ch[f[now]][]==now;
} inline void updata(int now)
{
size[now]=,ha[now]=;
if(ch[now][]) size[now]+=size[ch[now][]],ha[now]+=ha[ch[now][]];
ha[now]+=key[now]*mi[size[now]-];
if(ch[now][]) ha[now]+=ha[ch[now][]]*mi[size[now]],size[now]+=size[ch[now][]];
} inline void rotate(int now,int &to)
{
int fa=f[now],ffa=f[fa];bool pos=getson(now);
ch[fa][pos]=ch[now][pos^];
if(ch[fa][pos]) f[ch[fa][pos]]=fa;
if(to==fa) to=now;
else ch[ffa][getson(fa)]=now;
ch[now][pos^]=fa,f[fa]=now,f[now]=ffa;
updata(fa),updata(now);
} void splay(int now,int &to)
{
while(now!=to)
{
int fa=f[now],ffa=f[fa];
if(fa!=to)
{
if(ch[fa][]==now^ch[ffa][]==fa) rotate(now,to);
else rotate(now,to);
}rotate(now,to);
}
} void find(int p,bool pos)
{
int now=root;
while()
{
if(size[ch[now][]]>=p) now=ch[now][];
else
{
p-=size[ch[now][]];
if(p==)
{
if(pos) splay(now,root);
else splay(now,ch[root][]);
return ;
}
else p--,now=ch[now][];
}
}
} void insert(int p,int ci)
{
find(p,true),find(p+,false);
ch[ch[root][]][]=++tot;
key[tot]=ci,size[tot]=,f[tot]=ch[root][],ha[tot]=ci;
updata(ch[root][]),updata(root);
} int tree_build(int l,int r,int fa)
{
int now=l+r>>;key[now]=ai[now],f[now]=fa;
if(now>l) ch[now][]=tree_build(l,now-,now);
if(now<r) ch[now][]=tree_build(now+,r,now);
updata(now);return now;
} long long hash_ci(int p,int len)
{
find(p,true),find(p+len+,false);
return ha[ch[ch[root][]][]];
} int main()
{
mi[]=;
for(int i=;i<=;i++) mi[i]=mi[i-]*27LL;
scanf("%s",ai+),tot=strlen(ai+)+;
for(int i=;i<=tot-;i++) ai[i]=ai[i]-'a'+;
root=tree_build(,tot,);
in(m);char op[];int u,v;
for(;m--;)
{
scanf("%s",op);
if(op[]=='Q')
{
in(u),in(v);
if(u>v) swap(u,v);
int l=,r=tot-v-,ans=;
while(l<=r)
{
int mid=l+r>>;
if(hash_ci(u,mid)==hash_ci(v,mid)) ans=mid,l=mid+;
else r=mid-;
}
printf("%d\n",ans);
}
else if(op[]=='R')
{
in(u),scanf("%s",op);
find(u+,true),key[root]=op[]-'a'+,updata(root);
}
else
{
in(u),scanf("%s",op);
insert(u+,(int)(op[]-'a'+));
}
}
return ;
}
AC日记——[JSOI2008]火星人prefix bzoj 1014的更多相关文章
- 1014: [JSOI2008]火星人prefix - BZOJ
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6243 Solved: 2007[Submit] ...
- BZOJ 1014: [JSOI2008]火星人prefix Splay+二分
1014: [JSOI2008]火星人prefix 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1014 Description 火星人 ...
- bzoj 1014: [JSOI2008]火星人prefix hash && splay
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3154 Solved: 948[Submit][ ...
- 求帮看!!!!BZOJ 1014 [JSOI2008]火星人prefix
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4164 Solved: 1277[Submit] ...
- BZOJ 1014: [JSOI2008]火星人prefix( splay + hash )
用splay维护序列, 二分+hash来判断LCQ.. #include<bits/stdc++.h> using namespace std; typedef unsigned long ...
- BZOJ 1014 [JSOI2008]火星人prefix (Splay + Hash + 二分)
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 8112 Solved: 2569[Submit] ...
- 1014: [JSOI2008]火星人prefix
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MB Description 火星人最近研究了一种操作:求一个字串两个后缀 ...
- bzoj千题计划106:bzoj1014 [JSOI2008]火星人prefix
http://www.lydsy.com/JudgeOnline/problem.php?id=1014 两个后缀的最长公共前缀:二分+hash 带修改带插入:splay维护 #include< ...
随机推荐
- [Elasticsearch] 多字段搜索 (二) - 最佳字段查询及其调优
最佳字段(Best Fields) 假设我们有一个让用户搜索博客文章的网站,就像这两份文档一样: PUT /my_index/my_type/1 { "title": " ...
- akka与slf4j导致jvm直接crash的诡异
流程很简单,创建actorSystem,通过actorSystem获取AkkaQueryServiceRetriever,进而通过传递path获得的Gateway进行通信. 之前在主项目里跑的很稳定, ...
- React & styled component
React & styled component https://www.styled-components.com/#your-first-styled-component tagged t ...
- JVM GC Q&A(补充ing)
1.如果一个对象没有与其相连的GC ROOT,一定会被回收吗? 这个对象并非是非死不可的,这时他只是处于死缓阶段,要真正宣告一个对象的死亡,至少要经历两次标记过程:如果对象在进行可达性分析后发现并没有 ...
- poj 1034 The dog task (二分匹配)
The dog task Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2559 Accepted: 1038 Sp ...
- [Leetcode] Reverse nodes in k group 每k个一组反转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- java的哈希遍历 hashmap
Map<String,String> map = new HashMap<String, String>(); map.put("title"," ...
- Java之戳中痛点 - (5)switch语句break不能忘以及default不同位置的用法
先看一段代码: public class Test{ public static void main(String[] args){ System.)); } } public static Stri ...
- 如何解决DuplicateFileException: Duplicate files copied in APK问题
问题:有重复的文件存在APK里 解决方案:在Module里的build.gradle中设置忽略此重复文件即可.
- 转:Spring AOP 注解方式实现的一些“坑”
使用过Spring AOP的注解方式实现,也入过不少坑,现在做一下记录,希望有同样需求的朋友可以少走弯路 使用之前还是先过一下官方文档吧,至少可以少走弯路,不用担心英文读不懂,其实只看代码例子就能理解 ...