Poj 2887-Big String Splay
| Big String 
 Description You are given a string and supposed to do some string manipulations. Input The first line of the input contains the initial string. You can assume that it is non-empty and its length does not exceed 1,000,000. The second line contains the number of manipulation commands N (0 < N ≤ 2,000). The following N lines describe a command each. The commands are in one of the two formats below: 
 All characters in the input are digits or lowercase letters of the English alphabet. Output For each Q command output one line containing only the single character queried. Sample Input ab Sample Output a Source POJ Monthly--2006.07.30, zhucheng | 
题意:给一个字符串,要求查询某一位的字母,或在某一位前插入一个字母.
题解:
Splay的单点插入,和单点查询.
记得内存大小要把操作的2000加上。
速度422ms,还不错。。。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXN 1002010
struct node
{
int left,right,size;
char val;
}tree[MAXN];
int father[MAXN];
char str[MAXN];
void Pushup(int k)
{
tree[k].size=tree[tree[k].left].size+tree[tree[k].right].size+;
}
void rotate(int x,int &root)
{
int y=father[x],z=father[y];
if(y==root)root=x;
else
{
if(tree[z].left==y)tree[z].left=x;
else tree[z].right=x;
}
if(tree[y].left==x)
{
father[x]=z;father[y]=x;tree[y].left=tree[x].right;tree[x].right=y;father[tree[y].left]=y;
}
else
{
father[x]=z;father[y]=x;tree[y].right=tree[x].left;tree[x].left=y;father[tree[y].right]=y;
}
Pushup(y);Pushup(x);
}
void Splay(int x,int &root)
{
while(x!=root)
{
int y=father[x],z=father[y];
if(y!=root)
{
if((tree[y].left==x)^(tree[z].left==y))rotate(x,root);
else rotate(y,root);
}
rotate(x,root);
}
}
void Build(int l,int r,int f)
{
if(l>r)return;
int now=l,fa=f;
if(l==r)
{
tree[now].val=str[l];tree[now].size=;
father[now]=fa;
if(l<f)tree[fa].left=now;
else tree[fa].right=now;
return;
}
int mid=(l+r)/;
now=mid;
Build(l,mid-,mid);Build(mid+,r,mid);
tree[now].val=str[mid];father[now]=fa;
Pushup(now);
if(mid<f)tree[fa].left=now;
else tree[fa].right=now;
}
int Find(int root,int rank)
{
if(rank==tree[tree[root].left].size+)return root;
if(rank<=tree[tree[root].left].size)return Find(tree[root].left,rank);
else return Find(tree[root].right,rank-tree[tree[root].left].size-);
}
int main()
{
int m,i,k,L,R,x,y,z,lstr,rt,n;
char ch,fh[];
scanf("%s",str+);
lstr=strlen(str+);
m=lstr+;
Build(,m,);
rt=(+m)/;
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("\n%s",fh);
if(fh[]=='Q')
{
scanf("%d",&k);
L=k;R=k+;
x=Find(rt,L);y=Find(rt,R);
Splay(x,rt);Splay(y,tree[x].right);
z=tree[y].left;
printf("%c\n",tree[z].val);
}
else
{
scanf("\n%c %d",&ch,&k);
L=k;R=k+;
x=Find(rt,L);y=Find(rt,R);
Splay(x,rt);Splay(y,tree[x].right);
tree[y].left=++m;
z=tree[y].left;tree[z].size=;
father[z]=y;tree[z].val=ch;
Pushup(y);Pushup(x);
}
}
return ;
}
Poj 2887-Big String Splay的更多相关文章
- 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(分块)
		http://poj.org/problem?id=2887 题意:给出一个字符串,还有n个询问,第一种询问是给出一个位置p和字符c,要在位置p的前面插入c(如果p超过字符串长度,自动插在最后),第二 ... 
- Big String(poj 2887)
		题意: 给你一个不超过1e6的字符串,和不超过2000次的操作 操作分为两种: 1.将一个字符插入到某个位置的前面 2.询问当前位置的字符 /* 块状链表模板水题(我的智商也就能做这种题了). 观察题 ... 
- POJ 3580 SuperMemo (splay tree)
		SuperMemo Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6841 Accepted: 2268 Case Ti ... 
- 2887 Big String
		splay瞎搞一下,正解是分块数组或分块链表,但是学不会啊! #include<cstdio> #include<cstdlib> #include<iostream&g ... 
- POJ 2887
		#include <iostream> #include <string> #define MAXN 2000 using namespace std; struct node ... 
随机推荐
- Tran 与 Goto try catch raiserror等浅显应用
			---- Tran 事务 以及 Goto 的浅显应用 IF OBJECT_ID('tempdb..#tmptab','U') IS NOT NULL DROP TABLE #tmptab GO CRE ... 
- SQL内外左右交叉连接
			什么是连接查询? 概念:根据两个表或多个表的列之间的关系,从这些表中查询数据. 目的:实现多个表查询操作. 一般是用作关联两张或两张以上的数据表时用的.看起来有点抽象,我们举个例子,做两张表:学生表( ... 
- c语言学习之基础知识点介绍(七):循环结构
			本节主要介绍循环结构 一.while循环 /* 语法: while(表达式){ //循环体; } 注意:循环变量.循环条件和循环控制语句三者缺一不可. 例如: */ ; //循环变量 ){ //循环条 ... 
- Android - 服务器json数据交互.
			一,服务器端 服务器端使用的是Servlet,封装json对象使用的 'json-lib-2.2.2-jdk15.jar,ezmorph-1.0.4.jar,commons-logging-1.1.j ... 
- 小白偶遇Sublime Text 3
			sublime text3号称神一样的编辑器,主要归功于它丰富的插件所带来的可扩展性.以前曾经抱着玩一玩的心态下载了sublime ,没有插件的sublime 很快被我扔到一边.在用过很多的编辑器后, ... 
- AngularJS 路由:ui-router
			UI-Router是Angular-UI提供的客户端路由框架,它解决了原生的ng-route的很多不足:视图不能嵌套.这意味着$scope会发生不必要的重新载入.这也是我们在Onboard中引入ui- ... 
- tp接支付宝接口签名不相等解决办法 接口版本3.3 tp版本3.1
			(2)在Core.php和Notify.php文件中添加了去掉TP的URL中的'_URL_'参数的函数.这个是必须的,否则会导致验证失败问题.具体改变为: function paraFilter改为 ... 
- ios开发中加载的image无法显示
			昨天遇到一个较奇葩的问题,imageName加载的图片显示不出来,网上查了好多资料还是没找到解决的方法: 之前图片是放在项目中SupportingFiles文件下的,怎么加载都能显示图片,于是将图片拿 ... 
- Java学习----finally块
			public class Test { String x; public static void main(String[] args) { Test test = new Test(); try { ... 
- C#快递单号查询源码
			源码本人测试过,没有啥问题,能查询快递单号,支持的快递还挺多,圆通快递.申通快递.韵达快递的都支持单号查询的,程序是通过向爱快递(www.aikuaidi.cn)接口传输参数来查询快递单号,我直接把代 ... 
