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 ...
随机推荐
- Oracle 的merge into 用法
1.merge into的用途 Merge是一个非常有用的功能,与DB2中的merge into功能几乎一样,与Mysql里的insert into on duplicate key也很类似.MERG ...
- Objective-C中的@dynamic(转)
转自 http://blog.csdn.net/haishu_zheng/article/details/12873151 Objective-C中的@dynamic 一.@dynamic与@synt ...
- ios专题 - 单例模式的实现
[原创]http://www.cnblogs.com/luoguoqiang1985 单例模式是什么? 一个类只有一个实例. ----------------------- 这样做有什么好处? 在我的 ...
- Binary Tree Level Order Traversal 解题思路 ×
要求: 树的层级遍历 思路: 1.两个队列,q1 q2 ,root放到q1 2.q1首元素出列,判断是否有左右孩子,有的话,放入q2.(循环此步骤值得q1为空) 3.q1 = q2,重复2,直到q1为 ...
- windows phone 之手势识别(Manipulation)
在Windows Phone 7的多触摸屏上可以检测到至少四根同时存在的手指,并且一起操作使触摸屏充分发挥效果. 在silverlight开发中通过事件来实现触屏事件的检测,包括低级别的和高级别的接口 ...
- clientX/Y,pageX/Y,offsetX/Y,layerX/Y,screenX/Y ,offsetTop,offsetLeft 详解
clientX/Y: clientX/Y获取到的是触发点相对浏览器可视区域左上角距离,不随页面滚动而改变 兼容性:所有浏览器均支持 pageX/Y: pageX/Y获取到的是触发点相对文档区域左上角距 ...
- jquery自动切换tabs选项卡
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncodi ...
- Android Binder机制简单了解
Binder -- 一种进程间通信(IPC)机制, 基于OpenBinder来实现 毫无疑问, 老罗的文章是不得不看的 Android进程间通信(IPC)机制Binder简要介绍和学习计划 浅谈Ser ...
- python【第十四篇】HTML与CSS初遇
概述 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样就可以让浏览器 ...
- LINUX防火墙firewall、iptables
(1) 重启后永久性生效: 开启: systemctl enable iptables.service'.ln -s '/usr/lib/systemd/system/iptables.service ...