2887 Big String
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的更多相关文章
- POJ 2887 Big String(块状链表)
题目大意 给一个字符串,长度不超过 106,有两种操作: 1. 在第 i 个字符的前面添加一个字符 ch 2. 查询第 k 个位置是什么字符 操作的总数不超过 2000 做法分析 好多不同的做法都可以 ...
- poj 2887 Big String
题目连接 http://poj.org/problem?id=2887 Big String Description You are given a string and supposed to do ...
- Poj 2887 Big String(块状数组)
Big String Time Limit: 1000MS Memory Limit: 131072K Description You are given a string and supposed ...
- POJ 2887 Big String (块状数组)
题意:给一个字符串(<=1000000)和n个操作(<2000),每个操作可以在某个位置插入一个字符,或者查询该位置的字符.问查询结果. 思路:块状数组. 如果将原来的字符串都存在一起,每 ...
- Poj 2887-Big String Splay
题目:http://poj.org/problem?id=2887 Big String Time Limit: 1000MS Memory Limit: 131072K Total ...
- POJ 2887:Big String(分块)
http://poj.org/problem?id=2887 题意:给出一个字符串,还有n个询问,第一种询问是给出一个位置p和字符c,要在位置p的前面插入c(如果p超过字符串长度,自动插在最后),第二 ...
- Big String(poj 2887)
题意: 给你一个不超过1e6的字符串,和不超过2000次的操作 操作分为两种: 1.将一个字符插入到某个位置的前面 2.询问当前位置的字符 /* 块状链表模板水题(我的智商也就能做这种题了). 观察题 ...
- 透过WinDBG的视角看String
摘要 : 最近在博客园里面看到有人在讨论 C# String的一些特性. 大部分情况下是从CODING的角度来讨论String. 本人觉得非常好奇, 在运行时态, String是如何与这些特性联系上的 ...
- JavaScript String对象
本编主要介绍String 字符串对象. 目录 1. 介绍:阐述 String 对象的说明以及定义方式. 2. 实例属性:介绍 String 对象的实例属性: length. 3. 实例方法:介绍 St ...
随机推荐
- [转]Qt 5.5 操作 Excel 的速度 效率问题
转自:http://blog.csdn.net/li494816491/article/details/50274305 1. QAxObject *_excelObject1 =newQAxObje ...
- [转载]ant和maven的区别
Ant是软件构建工具,Maven的定位是软件项目管理和理解工具.Maven除了具备Ant的功能外,还增加了以下主要的功能: 1)使用Project Object Model来对软件项目管理: 2)内置 ...
- Codeforces GYM 100741A . Queries
time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input output stan ...
- 在uwp仿IOS的页面切换效果
有时候我们需要编写一些迎合IOS用户使用习惯的uwp应用,我在这里整理一下仿IOS页面切换效果的代码. 先分析IOS的页面切换.用户使用左右滑动方式进行前进和后退,播放类似于FlipView的切换动画 ...
- Python 语言规范
Python 语言规范 pychecker 对你的代码运行pychecker 定义: pychecker 是一个在Python 源代码中查找bug 的工具. 对于C 和C++这样的不那 么动态的( ...
- 简洁的KVO -- 使用Block响应事件
涉及内容: KVO,Runtime,Category,Block 首先创建NSObject的Category 举个例子是这样的: 随后定义你需要响应的Block结构 我简单一点就这样咯 typedef ...
- Linux常用的操作指令
1.pwd-显示当前所在位置 2.cd-进入当前目录 3.cd..-返回上一级目录 4..ls命令参数选项有很多,ls也是经常使用到的命令.如果不清楚命令的使用方式可以直接 ls --help来查看 ...
- OpenCV2:第六章 图像几何变换
一.简介 图像的几何变换有距离变换 坐标映射 平移 镜像 旋转 缩放 仿射变换等 二.重映射 把一张图像重新排列像素,比如倒置 CV_EXPORTS_W void remap( InputArr ...
- c# xml本地化用法
1.普通格式 2.占位符格式 注意事项: 1.Pascal命名法 2.key只是key,中间不需要空格,value可以空格 3.占位符左右两边分别空一格
- Node.js实现简单的爬取
学习[node.js]也有几天时间了,所以打算写着练练手:索然我作为一个后端的选手,写起来还有那么一丝熟悉的感觉.emmm~~ ‘货’不多讲 ,开搞........ 首先是依赖选择: 代码块如下: ...