BZOJ1014 JSOI2008火星人(splay+哈希)
splay维护哈希值即可。
- #include<iostream>
- #include<cstdio>
- #include<cmath>
- #include<cstdlib>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- #define ll long long
- #define ull unsigned long long
- #define N 100010
- #define P 509
- #define lson tree[k].ch[0]
- #define rson tree[k].ch[1]
- char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<''||c>'')) c=getchar();return c;}
- int gcd(int n,int m){return m==?n:gcd(m,n%m);}
- int read()
- {
- int x=,f=;char c=getchar();
- while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
- while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
- return x*f;
- }
- int m,cnt,root;
- ull p[N];
- char s[N];
- struct data{int ch[],fa,size,x;ull hash;
- }tree[N];
- void up(int k)
- {
- tree[k].size=tree[lson].size+tree[rson].size+;
- tree[k].hash=tree[lson].hash*p[tree[rson].size+]+tree[k].x*p[tree[rson].size]+tree[rson].hash;
- }
- void build(int &k,int l,int r)
- {
- if (l>r) return;
- int mid=l+r>>;
- k=++cnt,tree[k].x=s[mid]-'a';
- build(lson,l,mid-),build(rson,mid+,r);
- tree[lson].fa=tree[rson].fa=k;
- up(k);
- }
- int whichson(int k){return tree[tree[k].fa].ch[]==k;}
- void move(int k)
- {
- int fa=tree[k].fa,gf=tree[fa].fa,p=whichson(k);
- if (fa) tree[gf].ch[whichson(fa)]=k;tree[k].fa=gf;
- tree[tree[k].ch[!p]].fa=fa,tree[fa].ch[p]=tree[k].ch[!p];
- tree[k].ch[!p]=fa,tree[fa].fa=k;
- up(fa),up(k);
- }
- void splay(int k,int rt)
- {
- while (tree[k].fa!=rt)
- {
- int fa=tree[k].fa;
- if (tree[fa].fa!=rt)
- if (whichson(k)^whichson(fa)) move(k);
- else move(fa);
- move(k);
- }
- if (!rt) root=k;
- }
- int find(int k,int x)
- {
- if (tree[lson].size+==x) return k;
- if (tree[lson].size>=x) return find(lson,x);
- else return find(rson,x-tree[lson].size-);
- }
- int split(int x,int y)
- {
- int p=find(root,x),q=find(root,y+);
- splay(p,);
- splay(q,p);
- return q;
- }
- ull gethash(int x,int y)
- {
- int p=split(x,y);
- return tree[tree[p].ch[]].hash;
- }
- void ins(int k,int x)
- {
- int p=split(k+,k);
- k=++cnt;tree[k].fa=p,tree[p].ch[]=k,tree[k].x=x;
- up(k),up(p),up(root);
- }
- void modify(int k,int x)
- {
- int p=split(k,k);
- tree[k=tree[p].ch[]].x=x;
- up(k),up(p),up(root);
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("bzoj1014.in","r",stdin);
- freopen("bzoj1014.out","w",stdout);
- const char LL[]="%I64d\n";
- #else
- const char LL[]="%lld\n";
- #endif
- scanf("%s",s+);int n=strlen(s+);
- p[]=;for (int i=;i<=;i++) p[i]=p[i-]*P;
- build(root,,n+);
- m=read();
- while (m--)
- {
- char c=getc();
- if (c=='Q')
- {
- int x=read(),y=read();
- int l=,r=n-max(x,y)+,ans=;
- while (l<=r)
- {
- int mid=l+r>>;
- if (gethash(x,x+mid-)==gethash(y,y+mid-)) ans=mid,l=mid+;
- else r=mid-;
- }
- printf("%d\n",ans);
- }
- if (c=='I')
- {
- n++;int x=read();char c=getc();
- ins(x,c-'a');
- }
- if (c=='R')
- {
- int x=read();char c=getc();
- modify(x,c-'a');
- }
- }
- return ;
- }
BZOJ1014 JSOI2008火星人(splay+哈希)的更多相关文章
- BZOJ1014:[JSOI2008]火星人(Splay,hash)
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam, 我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 ...
- [BZOJ1014][JSOI2008]火星人prefix
[BZOJ1014][JSOI2008]火星人prefix 试题描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字 ...
- BZOJ1014 JSOI2008 火星人prefix 【非旋转Treap】*
BZOJ1014 JSOI2008 火星人prefix Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符 ...
- 2018.06.28 BZOJ1014 [JSOI2008]火星人prefix(非旋treap+hash)
[JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MB Submit: 8951 Solved: 2860 Description 火星 ...
- bzoj千题计划106:bzoj1014 [JSOI2008]火星人prefix
http://www.lydsy.com/JudgeOnline/problem.php?id=1014 两个后缀的最长公共前缀:二分+hash 带修改带插入:splay维护 #include< ...
- [bzoj1014](JSOI2008)火星人 prefix (Splay维护哈希)
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀. 比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 ...
- [BZOJ1014] [JSOI2008] 火星人prefix (splay & 二分答案)
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- bzoj1014: [JSOI2008]火星人prefix splay+hash+二分
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- bzoj1014: [JSOI2008]火星人prefix(splay+hash+二分)
题目大意:一个字符串三个操作:①求两个后缀的LCP②插入一个字符③修改一个字符. 前几天刚学了hash+二分求lcp,就看到这题. 原来splay还能这么用?!原来splay模板这么好写?我以前写的s ...
随机推荐
- Android学习之六种事件响应方法汇总
java源码如下: 1.MainActivity.java源码 package com.example.responsetest; import android.app.Activity; impor ...
- Android应用更新-自动检测版本及自动升级
原文 http://www.cnblogs.com/keyindex/articles/1819504.html 注:实质,把自己新版的APK放在可以下载的地方,先自己设置个通信,检查版本,我是直接放 ...
- day88
ContentType 场景需求:比方说我们现有两种商品,但是他们对应着一个价格策略表,为了防止数据库的浪费,我们在价格策略中加入一个表名字段,一个表明对应的id字段,这样的设计既优化了数据库,还不怕 ...
- flask多app和栈的应用
一.简介 flask的蓝图可以实现url的分发,当有多个app时也可以利用app进行url分发,这里介绍下使用方式和内部原理以及栈的应用. 二.多app使用 使用示例 from werkzeu ...
- Spring Boot 进行Bean Validate和Method Validate
SpringBoot在内部通过集成hibernate-validation 已经实现了JSR-349验证规范接口,在SpringBoot项目中只要直接使用就行了. 一般用在Controller中用于验 ...
- 分布式监控系统Zabbix--完整安装记录(7)-使用percona监控MySQL
前面已经介绍了分布式监控系统Zabbix-3.0.3-完整安装记录(2)-添加mysql监控,但是没有提供可以直接使用的Key,太过简陋,监控效果不佳.要想更加仔细的监控Mysql,业内同学们都会选择 ...
- zabbix邮件报警功能的验证
zabbix里面设置了很多监控项,有很多重要的监控预警,必须保证zabbix邮件报警功能正常,以确保那些告警信息能及时发送到运维人员的邮箱里. 所以需要每天8:30发一封确认zabbix邮件报警功能正 ...
- HTML5-indexedDB使用常见错误总结
indexedDB使用过程中常常会出现以下错误: Failed to execute ‘createObjectStore’ on ‘IDBDatabase’: The database is not ...
- linux内核分析第六周学习笔记
LINUX内核分析第六周学习总结 标签(空格分隔): 20135328陈都 陈都 原创作品转载请注明出处 <Linux内核分析>MOOC课程 http://mooc.study.163.c ...
- BF算法和KMP算法 python实现
BF算法 def Index(s1,s2,pos = 0): """ BF算法 """ i = pos j = 0 while(i < ...