UVa 11996 Jewel Magic (splay + Hash + 二分)
题意:给定一个长度为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 + 二分)的更多相关文章
- UVA 11996 Jewel Magic —— splay、序列的分裂与合并、LCP的哈希算法
#include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> ...
- UVA - 11996 Jewel Magic (Treap+二分哈希)
维护一个01序列,一共四种操作: 1.插入一个数 2.删除一个数 3.反转一个区间 4.查询两个后缀的LCP 用Splay或者Treap都可以做,维护哈希值,二分求LCP即可. 注意反转序列的时候序列 ...
- 【bzoj1014】[JSOI2008]火星人prefix Splay+Hash+二分
题目描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 8 9 10 ...
- 【BZOJ】1014: [JSOI2008]火星人prefix(splay+hash+二分+lcp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1014 题意:支持插入一个字符.修改一个字符,查询lcp.(总长度<=100000, 操作< ...
- bzoj1014: [JSOI2008]火星人prefix(splay+hash+二分)
题目大意:一个字符串三个操作:①求两个后缀的LCP②插入一个字符③修改一个字符. 前几天刚学了hash+二分求lcp,就看到这题. 原来splay还能这么用?!原来splay模板这么好写?我以前写的s ...
- BZOJ 1014 [JSOI2008]火星人prefix (Splay + Hash + 二分)
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 8112 Solved: 2569[Submit] ...
- bzoj1014: [JSOI2008]火星人prefix splay+hash+二分
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- 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$ ...
- UVA 12338 Anti-Rhyme Pairs(hash + 二分)题解
题意:给出两个字符串的最大相同前缀. 思路:hash是要hash,不hash是不可能的.hash完之后从头遍历判断超时然后陷入沉默,然后告诉我这能二分orz,二分完就过了,写二分条件写了半天.不要用数 ...
随机推荐
- ansible之感冒药
Ansible简介安装 Ansible是一个综合的强大的管理工具,他可以对多台主机安装操作系统,并为这些主机安装不同的应用程序,也可以通知指挥这些主机完成不同的任务.查看多台主机的各种信息的状态等,a ...
- 使用Ajax解析数据遇到的问题
数据格式 我最近在使用JQuery的$.ajax访问后台的时候,发现竟然无法解析返回的数据,具体的错误记不清了(以后在遇到问题先截个图),可以在浏览器的Console中看到一个错误,但是去看这条请求是 ...
- 【BZOJ】1218: [HNOI2003]激光炸弹(前缀和)
题目 题目描述 输入输出格式 输入格式: 输入文件名为input.txt 输入文件的第一行为正整数n和正整数R,接下来的n行每行有3个正整数,分别表示 xi,yi ,vi . 输出格式: 输出文件名为 ...
- START WITH...CONNECT BY PRIOR详解
START WITH...CONNECT BY PRIOR详解 START WITH...CONNECT BY PRIOR详解 ORACLE中的SELECT语句可以用START WITH...CONN ...
- Linux基础综合练习
Linux基本操作综合练习 1.建立用户zhangsan,密码使用明文123456: 命令:useradd -p 123456 zhangsan 解释: 参数 -p 添加明文密码 useradd添加用 ...
- python与冒泡排序
上一篇文章,介绍了一个非常快的排序算法--桶排序,但是它的缺点就是太耗资源了,这次要实现的算法就不用太耗资源了,它就是冒泡排序. 问题提出: 将以下数据升序排列:9, 2, 8, 6, 4 冒泡排序原 ...
- [Z]图灵奖获得者Richard Karp讲述Berkeley CS的发展史
A Personal View of Computer Science at Berkeley 赤裸裸的吊炸天
- 5、数据类型三:hash
Hash数据类型使用很普遍,它同样是key-value的方式来组织的,只是其value又包含多个field-fieldValue对.想要获取某个fieldValue,可以通过key-field联合来定 ...
- [转] 从数据库中读取图片并导入Excel文件,C#方式
原文地址, 作者 Lvyou1980 直接源码吧. using System; using System.IO; using System.Data; using System.Drawing; us ...
- Angularjs Ng_repeat中实现复选框选中并显示不同的样式
最近做了一个选择标签的功能,把一些标签展示给用户,用户选择自己喜欢的标签,就类似我们在购物网站看到的那种过滤标签似的: 简单的效果如图所示: 首先看一下html代码: 1 <!DOCTYPE h ...