UVa 11996 Jewel Magic (splay + Hash + 二分)
Posted dwtfukgv
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 + Hash + 二分)
UVA - 11996 可持久化Treap 维护Hash(仅观赏)