1. 程式人生 > >BZOJ 1014 [JSOI2008]火星人prefix (Splay + Hash + 二分)

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

uil 現在 scrip 滿足 gcd scan 地球 back define

1014: [JSOI2008]火星人prefix

Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 8112 Solved: 2569
[Submit][Status][Discuss]

Description

  火星人最近研究了一種操作:求一個字串兩個後綴的公共前綴。比方說,有這樣一個字符串:madamimadam,
我們將這個字符串的各個字符予以標號:序號: 1 2 3 4 5 6 7 8 9 10 11 字符 m a d a m i m a d a m 現在,
火星人定義了一個函數LCQ(x, y),表示:該字符串中第x個字符開始的字串,與該字符串中第y個字符開始的字串
,兩個字串的公共前綴的長度。比方說,LCQ(1, 7) = 5, LCQ(2, 10) = 1, LCQ(4, 7) = 0 在研究LCQ函數的過程
中,火星人發現了這樣的一個關聯:如果把該字符串的所有後綴排好序,就可以很快地求出LCQ函數的值;同樣,
如果求出了LCQ函數的值,也可以很快地將該字符串的後綴排好序。 盡管火星人聰明地找到了求取LCQ函數的快速
算法,但不甘心認輸的地球人又給火星人出了個難題:在求取LCQ函數的同時,還可以改變字符串本身。具體地說
,可以更改字符串中某一個字符的值,也可以在字符串中的某一個位置插入一個字符。地球人想考驗一下,在如此
復雜的問題中,火星人是否還能夠做到很快地求取LCQ函數的值。

Input

  第一行給出初始的字符串。第二行是一個非負整數M,表示操作的個數。接下來的M行,每行描述一個操作。操
作有3種,如下所示
1、詢問。語法:Qxy,x,y均為正整數。功能:計算LCQ(x,y)限制:1<=x,y<=當前字符串長度。
2、修改。語法:Rxd,x是正整數,d是字符。功能:將字符串中第x個數修改為字符d。限制:x不超過當前字
符串長度。
3、插入:語法:Ixd,x是非負整數,d是字符。功能:在字符串第x個字符之後插入字符d,如果x=0,則在字
符串開頭插入。限制:x不超過當前字符串長度

Output

  對於輸入文件中每一個詢問操作,你都應該輸出對應的答案。一個答案一行。

Sample Input

madamimadam
7
Q 1 7
Q 4 8
Q 10 11
R 3 a
Q 1 7
I 10 a
Q 2 11

Sample Output

5
1
0
2
1

HINT

1、所有字符串自始至終都只有小寫字母構成。

2、M<=150,000

3、字符串長度L自始至終都滿足L<=100,000

4、詢問操作的個數不超過10,000個。

對於第1,2個數據,字符串長度自始至終都不超過1,000

對於第3,4,5個數據,沒有插入操作。

Source

析:首先由於有插入還有修改,操作,用splay就可以解決,然後就是求lcp,這個可以用二分+Hash來解決,所以組合起來就好。

代碼如下:

#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>
#include <numeric>
#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(i,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 = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int maxn = 1e5 + 10;
const int maxm = 3e5 + 10;
const ULL 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;
}

#define Key_value ch[ch[root][1]][0]
int pre[maxn], ch[maxn][2], key[maxn], sz[maxn];
int root, tot1;
int s[maxn], tot2;
char a[maxn];
ULL H[maxn], xp[maxn];

void NewNode(int &rt, int fa, int x){
  if(tot2)  rt = s[tot2--];
  else  rt = ++tot1;
  pre[rt] = fa;
  key[rt] = x;
  ch[rt][0] = ch[rt][1] = 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] = H[r] + key[rt] * xp[sz[r]] + H[l] * xp[sz[r]+1];
}

void update_setv(int rt, int val){
  if(!rt)  return ;
  key[rt] = val;
  push_up(rt);
}

void Build(int &rt, int l, int r, int fa){
  if(l > r)  return ;
  int m = l+r >> 1;
  NewNode(rt, fa, a[m]);
  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;
  scanf("%s", a); n = strlen(a);
  NewNode(root, 0, -1);
  NewNode(ch[root][1], root, -1);
  Build(Key_value, 0, n-1, ch[root][1]);
  push_up(ch[root][1]);
  push_up(root);
}

int Get_kth(int rt, int k){
  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];
  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){
  while(pre[rt] != goal){
    if(pre[pre[rt]] == goal){
      Rotate(rt, ch[pre[rt]][0] == rt);
      continue;
    }
    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, tot = 1;
  scanf("%d", &pos);
  scanf("%s", a);
  Splay(Get_kth(root, pos+1), 0);
  Splay(Get_kth(root, pos+2), root);
  Build(Key_value, 0, tot-1, ch[root][1]);
  push_up(ch[root][1]);
  push_up(root);
  ++n;
}

bool judge(int x, int y, int mid){
  Splay(Get_kth(root, x), 0);
  Splay(Get_kth(root, x + mid + 1), root);
  ULL ans = H[Key_value];
  Splay(Get_kth(root, y), 0);
  Splay(Get_kth(root, y + mid + 1), root);
  return ans == H[Key_value];
}

int query(){
  int x, y;  scanf("%d %d", &x, &y);
  int l = 1, r = min(n - x + 1, n - y + 1);
  while(l <= r){
    int m = l + r >> 1;
    if(judge(x, y, m))  l = m + 1;
    else r = m - 1;
  }
  return l - 1;
}

void Make_setv(){
  int pos, tot = 1;
  scanf("%d", &pos);
  scanf("%s", a);
  Splay(Get_kth(root, pos), 0);
  Splay(Get_kth(root, pos+tot+1), root);
  update_setv(Key_value, a[0]);
  push_up(ch[root][1]);
  push_up(root);
}


int main(){
  xp[0] = 1;
  for(int i = 1; i < maxn; ++i)  xp[i] = xp[i-1] * mod;
  Init();
  scanf("%d", &m);
  char op[5];
  while(m--){
    scanf("%s", op);
    if(op[0] == ‘Q‘)  printf("%d\n", query());
    else if(op[0] == ‘R‘)  Make_setv();
    else Insert();
  }
  return 0;
}

  

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