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 ...
随机推荐
- android - android Couldn't load runtimecore_java from loader
在写Arcgis Android 或百度Android的时候,有时会报诸如,java.lang.UnsatisfiedLinkError:android Couldn't load runtimeco ...
- [HttpException (0x80004005): Failed to Execute URL.]之画蛇添足之痛
最近很悲惨,发布的一个mvc站点,所有的静态内容,如js.css.图片都不能正常加载,服务器给出的响应是一个如下的异常黄页: Server Error in '/ua' Application.Fai ...
- ENC28J60 + M430G2553,用uip搭建http服务器,解决“在XP系统下可以访问,在Win7下不能访问”的问题
近日,用ENC28J60,在M430G2553上搭建一个简单的HTTP服务器,结果发现在XP系统下可以访问,在Win7下不能访问,非常奇葩的问题. 通过抓包,如下图,计算机(IP地址为192.168. ...
- php中如何实现网上商城用户历史浏览记录的代码
/如是COOKIE 里面不为空,则往里面增加一个商品ID if (!empty($_COOKIE['SHOP']['history'])){ //取得COOKIE里面的值,并用逗号把它切割成一个数组 ...
- Strategy 模式
可以看到 Strategy 模式和 Template 模式解决了类似的问题,也正如在 Template 模式中分析的,Strategy模式和 Template 模式实际是实现一个抽象接口的两种方式:继 ...
- 判断IE浏览器用IE条件表达式
<!--[if IE]> <script type="text/javascript"> alert("ie") </script ...
- demo_02 less
html 中的代码<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> &l ...
- phpMyAdmin下载与安装
Part1 phpMyAdmin下载 浏览器输入网址 http://www.phpmyadmin.net 下载即可 我的下载是这样的 Part2 phpMyAdmin安装 解压下载的压缩包到apach ...
- 帝国cms7.0跳过[会员注册类型]这步的方法
改 e/config/config.php 文件,把$ecms_config['member']['changeregisterurl']="ChangeRegister.php" ...
- PHPCMS标签大全
{$head[title]} 页面标题,用法: {$phpcms[sitename]} 网站名称 用法: {$head[keywords]} 要害字 用法: {$head[description]} ...