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< ...
随机推荐
- C# 利用WMI对象获取物理内存和可用内存大小
下面的代码演示的是使用WMI对象可获取取物理内存和可用内存大小,在使用WMI对象前,先要添加对System.Management的引用,然后就可以调用WMI对象,代码如下: //获取总物理内存大小 M ...
- js阻止冒泡事件和默认事件的方法
阻止默认事件 function stopDeFault(e){ if(e&&e.preventDefault){//非IE e.preventDefault(); }else{//IE ...
- [洛谷P4329][COCI2006-2007#1] Bond
题目大意:有$n$个人有$n$个任务,每个人执行每个任务有不同的成功率,每个人只能执行一个任务,求所有任务都执行的总的成功率. 题解:可以跑最大费用最大流,把成功率取个$log$,最后$exp$回去就 ...
- JSONP以及Spring对象MappingJacksonValue的使用方式
什么是JSONP?,以及Spring对象MappingJacksonValue的使用方式 原文: https://blog.csdn.net/weixin_38111957/article/detai ...
- 小程序根据input输入,动态设置按钮的样式
[需求]实现当手机号已填写和协议已勾选时,“立即登录”按钮变亮,按钮可点击:若有一个不满足,按钮置灰,不可点击:实现获取短信验证码,倒计时提示操作:对不满足要求内容进行toast弹窗提示. <v ...
- windows主机控制
一.开关机控制 using System.Runtime.InteropServices; //注销.关机.重启 class shutdown { [StructLayout(LayoutKind.S ...
- 【Foreign】无聊的计算姬 [Lucas][BSGS]
无聊的计算姬 Time Limit: 10 Sec Memory Limit: 256 MB Description Input Output Sample Input 6 2 2 3 4 3 2 ...
- 【洛谷 P4886】 快递员 (点分治)
这题因为一些小细节还是\(debug\)了很久...不过我第一次用脚本对拍,不亏. 先随便找一个点作为根,算出答案,即所有点对到这个点的距离和的最大值,并记录所有距离最大的点对.如果这个点在任意一个距 ...
- Java任务调度框架----kunka
初衷 工作中用到了很多框架,但是给我印象最深的还是我们PO(Product Owner)在若干年前写的一套任务调度框架,在JDK1.4之前,concurrent包还没有引入, 手写的这套Token调度 ...
- python函数篇:名称空间、作用域和函数的嵌套
一.名称空间:(有3类) (1)内置名称空间(全局作用域) (2)全局名称空间(全局作用域) (3)局部名称空间(局部作用域) 关于名称空间的查询: x=1 def func(): print('fr ...