UVa11996 Jewel Magic 魔法珠宝

传送门


这一看就时一道平衡树的题。


前三个操作好办,关键是怎么找lcp。
其实也不难,就是二分长度,然后判断哈希值是否相等。
而splay是可以维护哈希值的,而且这道题的数据并没有用hashkiller的极限数据,因此直接用溢出哈希就可以过。


那么具体是怎么维护呢?
首先因为有区间反转,所以要维护该区间的哈希值和反转的哈希值。
举个栗子来说明:对于区间"abcxde",左子树里存的是"abc"的哈希值\(h_1\),该节点存的是"x"的哈希值\(h\),右子树存的是"de"的哈希值\(h_2\),那么想得到"abcxde"的哈希值,就是\(h_1*bas^{2+1}+h*bas^2+h_2\),即乘以哈希底数的右区间长度次方。
反转的哈希值同理。


于是这道题就没什么难点了呀,平衡树熟练的话一会儿就写完了。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<queue>
#include<assert.h>
#include<ctime>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
#define forE(i, x, y) for(int i = head[x], y; ~i && (y = e[i].to); i = e[i].nxt)
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 2e5 + 5;
const ull BAS = 233;
In ll read()
{
	ll ans = 0;
	char ch = getchar(), las = ' ';
	while(!isdigit(ch)) las = ch, ch = getchar();
	while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
	if(las == '-') ans = -ans;
	return ans;
}
In void write(ll x)
{
	if(x < 0) x = -x, putchar('-');
	if(x >= 10) write(x / 10);
	putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
	freopen(".in", "r", stdin);
	freopen(".out", "w", stdout);
#endif
}

int n, m;
char a[maxn];
ull p[maxn];

#define ls t[now].ch[0]
#define rs t[now].ch[1]
struct Splay
{
	int ch[2], fa;
	int val, siz, rev;
	ull h[2];
}t[maxn << 1];
int root = 0, cnt = 0;
In void pushdown(int now)
{
	if(now && t[now].rev)
	{
		t[ls].rev ^= 1, t[rs].rev ^= 1;
		swap(t[ls].h[0], t[ls].h[1]);	//别忘了交换正反哈希值 
		swap(t[rs].h[0], t[rs].h[1]);
		swap(ls, rs);
		t[now].rev = 0; 
	}
}
In void pushup(int now)
{
	t[now].siz = t[ls].siz + t[rs].siz + 1;
	t[now].h[0] = t[ls].h[0] * p[t[rs].siz + 1] + p[t[rs].siz] * t[now].val + t[rs].h[0];
	t[now].h[1] = t[rs].h[1] * p[t[ls].siz + 1] + p[t[ls].siz] * t[now].val + t[ls].h[1];
}
void rotate(int x)
{
	int y = t[x].fa, z = t[y].fa, k = (t[y].ch[1] == x);
  	t[z].ch[t[z].ch[1] == y] = x; t[x].fa = z;
  	t[y].ch[k] = t[x].ch[k ^ 1]; t[t[y].ch[k]].fa = y;
  	t[x].ch[k ^ 1] = y; t[y].fa = x;
  	pushup(y); pushup(x);
}
In void splay(int x, int s)
{
	while(t[x].fa ^ s)
	{
		int y = t[x].fa, z = t[y].fa;
		if(z ^ s) ((t[z].ch[0] == y) ^ (t[y].ch[0] == x)) ? rotate(x) : rotate(y);
		rotate(x);
	}
	if(!s) root = x;
}
In int build(int L, int R, int f)
{
	if(L > R) return 0;
	int mid = (L + R) >> 1, now = ++cnt;
	t[now].val = a[mid] - '0', t[now].fa = f;
	ls = build(L, mid - 1, now);
	rs = build(mid + 1, R, now);
	pushup(now);
	return now;
}
In int getRank(int k)
{
	int now = root;
	while("HA")
	{
		pushdown(now);
		if(t[ls].siz >= k) now = ls;
		else if(t[ls].siz + 1 == k) return now;
		else k -= t[ls].siz + 1, now = rs;
	}
}

In void getRange(int L, int R)
{
	int a = getRank(L), b = getRank(R + 2);
	splay(a, 0), splay(b, a);
	pushdown(root), pushdown(t[root].ch[1]);
}
#define rr t[root].ch[1]
In void insert(int x, char c)
{
	getRange(x + 1, x);
	int now = ++cnt, f = rr;
	t[now].fa = f, t[f].ch[0] = now, t[now].siz = 1;
	t[now].h[0] = t[now].h[1] = t[now].val = c - '0';
}
In void erase(int x)
{
	getRange(x, x);
	int& now = t[rr].ch[0];
	t[now].fa = 0; now = 0;
}
In void reverse(int L, int R)
{
	getRange(L, R);
	int now = t[rr].ch[0];
	t[now].rev ^= 1;
	swap(t[now].h[0], t[now].h[1]);
}
In bool check(int p1, int p2, int len)
{
	getRange(p1, p1 + len - 1);
	ull h1 = t[t[rr].ch[0]].h[0];
	getRange(p2, p2 + len - 1);
	ull h2 = t[t[rr].ch[0]].h[0];
	return h1 == h2;
}
In int lcp(int p1, int p2)
{
	int L = 0, R = min(n - p1 + 1, n - p2 + 1);
	while(L < R)
	{
		int mid = (L + R + 1) >> 1;
		if(check(p1, p2, mid)) L = mid;
		else R = mid - 1;
	}
	return L;
}

int main()
{
//	MYFILE();
	p[0] = 1;
	for(int i = 1; i < maxn; ++i) p[i] = p[i - 1] * BAS;
	n = read(), m = read();
	scanf("%s", a + 2);
	a[1] = '2', a[n + 2] = '2';
	root = build(1, n + 2, 0); 
	char s[2];
	for(int i = 1; i <= m; ++i)
	{
		int op = read(), p1 = read();
		if(op == 1)
		{
			scanf("%s", s);
			insert(p1, s[0]);
			++n;
		}
		else if(op == 2) erase(p1), n--;
		else if(op == 3) reverse(p1, read());
		else write(lcp(p1, read())), enter;
	}
	return 0;
}
posted @ 2021-04-21 15:43  mrclr  阅读(40)  评论(0编辑  收藏  举报