前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >BZOJ1014: [JSOI2008]火星人prefix(splay 二分 hash)

BZOJ1014: [JSOI2008]火星人prefix(splay 二分 hash)

作者头像
attack
发布2018-12-19 18:15:06
5270
发布2018-12-19 18:15:06
举报

题意

题目链接

Sol

一眼splay + 二分hash,不过区间splay怎么写来着呀

试着写了两个小时发现死活不对

看了一下yyb的代码发现自己根本就不会splay。。。。

代码语言:javascript
复制
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define ull unsigned long long 
using namespace std;
const int MAXN = 1e6 + 10;
const ull base = 27;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, M;
char s[MAXN];
int root, tot, ch[MAXN][2], fa[MAXN], siz[MAXN];
ull ha[MAXN], val[MAXN], po[MAXN];
bool rev[MAXN];
int ident(int x) {
    return ch[fa[x]][1] == x;
}
void connect(int x, int f, int id) {
    fa[x] = f; ch[f][id] = x;
}
void update(int x) {
    siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + 1;
    ha[x] = ha[ch[x][0]] + val[x] * po[siz[ch[x][0]]] + po[siz[ch[x][0]] + 1] * ha[ch[x][1]];
}
void rotate(int x) {
    int y = fa[x], R = fa[y], Yson = ident(x), Rson = ident(y);
    int B = ch[x][Yson ^ 1];
    connect(x, R, Rson); connect(B, y, Yson); connect(y, x, Yson ^ 1);
    update(y); update(x); 
}
void splay(int x, int to) {
    //to = fa[to];
    while(fa[x] != to) {
        if(fa[fa[x]] == to) rotate(x);
        else if(ident(x) == ident(fa[x])) rotate(fa[x]), rotate(x);
        else rotate(x), rotate(x);
    }
    if(!to) root = x; update(x);
}
int find(int k) {
    int x = root;
    while(1) {
        if(siz[ch[x][0]] + 1 == k) return x;
        if(siz[ch[x][0]] + 1 < k) k -= siz[ch[x][0]] + 1, x = ch[x][1];//tag
        else x = ch[x][0];
    }
}
ull gethash(int l, int len) {
    int x = find(l), y = find(l + len + 1);
    splay(x, 0); splay(y, x); 
    return ha[ch[y][0]];
}
int LCP(int y, int x) {
    int l = 0, r = tot - max(x, y) - 1, ans = 0;
    while(l <= r) {
        int mid = l + r >> 1;
        if(gethash(x, mid) == gethash(y, mid)) l = mid + 1, ans = mid;
        else r = mid - 1;
    }
    return ans;
}
void insert(int x, int v) {
    int l = find(x + 1), r = find(x + 2);
    splay(l, 0); splay(r, l);
    val[++tot] = v; fa[tot] = r; ch[r][0] = tot; splay(tot, 0); 
}
void change(int x, int v) {
    int l = find(x), r = find(x + 2);
    splay(l, 0); splay(r, l);
    val[ch[r][0]] = v; update(ch[r][0]); 
    update(r); update(l);
}
int main() {
//  freopen("a.in", "r", stdin);freopen("a.out", "w", stdout);
    po[0] = 1; 
    for(int i = 1; i <= (int)1e5; i++) po[i] = base * po[i - 1];
    scanf("%s", s + 1); N = strlen(s + 1);  
    ch[1][1] = 2; fa[2] = 1; tot = 2; root = 1; update(2); update(1); 
    for(int i = 1; i <= N; i++) 
        insert(i - 1, s[i]);
    int M = read();
    while(M--) {
        char opt[3]; scanf("%s", opt);
        if(opt[0] == 'Q') {int x = read(), y = read(); printf("%d\n", LCP(x, y));}
        else if(opt[0] == 'R') {
            int x = read(); scanf("%s", opt + 1);
            change(x, opt[1]);
        } else {
            int x = read(); scanf("%s", opt + 1);
            insert(x, opt[1]); 
        }
    }
    return 0;
}
/*
*/
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-11-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意
  • Sol
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档