题意:给定一个长度为n的01串,你的任务是依次执行如表所示的m条指令:

1 p c 在第p个字符后插入字符,p = 0表示在整个字符串之前插入
2 p 删除第p个字符,后面的字符往前移
3 p1 p2反转第p1到第p2个字符
4 p1 p2输出从p1开始和p2开始的两个后缀的LCP。

析:对于前三个操作,splay 很容易就可以解决,但是对于最后一个操作,却不是那么容易,因为这是动态的,所以我们也要维护一个可以动态的,这就可以用Hash来解决,由于要翻转,所以要维护两个,一个正向的,一个反向的。在操作4时,先进行二分,然后用哈希进行判断,由于串不是太长,所以误差比较小。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
//#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e15;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 4e5 + 100;
const int mod = 3;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} // UVa 11996 #define Key_value ch[ch[root][1]][0]
int pre[maxn], ch[maxn][2], key[maxn], sz[maxn];
int root, tot1;
int rev[maxn];
int s[maxn], tot2;
char a[maxn];
ULL H[maxn], revH[maxn];
ULL xp[maxn]; void NewNode(int &rt, int fa, int x){
if(tot2) rt = s[tot2--];
else rt = ++tot1;
pre[rt] = fa;
key[rt] = H[rt] = revH[rt] = x;
ch[rt][0] = ch[rt][1] = 0;
rev[rt] = 0;
sz[rt] = 1;
} void push_up(int rt){
int l = ch[rt][0], r = ch[rt][1];
sz[rt] = sz[l] + sz[r] + 1;
H[rt] = key[rt] * xp[sz[r]] + H[r] + H[l] * xp[sz[r]+1];
revH[rt] = key[rt] * xp[sz[l]] + revH[l] + revH[r] * xp[sz[l]+1];
} void update_rev(int rt){
if(!rt) return ;
swap(ch[rt][0], ch[rt][1]);
swap(H[rt], revH[rt]);
rev[rt] ^= 1;
} void push_down(int rt){
if(rev[rt]){
update_rev(ch[rt][0]);
update_rev(ch[rt][1]);
rev[rt] = 0;
}
} void Build(int &rt, int l, int r, int fa){
if(l > r) return ;
int m = l+r >> 1;
NewNode(rt, fa, a[m] - '0');
Build(ch[rt][0], l, m-1, rt);
Build(ch[rt][1], m+1, r, rt);
push_up(rt);
} void Init(){
tot1 = root = tot2 = 0;
ch[root][0] = ch[root][1] = sz[root] = pre[root] = 0;
key[root] = 0; H[root] = revH[root] = 0;
NewNode(root, 0, -1);
NewNode(ch[root][1], root, -1);
scanf("%s", a);
Build(Key_value, 0, n-1, ch[root][1]);
push_up(ch[root][1]);
push_up(root);
} int Get_kth(int rt, int k){
push_down(rt);
int t = sz[ch[rt][0]] + 1;
if(t == k) return rt;
if(t > k) return Get_kth(ch[rt][0], k);
return Get_kth(ch[rt][1], k-t);
} void Rotate(int x, int k){
int y = pre[x];
push_down(y);
push_down(x);
ch[y][!k] = ch[x][k];
pre[ch[x][k]] = y;
if(pre[y]) ch[pre[y]][ch[pre[y]][1]==y] = x;
pre[x] = pre[y];
ch[x][k] = y;
pre[y] = x;
push_up(y);
} void Splay(int rt, int goal){
push_down(rt);
while(pre[rt] != goal){
if(pre[pre[rt]] == goal){
push_down(pre[rt]);
push_down(rt);
Rotate(rt, ch[pre[rt]][0] == rt);
continue;
}
push_down(pre[pre[rt]]);
push_down(pre[rt]);
push_down(rt);
int y = pre[rt];
int k = ch[pre[y]][0] == y;
if(ch[y][k] == rt){
Rotate(rt, !k);
Rotate(rt, k);
}
else{
Rotate(y, k);
Rotate(rt, k);
}
}
push_up(rt);
if(goal == 0) root = rt;
} void Insert(int pos){
scanf("%s", a);
Splay(Get_kth(root, pos+1), 0);
Splay(Get_kth(root, pos+2), root);
Build(Key_value, 0, 0, ch[root][1]);
push_up(ch[root][1]);
push_up(root);
++n;
} void Erase(int rt){
if(!rt) return ;
s[++tot2] = rt;
Erase(ch[rt][0]);
Erase(ch[rt][1]);
} void Delete(int pos){
Splay(Get_kth(root, pos), 0);
Splay(Get_kth(root, pos+2), root);
Erase(Key_value);
pre[Key_value] = 0;
Key_value = 0;
push_up(ch[root][1]);
push_up(root);
--n;
} void Reverse(int pos, int tot){
Splay(Get_kth(root, pos), 0);
Splay(Get_kth(root, pos+tot+1), root);
update_rev(Key_value);
push_up(ch[root][1]);
push_up(root);
} bool judge(int p1, int p2, int mid){
Splay(Get_kth(root, p1), 0);
Splay(Get_kth(root, p1+mid+1), root);
ULL ans = H[Key_value];
Splay(Get_kth(root, p2), 0);
Splay(Get_kth(root, p2+mid+1), root);
return ans == H[Key_value];
} int solve(int p1, int p2){
int l = 1, r = n - p2 + 1;
while(l <= r){
int mid = l + r >> 1;
if(judge(p1, p2, mid)) l = mid + 1;
else r = mid - 1;
}
return l - 1;
} int main(){
xp[0] = 1;
for(int i = 1; i < maxn; ++i) xp[i] = xp[i-1] * mod;
while(scanf("%d %d", &n, &m) == 2){
Init();
while(m--){
int op, p, q;
scanf("%d %d", &op, &p);
if(1 == op) Insert(p);
else if(2 == op) Delete(p);
else if(3 == op){
scanf("%d", &q);
Reverse(p, q-p+1);
}
else{
scanf("%d", &q);
printf("%d\n", solve(p, q));
}
}
}
return 0;
}

  

UVa 11996 Jewel Magic (splay + Hash + 二分)的更多相关文章

  1. UVA 11996 Jewel Magic —— splay、序列的分裂与合并、LCP的哈希算法

    #include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> ...

  2. UVA - 11996 Jewel Magic (Treap+二分哈希)

    维护一个01序列,一共四种操作: 1.插入一个数 2.删除一个数 3.反转一个区间 4.查询两个后缀的LCP 用Splay或者Treap都可以做,维护哈希值,二分求LCP即可. 注意反转序列的时候序列 ...

  3. 【bzoj1014】[JSOI2008]火星人prefix Splay+Hash+二分

    题目描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 8 9 10 ...

  4. 【BZOJ】1014: [JSOI2008]火星人prefix(splay+hash+二分+lcp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1014 题意:支持插入一个字符.修改一个字符,查询lcp.(总长度<=100000, 操作< ...

  5. bzoj1014: [JSOI2008]火星人prefix(splay+hash+二分)

    题目大意:一个字符串三个操作:①求两个后缀的LCP②插入一个字符③修改一个字符. 前几天刚学了hash+二分求lcp,就看到这题. 原来splay还能这么用?!原来splay模板这么好写?我以前写的s ...

  6. BZOJ 1014 [JSOI2008]火星人prefix (Splay + Hash + 二分)

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 8112  Solved: 2569[Submit] ...

  7. bzoj1014: [JSOI2008]火星人prefix splay+hash+二分

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  8. P4036 [JSOI2008]火星人(splay+hash+二分)

    P4036 [JSOI2008]火星人 Splay维护hash,查询二分 $a[x].vl=a[lc].vl*ha[a[rc].sz+1]+a[x].w*ha[a[rc].sz]+a[rc].vl$ ...

  9. UVA 12338 Anti-Rhyme Pairs(hash + 二分)题解

    题意:给出两个字符串的最大相同前缀. 思路:hash是要hash,不hash是不可能的.hash完之后从头遍历判断超时然后陷入沉默,然后告诉我这能二分orz,二分完就过了,写二分条件写了半天.不要用数 ...

随机推荐

  1. ubuntu下面搭建SolrCloud集群

    首先要先把ubuntu环境搭建好,配置好静态IP,我这边配置的是3台机子,solr搭建集群至少是2台. 192.168.0.15  主机 192.168.0.16  从机 192.168.0.17  ...

  2. Linux的内存管理机制

    原文作者:技术成就梦想 链接:http://ixdba.blog.51cto.com/2895551/541355 一 物理内存和虚拟内存          我们知道,直接从物理内存读写数据要比从硬盘 ...

  3. 利用html5制作一个时钟动画

    <canvas id="clock" width="500" height="500" style="background- ...

  4. small_vector

    folly/small_vector.h folly::small_vector<T,Int=1,...> is a sequence container that implements ...

  5. Change R source code

    If you'd like to simply test out the effect of that change in an interactive R session, you can do s ...

  6. [z]计算机架构中Cache的原理、设计及实现

    前言 虽然CPU主频的提升会带动系统性能的改善,但系统性能的提高不仅仅取决于CPU,还与系统架构.指令结构.信息在各个部件之间的传送速度及存储部件的存取速度等因素有关,特别是与CPU/内存之间的存取速 ...

  7. js将秒转换为 分:秒 函数

    /** * 将秒转换为 分:秒 * s int 秒数 */ function s_to_hs(s){ //计算分钟 //算法:将秒数除以60,然后下舍入,既得到分钟数 var h; h = Math. ...

  8. Hash表从了解到深入(浅谈)

    · Hasn表,将一个数据进行Value化,再进行一个映射关系到Key直接进行访问的一个数据结构,这样可以通过直接的计算进行数据的访问和插入.关于Hash表的基本概念这里就不一一叙述,可以通过百度了解 ...

  9. Sso单点登录分析

    1.   Sso系统分析 1.1. 什么是sso系统 SSO英文全称Single Sign On,单点登录.SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.它包括可以将这 ...

  10. javascript对变量和函数的声明提前‘hoist’

    hoist vt.升起,提起; vi.被举起或抬高; n.起重机,升降机; 升起; <俚>推,托,举; 原文地址:http://www.bootcss.com/article/variab ...