题目

发现这样一句话就会导致\(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. python 网页爬取数据生成文字云图

    1. 需要的三个包: from wordcloud import WordCloud #词云库 import matplotlib.pyplot as plt #数学绘图库 import jieba; ...

  2. python 管理多版本之pyenv

    一, [root@management ~]# pyenv install -listAvailable versions:  3.3.0  3.3.1  3.3.2  3.3.3  3.3.4  3 ...

  3. TimesTen启动停止命令

    ttDaemonAdmin –start 启动 ttDaemonAdmin –stop 停止 或打开服务cmd-serviers.msc,找到相关服务启动或停止.

  4. C#(Winform)的SaveFileDialog(文件保存对话框)控件使用

       #region 保存对话框   private void ShowSaveFileDialog()   {         //string localFilePath, fileNameExt ...

  5. 调用sqlserver中的存储过程

    1.存储过程名 string strSQL = "usp_GetUnReturnPassports"; 2.创建数据库对象 database Database db = Datab ...

  6. Codeforces 975D. Ghosts

    Description 给出一条直线 \(a*x+b\) 上的 \(n\) 个点,每一个点有一个速度 \((v_x,v_y)\),求 \(T=[-oo,oo]\) 相交的次数乘以 \(2\) 题面 S ...

  7. Java工程路径及相对路径(转载)

    3. 新建文件,默认位于工程目录new File("xxx.txt").getAbsolutePath();例如输出,D:\workspaces\workspace1\myProj ...

  8. springboot和mybatis集成,自动生成model、mapper,增加mybatis分页功能

    整体思路和http://www.cnblogs.com/mahuan2/p/5859921.html相同. 主要讲maven的pom.xml和一些配置变化,详细说明. 软件简介 Spring是一个流行 ...

  9. JavaScript对象 原型

    javascript对象就是一组数据和功能的集合,除原始类型(string.number.boolean.null.undefined)之外,其余都是对象. 可以通过对象直接量(字面量).new.和O ...

  10. css3轮播渐显效果 2016/11/29

    css3因为其兼容性的问题,被我忽略很久,这次正好做到一个轮播渐显的效果,想了想,正好复习下css3的相关内容,废话不多说,直接上代码. <ul class="cb-slideshow ...