【BZOJ 1014】 [JSOI2008]火星人prefix
【题目链接】:http://www.lydsy.com/JudgeOnline/problem.php?id=1014
【题意】
让你在线查询最长公共前缀.
支持单节点修改;
插入操作;
【题解】
/*
伸展树会保证
这棵树的中序遍历的结果是s[1..n]
即整个序列;
在进行旋转操作的时候,这个性质能被保持住;
伸展树在维护的时候;
每次会把需要操作的节点转到根节点;
然后进行对应的操作;
我们在进行
LCQ(x,y)的时候,
先二分枚举长度len;
然后把
x-1,x+len的节点编号获取a1,a2;
y-1,y+len的节点编号也获取b1,b2;
然后把a1转到根节点,a2放到根节点(也就是a1)的下面;
因为x+len>x-1所以a2肯定是在a1的右儿子处;
而这个时候a2的左子树代表的字符就是
s[x..x+len-1]了;
相应的对b1,b2也做同样的事情
也能获取s[y..y+len-1];
根据伸展树维护的hash值判断这两个子串是否相同。。
如果相同的话,就可以让len边长一点;
不同的话,肯定不能变长了,就变短一点呗.
然后返回答案就好
插入操作的话;
先提取x节点,把它转到根节点的位置;
然后再提取x+1号节点,把它转到根节点的下方
这里x+1号节点的左儿子肯定是空的,因为x和x+1是连在一起的;
中间不可能还有比x+1小的了;
则把这个新插入的节点放在x+1号节点的左边.
修改操作就简答多了
直接找到那个节点;
然后把它转到根节点去;
再修改它的值;
这里记住每次都把节点转到根节点就好
头部和尾部都要加一个空节点;
这样做写插入操作会好写一点吧?
hash值可以搜一下RKhash;
这里不用管它会溢出;
你开一个unsigned long long就可以毁天灭地了;
let it go~~
*/
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL unsigned long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define which(x) (ch[fa[x]][1]==x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 1e5 + 200;
const LL seed = 131;
char s[N], str[N], op[10], val[10];
int m, n, fa[N], tot, ch[N][2], root, x, siz[N];
LL po_w[N], has[N];
void push_up(int x)
{
siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + 1;
has[x] = has[ch[x][0]] + po_w[siz[ch[x][0]]] * str[x] + po_w[siz[ch[x][0]] + 1] * has[ch[x][1]];
}
int build(int l, int r, int rt)
{
if (l > r) return 0;
int mid = (l + r) >> 1;
int x = ++tot;
fa[x] = rt; str[x] = s[mid];
ch[x][0] = build(l, mid - 1, x);
ch[x][1] = build(mid + 1, r, x);
push_up(x);
return x;
}
int Rank(int x, int k)
{
if (siz[ch[x][0]] >= k)
return Rank(ch[x][0], k);
else
if (k == siz[ch[x][0]] + 1)
return x;
else
return Rank(ch[x][1], k - siz[ch[x][0]] - 1);
}
void Rotate(int x)
{
int f = fa[x];
bool k = which(x);
ch[f][k] = ch[x][!k];
ch[x][!k] = f;
ch[fa[f]][which(f)] = x;
fa[ch[f][k]] = f;
fa[x] = fa[f];
fa[f] = x;
siz[x] = siz[f], has[x] = has[f];
push_up(f);
}
void Splay(int x, int g)
{
while (fa[x] != g)
{
int f = fa[x];
if (fa[f] == g)
{
Rotate(x);
break;
}
if (which(x) ^ which(f))
Rotate(x);
else
Rotate(f);
Rotate(x);
}
if (!g) root = x;
}
void Change(int pos, char val)
{
int x = Rank(root, pos);
Splay(x, 0);
str[x] = val;
push_up(x);
}
void Insert(int pos, char val)
{
int x = Rank(root, pos), y = Rank(root, pos + 1);
Splay(x, 0), Splay(y, x);
ch[y][0] = ++tot;
str[tot] = val, fa[tot] = y;
push_up(tot), push_up(y), push_up(x);
}
int lcq(int tx, int ty)
{
int l = 0, r = n, ans = 0;
while (l <= r)
{
int mid = (l + r) >> 1;
if (ty + mid - 1 > n + 1) //?????
{
r = mid - 1;
continue;
}
//[tx..tx+mid-1] but (tx-1,tx+mid)
int x = Rank(root, tx - 1), y = Rank(root, tx + mid);
Splay(x, 0), Splay(y, x);
LL temp1 = has[ch[y][0]];
x = Rank(root, ty - 1), y = Rank(root, ty + mid);
Splay(x, 0), Splay(y, x);
if (temp1 == has[ch[y][0]])
{
ans = mid;
l = mid + 1;
}
else
r = mid - 1;
}
return ans;
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
po_w[0] = 1;
rep1(i, 1, N - 2)
po_w[i] = po_w[i - 1] * seed;
scanf("%s", s + 1);
n = strlen(s + 1);
root = build(0, n + 1, 0);
rei(m);
rep1(i, 1, m)
{
scanf("%s", op);
if (op[0] == 'R')
{
scanf("%d%s", &x, val);
Change(x + 1, val[0]);
}
else
if (op[0] == 'I')
{
scanf("%d%s", &x, val);
Insert(x + 1, val[0]);
n++;
}
else
if (op[0] == 'Q')
{
int x, y;
scanf("%d%d", &x, &y);
if (x > y)
swap(x, y);
if (x != y)
printf("%d\n", lcq(x + 1, y + 1));
else
printf("%d\n", n - x + 1);
}
}
return 0;
}
【BZOJ 1014】 [JSOI2008]火星人prefix的更多相关文章
- 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] ...
- [BZOJ 1014] [JSOI2008] 火星人prefix 【Splay + Hash】
题目链接:BZOJ - 1014 题目分析 求两个串的 LCP ,一种常见的方法就是 二分+Hash,对于一个二分的长度 l,如果两个串的长度为 l 的前缀的Hash相等,就认为他们相等. 这里有修改 ...
- BZOJ 1014: [JSOI2008]火星人prefix
Sol Splay+Hash+二分答案. 用Splay维护Hash,二分答案判断. 复杂度 \(O(nlog^2n)\) PS:这题调了两个晚上因为没开long long.许久不写数据结构题感觉写完整 ...
- bzoj 1014 [JSOI2008]火星人prefix(splay+hash)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1014 [题意] 给定一个字符串,要求提供修改一个字符,插入一个字符,查询两个后缀LCP ...
- bzoj 1014 [JSOI2008]火星人prefix——splay+哈希
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1014 用splay维护字符串,每个点记录子树的哈希值,然后二分查询. 二分不是把两个点的哈希 ...
随机推荐
- C#中对XML的操作
现在有一个xml文件,名称:BookStore.xml,数据如下: <?xml version="1.0" encoding="gb2312"?>& ...
- amazeui学习笔记--css(基本样式)--样式统一Normalize
amazeui学习笔记--css(基本样式)--样式统一Normalize 一.总结 1.统一浏览器默认样式: Amaze UI 也使用了 normalize.css,就是让不同浏览器显示相同的样式 ...
- 分治法(divide & conquer)与动态规划(dynamic programming)应用举例
动态规划三大重要概念:最优子结构,边界,状态转移公式(问题规模降低,如问题由 n 的规模降低为 n−1 或 n−2 及二者之间的关系): 0. 爬台阶 F(n)⇒F(n−1)+F(n−2) F(n−1 ...
- report_timing
report_timing -max_path 2 会报告两条路径,但不一定是最差的路径 report_timing -nworst 2 -max_path 2 会报告两条最差的路径
- Log4net.confager配置官方文档
http://logging.apache.org/log4net/release/config-examples.html
- akka---Getting Started Tutorial (Java): First Chapter
原文地址:http://doc.akka.io/docs/akka/2.0.2/intro/getting-started-first-java.html Introduction Welcome t ...
- poj 3100 (zoj 2818)||ZOJ 2829 ||ZOJ 1938 (poj 2249)
水题三题: 1.给你B和N,求个整数A使得A^n最接近B 2. 输出第N个能被3或者5整除的数 3.给你整数n和k,让你求组合数c(n,k) 1.poj 3100 (zoj 2818) Root of ...
- 12行Python暴力爬《黑豹》豆瓣短评
作者:黄嘉锋 来源:https://www.jianshu.com/p/ea0b56e3bd86 草长莺飞,转眼间又到了三月"爬虫月".这时往往不少童鞋写论文苦于数据获取艰难,辗转 ...
- bitnami WAMP stack使用方法(转)
想学习PHP,在网上找了些资料看了一下.介绍一个简单快速的服务器搭建方法,基于WAMP(WINDOWS+APATCH_MYSQL/MARIADB+PERL/PHP/PYTHON)架构.对应的也有LAM ...
- PatentTips - High-performance AHCI Interface
BACKGROUND OF THE INVENTION Various storage protocols for communicating with storage devices are kno ...