题目

发现这样一句话就会导致\(T\)

ch[m][0]=++m;

并不是很知道为什么,可能这是某种未定义行为在不同编译器下会有不同后果?

至于这道题就很简单了,几个有关光标位置的操作就用一个变量模拟就好了

插入的话把这个位置转出来构造一棵完美\(splay\)插入就好了

删除直接转出区间断开的父亲的链接

输出直接转出区间中序遍历

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define maxn 2100005
#define re register
char opt[15],S[maxn],val[maxn];
int n,root,pos,m,len;
int fa[maxn],ch[maxn][2],sz[maxn];
inline void update(int x) {sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;}
inline void rotate(int x) {
int y=fa[x],z=fa[y],k=ch[y][1]==x,w=ch[x][k^1];
ch[z][ch[z][1]==y]=x,ch[x][k^1]=y,ch[y][k]=w;
update(y),update(x),fa[w]=y,fa[y]=x,fa[x]=z;
}
inline void splay(int x,int goal) {
while(fa[x]!=goal) {
int y=fa[x];
if(fa[y]!=goal) rotate((ch[y][1]==x)^(ch[fa[y]][1]==y)?x:y);
rotate(x);
}
if(!goal) root=x;
}
inline int kth(int x) {
int u=root;
while(1) {
if(sz[ch[u][0]]>=x) u=ch[u][0];
else if(x>sz[ch[u][0]]+1) x-=sz[ch[u][0]]+1,u=ch[u][1];
else return u;
}
}
void dfs(int x) {
if(ch[x][0]) dfs(ch[x][0]);
putchar(val[x]);
if(ch[x][1]) dfs(ch[x][1]);
}
int build(int x,int y,int f) {
if(x>y) return 0;
if(x==y) {
val[++m]=S[x];sz[m]=1;fa[m]=f;
return m;
}
int mid=x+y>>1,rt=++m;
val[rt]=S[mid];fa[rt]=f;
ch[rt][0]=build(x,mid-1,rt),ch[rt][1]=build(mid+1,y,rt);update(rt);
return rt;
}
int main()
{
scanf("%d",&n);
pos=1;
root=++m;sz[m]=2;ch[1][0]=++m;
sz[m]=1;fa[m]=1;
while(n--) {
scanf("%s",opt);
if(opt[0]=='M') scanf("%d",&pos),pos++;
if(opt[0]=='P') pos--;
if(opt[0]=='N') pos++;
if(opt[0]=='I') {
scanf("%d",&len);S[0]=' ';
for(re int i=1;i<=len;i++) {
S[i]=getchar();
if(S[i]=='\n'||S[i]=='\r') --i;
}
int aa=kth(pos),bb=kth(pos+1);
splay(aa,0),splay(bb,aa);
int t=ch[root][1],rt=build(1,len,t);
ch[t][0]=rt,update(t),splay(t,0);
}
if(opt[0]=='D') {
scanf("%d",&len);
int aa=kth(pos),bb=kth(pos+len+1);
splay(aa,0),splay(bb,aa);
ch[ch[root][1]][0]=0;update(ch[root][1]);splay(ch[root][1],0);
}
if(opt[0]=='G') {
scanf("%d",&len);
int aa=kth(pos),bb=kth(pos+len+1);
splay(aa,0),splay(bb,aa);
dfs(ch[ch[root][1]][0]);putchar(10);
}
}
return 0;
}

【[NOI2003]文本编辑器】的更多相关文章

  1. [NOI2003] 文本编辑器 (splay)

    复制炸格式了,就不贴题面了 [NOI2003] 文本编辑器 Solution 对于光标的移动,我们只要记录一下现在在哪里就可以了 Insert操作:手动维护中序遍历结果,即每次取中点像线段树一样一样递 ...

  2. 洛谷 P4008 [NOI2003]文本编辑器 解题报告

    P4008 [NOI2003]文本编辑器 题目描述 很久很久以前,\(DOS3.x\)的程序员们开始对 \(EDLIN\) 感到厌倦.于是,人们开始纷纷改用自己写的文本编辑器⋯⋯ 多年之后,出于偶然的 ...

  3. [NOI2003]文本编辑器 [Fhq Treap]

    [NOI2003]文本编辑器 没啥好说的 就是个板子 #include <bits/stdc++.h> // #define int long long #define rep(a , b ...

  4. cogs 330. [NOI2003] 文本编辑器

    ★★★   输入文件:editor2003.in   输出文件:editor2003.out   简单对比 时间限制:2 s   内存限制:128 MB [问题描述] 很久很久以前,DOS3.x的程序 ...

  5. luogu P4008 [NOI2003]文本编辑器 splay 块状链表

    LINK:文本编辑器 这个东西感觉块状链表写细节挺多 (块状链表本来就难写 解释一下块状链表的做法:其实是一个个数组块 然后利用链表给链接起来 每个块的大小为sqrt(n). 这样插入删除的时候直接暴 ...

  6. NOI2003 文本编辑器editor

    1507: [NOI2003]Editor Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 1908  Solved: 738[Submit][Statu ...

  7. NOI2003 文本编辑器

    练手QAQ #include<iostream> #include<algorithm> #include<cstdio> #include<cstdlib& ...

  8. 题解 P4008 【[NOI2003]文本编辑器】

    块状链表及其应用 思路楼上已经说的很清楚了 看代码注释 代码很丑 #include<cstdio> #include<cctype> #include<cstring&g ...

  9. P4008 [NOI2003]文本编辑器

    思路 FHQ Treap的板子 用FHQ Treap维护中序遍历序列即可 然后数组开够! 代码 #include <cstdio> #include <cstring> #in ...

  10. 【洛谷 P4008】 [NOI2003]文本编辑器 (Splay)

    题目链接 \(Splay\)先练到这吧(好像还有道毒瘤的维护数列诶,算了吧) 记录下光标的编号,维护就是\(Splay\)基操了. 另外数据有坑,数据是\(Windows\)下生成了,回车是'\n\r ...

随机推荐

  1. java多线程-synchronized

    一.线程安全问题 多线程操作各自线程创建的资源的时候,不存在线程安全问题.但多线程操作同一个资源的时候就会出现线程安全问题.下例为两个线程操作同一个name资源时发生的问题. class TestSy ...

  2. 5. AQS(AbstractQueuedSynchronizer)抽象的队列式的同步器

    5.1 AbstractQueuedSynchronizer里面的设计模式--模板模式 模板模式:父类定义好了算法的框架,第一步做什么第二步做什么,同时把某些步骤的实现延迟到子类去实现. 5.1.1 ...

  3. 2019.03.27 读书笔记 关于GC垃圾回收

    在介绍GC前,有必要对.net中CLR管理内存区域做简要介绍: 1. 堆栈:用于分配值类型实例.堆栈主要操作系统管理,而不受垃圾收集器的控制,当值类型实例所在方法结束时,其存储单位自动释放.栈的执行效 ...

  4. pat09-散列3. Hashing - Hard Version (30)

    09-散列3. Hashing - Hard Version (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HE, Qin ...

  5. 无监督学习(Unsupervised Learning)

    无监督学习(Unsupervised Learning) 聚类无监督学习 特点 只给出了样本, 但是没有提供标签 通过无监督学习算法给出的样本分成几个族(cluster), 分出来的类别不是我们自己规 ...

  6. BNU 4356 ——A Simple But Difficult Problem——————【快速幂、模运算】

    A Simple But Difficult Problem Time Limit: 5000ms Memory Limit: 65536KB 64-bit integer IO format: %l ...

  7. vs2012 使用方法汇总

    1)安装Vsiual Assist插件 工具栏-->tools-->Extentsions and Upates-->点击左边的Online然后右边会出现可以安装的插件,找到Visu ...

  8. 【Linux】linux文件夹打包命令

    .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ---------------------- ...

  9. django细节

    1.处理请求 1.1  /add/?a=4&b=5 这样GET方法进行[获取参数] from django.shortcuts import render from django.http i ...

  10. Spring @Resource, @Autowired and @Inject 注入

    Overview I’ve been asked several times to explain the difference between injecting Spring beans with ...