【BZOJ 1269】 [AHOI2006]文本编辑器editor
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
```cpp
/*
【move k】 指令。直接 把pos改成k.表示改变光标位置
【insert n s】,在pos后面插入一个长度为n的字符串。
这个操作。
我们可以先找出第pos个节点x和第pos+1个节点y
(这里其实就是找第pos小的数,在splay上,加一个size大小域,找第k小即可。)
(伸展树在旋转的过程中,不会影响性质,即,它中序遍历的结果始终是字符串s1..n)
我们执行splay x 0
再执行splay y x
然后显然y节点在x的右儿子上。
那么,此时,我们新建一颗节点为插入的字符串的子树root
然后把root放在y的左子树
然后我们新建一颗子树,把它加在y的左儿子上。
我们在插入节点的时候。放在左儿子和右儿子。
其实就是约束了这个点和其父亲节点的先后关系。
左儿子在前.右儿子的话就是在后。
也正因为如此。
我们才能保证中序遍历结果为s[1..n]
【delete(n)】 找出第pos个节点x和第pos+1+n个节点y.然后同样的splay(x,0)、splay(y,x),然后把y的左子树删掉就可以了。
【rotate n】一样。找到pos和pos+1+n两个节点x,y 然后spay(x,0),splay(y,x) 然后把y的左儿子打上翻转标记。(这里后序的我不知道怎么处理
(什么时候要push_down?
【get】就直接输出第pos个节点的字符就好。
【prev】 pos--;
【next] pos++;
*/
**【注意】**
push_down只要加在Rank函数的开头就好。
因为每次从根到root.
一路上都会把标记给Push_down了
那么rotate里面就不需要push_down了。
</font>
<font color = black size = 6> 【代码】</font>
```cpp
#include <bits/stdc++.h>
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define all(x) x.begin(),x.end()
#define pb push_back
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define which(x) (ch[fa[x]][1]==x)
using namespace std;
const double pi = acos(-1);
const int dx[4] = {0,0,1,-1};
const int dy[4] = {1,-1,0,0};
const int N = 2*1024*1024;
int n,siz[N+100],fa[N+10],tag[N+10],tot,pos;
int ch[N+10][2],root;
char s[10],S[N+10],str[N+10];
void push_up(int x){
siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + 1;
}
void push_down(int x){
if (tag[x]>0){
tag[x] = 0;
tag[ch[x][0]]^=1;tag[ch[x][1]] ^= 1;
swap(ch[x][0],ch[x][1]);
}
}
int Rank(int x, int k)
{
push_down(x);
if (siz[ch[x][0]] >= k)
return Rank(ch[x][0], k);
else
if (k == siz[ch[x][0]] + 1)
return x;
else
return Rank(ch[x][1], k - siz[ch[x][0]] - 1);
}
void Rotate(int x)
{
int f = fa[x];
bool k = which(x);
ch[f][k] = ch[x][!k];
ch[x][!k] = f;
ch[fa[f]][which(f)] = x;
fa[ch[f][k]] = f;
fa[x] = fa[f];
fa[f] = x;
siz[x] = siz[f];
push_up(f);
}
void Splay(int x, int g)
{
while (fa[x] != g)
{
int f = fa[x];
if (fa[f] == g)
{
Rotate(x);
break;
}
if (which(x) ^ which(f))
Rotate(x);
else
Rotate(f);
Rotate(x);
}
if (!g) root = x;
}
int build(int l, int r, int rt) //创建一颗新的子树。
{
if (l > r) return 0;
int mid = (l + r) >> 1;
int x = ++tot;
fa[x] = rt; str[x] = S[mid];
ch[x][0] = build(l, mid - 1, x);
ch[x][1] = build(mid + 1, r, x);
push_up(x);
return x;
}
int main(){
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
root = 1;
tot++;str[tot] = '@';siz[tot] = 2;ch[tot][1] = tot+1;
tot++;str[tot] = '#';siz[tot] = 1;fa[tot] = 1;
//创建两个边界节点。
cin >> n;
while (n--){
cin >> s;
//cout<<s<<endl;
if (s[0]=='M'){
int k;
cin >> k;
pos = k;
}else if (s[0]=='P'){
pos--;
}else if (s[0]=='N'){
pos++;
}else if (s[0]=='I'){
int len;
cin >> len;cin.get();
cin.getline(S,N);
int x = Rank(root,pos+1),y = Rank(root,pos+1+1);
Splay(x,0);Splay(y,x);
ch[y][0] = build(0,len-1,y);
push_up(y);push_up(x);
Splay(y,0);
}else if (s[0]=='D'){
int len;
cin >> len;
int x = Rank(root,pos+1),y = Rank(root,pos+1+len+1);
Splay(x,0);Splay(y,x);
ch[y][0] = 0;
push_up(y);push_up(x);
Splay(y,0);
}else if (s[0]=='R'){
int len;
cin >> len;
int x = Rank(root,pos+1),y = Rank(root,pos+1+len+1);
Splay(x,0);Splay(y,x);
int t = ch[y][0];
tag[t] ^= 1;
}else if (s[0]=='G'){
int x = Rank(root,pos+2);
cout<<str[x]<<endl;
}
}
return 0;
}
【BZOJ 1269】 [AHOI2006]文本编辑器editor的更多相关文章
- BZOJ 1269: [AHOI2006]文本编辑器editor( splay )
splay..( BZOJ 1507 题目基本相同..双倍经验 ) ------------------------------------------------------------------ ...
- BZOJ 1269: [AHOI2006]文本编辑器editor (splay tree)
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1213 Solved: 454[Submit ...
- bzoj 1269 [AHOI2006]文本编辑器editor
原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1269 伸展树的运用,如下: #include<cstdio> #include ...
- 【BZOJ】1269: [AHOI2006]文本编辑器editor(Splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=1269 这题RE2次啊,好不爽啊,我一直以为是splay的问题,其实是数组开小了......(我老犯这 ...
- 1269: [AHOI2006]文本编辑器editor
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5269 Solved: 2037[Submit][Status][Discuss] Descript ...
- AHOI2006文本编辑器editor
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1885 Solved: 683[Submit ...
- BZOJ1269 [AHOI2006]文本编辑器editor 【82行splay】
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MB Submit: 4633 Solved: 1782 [Sub ...
- BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor
BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor 题意: 分析: splay模拟即可 注意1507的读入格式 ...
- 【BZOJ1269/1507】[AHOI2006]文本编辑器editor Splay
[BZOJ1269][AHOI2006]文本编辑器editor Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目 ...
- 【bzoj1507】[NOI2003]Editor /【bzoj1269】[AHOI2006]文本编辑器editor Splay
[bzoj1507][NOI2003]Editor 题目描述 输入 输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中 ...
随机推荐
- 11、E-commerce in Your Inbox:Product Recommendations at Scale-----产品推荐(prod2vec和user2vec)
一.摘要 本文提出一种方法,将神经语言模型应用在用户购买时间序列上,将产品嵌入到低维向量空间中.结果,具有相似上下文(即,其周围购买)的产品被映射到嵌入空间中附近的向量. 二.模型: 低维项目向量表示 ...
- 【XSY2968】线性代数
题目来源:noi2018模拟测试赛(二十二) 毒瘤板题+提答场……真tm爽 提答求最大团,各路神仙退火神仙随机化八仙过海 题意: 题解: 支持双端插入的回文自动机板题 代码: #include< ...
- docker 镜像的导入导出
今天使用docker部署asp.net core应用程序时,发现当我们做好基础镜像之后需要把镜像导出到正式环境,因此学习了一下如何从docker中导出镜像: 1.首先通过docker images命令 ...
- W10如何开启LinuxBash及安装Ubuntu
W10如何开启LinuxBash的功能 1)开启开发人员模式 2)启动部分windows功能 完成后重启系统 然后在cmd中输入bash按命令操作即可使用bash命令 3)下载安装ubuntu lxr ...
- JavaScript变量提升(Hoisting)的小案例
变量提升(Hoisting)的小案例 执行以下代码的结果是什么?为什么? 答案 这段代码的执行结果是undefined 和 2. 这个结果的原因是,变量和函数都被提升(hoisted) 到了函数体的顶 ...
- HDU 4314 Contest 2
可以知道,逃出的人中,最后一个应当是A+B最长的,这是很容易发现的.那么,最选逃出去的必定是A+B最短的.这符合最优. 于是,可以把各小矮人按A+B的和由大到小排序.定义DP[i][j]为i个人中逃出 ...
- Codeforces Round #274 (Div. 2) 解题报告
题目地址:http://codeforces.com/contest/479 这次自己又仅仅能做出4道题来. A题:Expression 水题. 枚举六种情况求最大值就可以. 代码例如以下: #inc ...
- JAVA设计模式之【适配器模式】
适配器模式 当不需要实现一个接口所提供的所有方法时,可先设计一个抽象类该接口,并为接口每个方法提供一个默认实现 该抽象类的子类可以选择性地覆盖父类的某些方法来实现需求 角色 适配者接口 通常在接口中声 ...
- 集群节点Elasticsearch升级
集群节点Elasticsearch升级 操作流程 1.首先执行Elasticsearch-1.2.2集群的索引数据备份 2.关闭elasticsearch-1.2.2集群的recovery.compr ...
- Another app is currently holding the yum lock; waiting for it to exit…
yum被锁定无法使用,错误信息截图如下:解决方法:rm -rf /var/run/yum.pid 来强行解除锁定,然后你的yum就可以运行了