【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维护字符串,每个点记录子树的哈希值,然后二分查询. 二分不是把两个点的哈希 ...
 
随机推荐
- 自己写的Android图表库XCL-Charts一些旧的样例
			
话说有了灵感就要抓住,来了兴趣就要去研究它. 所以尽管近期非常忙.但我还是没有丢下Android图表实现的研究.最终如今我的图表库基类 基本上已经有点模样了.不在是小打小闹,而是能依传入參数非常灵活的 ...
 - LED恒流设计
 - 关于jsonp跨域的问题以及解决方法(跨域、同源与非同源)
			
什么是跨域? 想要了解跨域,首先需要了解下浏览器的同源机制: JSONP和AJAX相同,都是客户端向服务器端发送请求:给服务器端传递数据 或者 从服务器端获取数据 的方式 JSONP属于非同源策略(跨 ...
 - FZU Problem 2168 防守阵地 I
			
http://acm.fzu.edu.cn/problem.php?pid=2168 题目大意: 给定n个数和m,要求从n个数中选择连续的m个,使得a[i]*1+a[i+1]*2+--a[i+m]*m ...
 - 代码高亮显示——google-code-prettify
			
先放着,搭建完HEXO博客再来写这篇. https://code.google.com/archive/p/google-code-prettify/
 - GO语言学习(二)Windows 平台下 LiteIDE 的安装和使用
			
1. 安装 Go 语言并设置环境变量 参考GO语言学习(一) 2. MinGW 的下载和安装 Windows 下的 Go 调试还需要安装 MinGW. 2.1 下载安装工具的安装 最新版本下载安装工具 ...
 - Storm新特性之Flux
			
Storm新特性之Flux Flux是Storm版本号0.10.0中的新组件,主要目的是为了方便拓扑的开发与部署.原先在开发Storm拓扑的时候整个拓扑的结构都是硬编码写在代码中的,当要对其进行改动时 ...
 - 【习题 3-6 UVA - 232】Crossword Answers
			
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟题.注意场宽为3 [代码] #include <bits/stdc++.h> using namespace std ...
 - Android ViewGroup使用小结
			
ViewGroup定义 在api中是这么描写叙述ViewGroup的:A ViewGroup is a special view that can contain other views. 依据意思我 ...
 - jQuery实现多种切换效果的图片切换的五款插件
			
1:Nivo SliderNivoslider:丰富的图片切换效果 官方网址:https://themeisle.com/plugins/nivo-slider 查看演示:https://www.he ...