splay瞎搞一下,正解是分块数组或分块链表,但是学不会啊!

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<string>
#include<set>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<cmath>
#include<cstring>
#include<map>
#include<stack>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 2005
#define ull unsigned long long
#define ll long long
#define hashmod 99999839
#define mod 9997
int son[maxn][],fa[maxn],sz[maxn],root,len,q;
char s[maxn],v[maxn],ch;
//kpmsrzxxzu
inline void update(int o){
sz[o] = ;
if(son[o][] != -) sz[o] += sz[son[o][]];
if(son[o][] != -) sz[o] += sz[son[o][]];
}
inline int isrson(int p,int f){
if(f == -) return ;
return son[f][] == p;
}
inline void changeson(int p,int f,int v){
if(f != -) son[f][v] = p;
if(p != -) fa[p] = f;
if(f == -) root = p;
}
int build(int l,int r){
if(l > r) return -;
int o = len;
len++;
if(l == r){son[o][] = son[o][] = -,sz[o] = ,v[o] = s[l];return o;}
int mid = (l + r) >> ,ls,rs;
v[o] = s[mid],ls = build(l,mid-),rs = build(mid+,r);
changeson(ls,o,),changeson(rs,o,),update(o);
return o;
}
void rotate(int p){//只会出现祖辈不存在的情况
int f = fa[p],g = fa[f];
int u = isrson(p,f),v = isrson(f,g);
changeson(son[p][u^],f,u),changeson(f,p,u^),changeson(p,g,v);
update(f);
}
void splay(int p,int tar){
if(fa[p] == tar) return;
while(fa[p] != tar && fa[fa[p]] != tar){
int f = fa[p],g = fa[f];//保证了父亲及其祖先存在
int u = isrson(p,f),v = isrson(f,g);
if(u ^ v) rotate(p),rotate(p);
else rotate(f),rotate(p);
}
if(fa[p] != tar) rotate(p);
update(p);
}
int findtr(){
int p = root,ls = son[p][],rs = son[p][];
if(q >= sz[p]) q = sz[p] - ;
while(){
if(ls == -){
if(q == ) break;
else p = rs,q--;
ls = son[p][],rs = son[p][];
continue;
}
if(sz[ls] >= q) p = ls;
else if(sz[ls] + == q) break;
else p = rs,q = q - sz[ls] - ;
ls = son[p][],rs = son[p][];
}
return p;
}
void insert(){
int p = findtr();
splay(p,-);
int o = len;
len++,v[o] = ch;
changeson(root,o,),changeson(son[root][],o,);
son[root][] = -,fa[o] = -;
update(root);
root = o;
update(root);
}
void query(){
int p = findtr();
printf("%c\n",v[p]);
splay(p,-);
}
signed main(){
// freopen("a.in","r",stdin);
// freopen("b.out","w",stdout);
// int k = 0;
while(~scanf("%s",s + )){
int t = strlen(s + );
len = ;
s[] = '\0',s[t + ] = '\0';
root = build(,t + );
fa[root] = -;
int n;
char op[];
scanf("%d",&n);
// printf("Case %d:\n",++k);
for(register int i = ;i <= n;++i){
scanf("%s",op);
if(op[] == 'Q') scanf("%d",&q),q++,query();
else scanf("%s%d",op,&q),ch = op[],insert();
}
}
return ;
}

2887 Big String的更多相关文章

  1. POJ 2887 Big String(块状链表)

    题目大意 给一个字符串,长度不超过 106,有两种操作: 1. 在第 i 个字符的前面添加一个字符 ch 2. 查询第 k 个位置是什么字符 操作的总数不超过 2000 做法分析 好多不同的做法都可以 ...

  2. poj 2887 Big String

    题目连接 http://poj.org/problem?id=2887 Big String Description You are given a string and supposed to do ...

  3. Poj 2887 Big String(块状数组)

    Big String Time Limit: 1000MS Memory Limit: 131072K Description You are given a string and supposed ...

  4. POJ 2887 Big String (块状数组)

    题意:给一个字符串(<=1000000)和n个操作(<2000),每个操作可以在某个位置插入一个字符,或者查询该位置的字符.问查询结果. 思路:块状数组. 如果将原来的字符串都存在一起,每 ...

  5. Poj 2887-Big String Splay

    题目:http://poj.org/problem?id=2887       Big String Time Limit: 1000MS   Memory Limit: 131072K Total ...

  6. POJ 2887:Big String(分块)

    http://poj.org/problem?id=2887 题意:给出一个字符串,还有n个询问,第一种询问是给出一个位置p和字符c,要在位置p的前面插入c(如果p超过字符串长度,自动插在最后),第二 ...

  7. Big String(poj 2887)

    题意: 给你一个不超过1e6的字符串,和不超过2000次的操作 操作分为两种: 1.将一个字符插入到某个位置的前面 2.询问当前位置的字符 /* 块状链表模板水题(我的智商也就能做这种题了). 观察题 ...

  8. 透过WinDBG的视角看String

    摘要 : 最近在博客园里面看到有人在讨论 C# String的一些特性. 大部分情况下是从CODING的角度来讨论String. 本人觉得非常好奇, 在运行时态, String是如何与这些特性联系上的 ...

  9. JavaScript String对象

    本编主要介绍String 字符串对象. 目录 1. 介绍:阐述 String 对象的说明以及定义方式. 2. 实例属性:介绍 String 对象的实例属性: length. 3. 实例方法:介绍 St ...

随机推荐

  1. BST AVL RBT B- B+ 的一些理解

    BST(二叉查找树,排序二叉树),如果数据有序的话,组成的二叉树会形成单列的形式,导致查询效率低AVL(平衡二叉树) 使树的左右高度差的绝对值不超过2,保证了查询效率.但是插入和删除会带来多次旋转,导 ...

  2. MongoDB最简单的入门教程之五-通过Restful API访问MongoDB

    通过前面四篇的学习,我们已经在本地安装了一个MongoDB数据库,并且通过一个简单的Spring boot应用的单元测试,插入了几条记录到MongoDB中,并通过MongoDB Compass查看到了 ...

  3. MySQL数据表查询操作

    准语法结构:编写DQL时一定要严格按照此语法的顺序来实现!/* SELECT [ALL | DISTINCT] ALL表示查询出所有的内容 DISTINCT 去重 {* | 表名.* | 表名.字段名 ...

  4. 使用正则进行HTML页面属性的替换

    使用正则表达式拼接富文本框 package com.goboosoft.common.utils; import org.apache.commons.lang3.StringUtils; impor ...

  5. Linux基础命令——查看进程命令

    linux是一个 多进程   多用户的操作系统 ps(显示当前进程的状态) ps -ef  查看当前linux 进程 ps -ef | grep 'mysqld'  过滤mysql的进程 (grep  ...

  6. JavaEE-09 Ajax与jQuery在JavaEE项目中的使用

    学习要点 JavaScript实现Ajax jQuery实现Ajax JSON JSON-LIB FastJSON JavaScript实现Ajax 认识Ajax 旧版百度地图 百度搜索自动补全 百度 ...

  7. ZOJ3228 Searching the String (AC自动机)

    Searching the String Time Limit: 7 Seconds                                      Memory Limit: 129872 ...

  8. 谈谈你对java的理解

    这个题目是考察多个方面 但是要回答出关键点: 1.平台无关性 2.GC 3.语言特性.泛型.反射.lamda 4.面向对象 5.类库 6.异常处理

  9. 框模型中设置内容区域元素占地尺寸box-sizing属性

    盒子模型有两种 一种是 内容盒子模型(content-box)一种是边框盒子模型(border-box). content-box:设置的尺寸,只设置内容区域, 左外边距+左边框+左内边距+内容区域宽 ...

  10. 任务五:零基础HTML及CSS编码(二)

    面向人群: 零基础或初学者 难度: 简单 重要说明 百度前端技术学院的课程任务是由百度前端工程师专为对前端不同掌握程度的同学设计.我们尽力保证课程内容的质量以及学习难度的合理性,但即使如此,真正决定课 ...