传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1014

天,写kth()时,把判断条件k <= siz[ch[x][0]]错写成了k <= ch[x][0],RE不停,还爆掉了几个小时,以后写数据结构题一定要头脑清晰啊!

#include <cstdio>
#include <cstring> const int maxn = 300005, base = 131; int fa[maxn], ch[maxn][2], key[maxn], siz[maxn], root, cnt;
unsigned long long hash[maxn], poww[maxn];
int n, m, t1, t2, stk[maxn], top;
char s[maxn], opr, t3; inline void pushup(int x) {
hash[x] = hash[ch[x][0]] * (poww[siz[ch[x][1]] + 1]) + key[x] * poww[siz[ch[x][1]]] + hash[ch[x][1]];
siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + 1;
}
inline void rotate(int x) {
int y = fa[x];
if (y == ch[fa[y]][0]) {
ch[fa[y]][0] = x;
}
else {
ch[fa[y]][1] = x;
}
fa[x] = fa[y];
int dir = x == ch[y][1];
ch[y][dir] = ch[x][dir ^ 1];
fa[ch[x][dir ^ 1]] = y;
ch[x][dir ^ 1] = y;
fa[y] = x;
pushup(y);
pushup(x);
}
inline void splay(int x, int rt) {
int p;
while (fa[x] != rt) {
p = fa[x];
if (fa[p] == rt) {
rotate(x);
}
else {
if ((p == ch[fa[p]][1]) ^ (x == ch[p][1])) {
rotate(x);
}
else {
rotate(p);
}
rotate(x);
}
}
if (!rt) {
root = x;
}
}
int make_tree(int left, int right) {
if (left > right) {
return 0;
}
int rt = (left + right) >> 1;
key[rt] = s[rt];
ch[rt][0] = make_tree(left, rt - 1);
fa[ch[rt][0]] = rt;
ch[rt][1] = make_tree(rt + 1, right);
fa[ch[rt][1]] = rt;
pushup(rt);
return rt;
}
inline int kth(int k) {
int x = root;
while (k != siz[ch[x][0]] + 1) {
if (k <= siz[ch[x][0]]) {
x = ch[x][0];
}
else {
k -= siz[ch[x][0]] + 1;
x = ch[x][1];
}
}
return x;
}
inline void modify(int root1, int root2) {
root1 = kth(root1);
root2 = kth(root2);
splay(root1, 0);
splay(root2, root1);
}
inline void ist(int pos, int _key) {
modify(pos, pos + 1);
++cnt;
hash[cnt] = key[cnt] = _key;
siz[cnt] = 1;
ch[ch[root][1]][0] = cnt;
fa[cnt] = ch[root][1];
pushup(ch[root][1]);
pushup(root);
}
/*void ist(int x, int k, int _key) {
if (k == siz[ch[x][0]] + 1) {
if (ch[x][1]) {
top = 0;
int i;
for (i = ch[x][1]; ch[i][0]; i = ch[i][0]) {
stk[top++] = i;
}
++cnt;
ch[i][0] = cnt;
fa[cnt] = i;
key[cnt] = _key;
siz[cnt] = 1;
pushup(i);
for (i = top - 1; ~i; --i) {
pushup(stk[i]);
}
}
else {
++cnt;
ch[x][1] = cnt;
fa[cnt] = x;
key[cnt] = _key;
}
pushup(x);
splay(cnt, 0);
return;
}
if (k <= siz[ch[x][0]]) {
ist(ch[x][0], k, _key);
}
else {
ist(ch[x][1], k - siz[ch[x][0]] - 1, _key);
}
}*/
void upd(int x, int k, int _key) {
if (k == siz[ch[x][0]] + 1) {
key[x] = _key;
pushup(x);
return;
}
if (k <= siz[ch[x][0]]) {
upd(ch[x][0], k, _key);
}
else {
upd(ch[x][1], k - siz[ch[x][0]] - 1, _key);
}
pushup(x);
}
unsigned long long gethash(int start, int end) {
modify(start - 1, end + 1);
return hash[ch[ch[root][1]][0]];
} int main(void) {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
scanf("%s", s + 2);
n = strlen(s + 2);
s[1] = s[n + 2] = '$';
n += 2;
cnt = n;
poww[0] = 1;
for (int i = 1; i < maxn; ++i) {
poww[i] = poww[i - 1] * base;
}
root = make_tree(1, n);
scanf("%d", &m);
int left, right, mid;
while (m--) {
while ((opr = getchar()) < 'A');
scanf("%d", &t1);
++t1;
if (opr == 'I') {
while ((t3 = getchar()) < 'a');
ist(t1, t3);
++n;
}
else if (opr == 'R') {
while ((t3 = getchar()) < 'a');
upd(root, t1, t3);
}
else {
scanf("%d", &t2);
++t2;
left = 0;
right = n - (t1 > t2? t1: t2);
while (left != right) {
mid = (left + right + 1) >> 1;
if (gethash(t1, t1 + mid - 1) == gethash(t2, t2 + mid - 1)) {
left = mid;
}
else {
right = mid - 1;
}
}
printf("%d\n", left);
}
}
return 0;
}

  

最新文章

  1. C++基础-01
  2. XML文件的读取----cElementTree
  3. C++ string 转 char*
  4. Netty4.x中文教程系列(三) Hello World !详解
  5. 修改Eclipse的EasyExplore插件的键盘快捷键
  6. group by order by having
  7. 【5】说说Laravel5的blade模板
  8. [置顶] 程序员面试之道(《程序员面试笔试宝典》)之看着别人手拿大把的offer,不淡定了怎么办?
  9. leetcode第一刷_Largest Rectangle in Histogram
  10. EF架构~Migration数据迁移的执行顺序
  11. vue + element + 初始化项目
  12. Network POJ - 3417(LCA+dfs)
  13. OSI七层协议模型、TCP/IP四层模型和五层协议体系结构之间的关系
  14. 《锋利的jQuery》笔记:插件的使用和写法
  15. Django中模型层中ORM的多表操作
  16. TryEnterCriticalSection___Delphi
  17. Snmp学习总结(二)——WinXP安装和配置SNMP
  18. BZOJ 1845三角形面积并
  19. EXPLAIN执行计划中要重点关注哪些要素(转)
  20. EJB是什么

热门文章

  1. 学习swift从青铜到王者之swift枚举07
  2. 学习MarkDown--初体验
  3. C#实现如何判断一个数组中是否有重复的元素 返回一个数组升序排列后的位置信息--C#程序举例 求生欲很强的数据库 别跟我谈EF抵抗并发,敢问你到底会不会用EntityFramework
  4. 学习Opencv 2.4.9 (一)---Opencv + vs2012环境配置
  5. leetCode(26):Unique Binary Search Trees
  6. Android Studio keymap到Eclipse后,查找下一个同样变量快捷键Ctrl+K失效
  7. 2016/3/30 ①投票checkbox ②进度条两个div套起百分比控制内div(width) &lt;div&gt;&lt;div&gt;&lt;/div&gt;&lt;/div&gt; ③数据库test2 表 diaoyan... 35岁发展方向投票
  8. C项目实践--俄罗斯方块(1)
  9. Synthesizing Images of Humans in Unseen Poses
  10. Lightoj 1023 - Discovering Permutations