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. casting in C++

    这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=39 February 20, 2013 casting in C++ Fil ...

  2. 获取页面URL两种方式

    以请求http://localhost:8080/doctor/demo?code=1为例 一:用java代码获取 //获取URL中的请求参数.即?后的条件 code=1 String querySt ...

  3. Types of Security Vulnerabilities

    1)内存空间安全.2)参量级别数据安全:3)通信级别数据安全:4)数据访问控制:5)通信对象身份确认. https://developer.apple.com/library/content/docu ...

  4. nginx 的编译安装及基本操作

    下载nginx [root@nginx ~]# wget http://nginx.org/download/nginx-1.14.0.tar.gz --2019-05-02 21:52:23-- h ...

  5. C程序(1)

  6. Android UI: LinearLayout中layout_weight 属性的使用规则

    首先来查看android sdk文档,有这么一段话 LinearLayout also supports assigning a weight to individual children with ...

  7. openjudge-1664 放苹果

    总时间限制: 1000ms 内存限制: 65536kB 描述 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. 输 ...

  8. [LUOGU] P2704 炮兵阵地

    题目描述 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队. 一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示), 也可能是平原(用"P&q ...

  9. ps指令详解

    ps aux #显示出系统上的全部进程ps -ef #显示出系统上的全部进程,且显示出PPID一栏ps -ljF #仅显示与本终端上开启的进程 选项:-t 终端名称1 终端名称2 #指定关联的多个终端 ...

  10. 第二讲:vcs debugging basics

    要求: 1.describe three methods of debugging verilog code using vcs 2.invoke ucli debugger(不重要) 3.debug ...