P4008 [NOI2003]文本编辑器
思路
FHQ Treap的板子
用FHQ Treap维护中序遍历序列即可
然后数组开够!
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Node{
int lson,rson,sz,num,ran;
}FHQ[300000*30];
int nowpos,n,m,Nodecnt,a[300000*30],root;
int new_Node(int num){
++Nodecnt;
FHQ[Nodecnt].lson=FHQ[Nodecnt].rson=0;
FHQ[Nodecnt].ran=rand();
FHQ[Nodecnt].num=num;
FHQ[Nodecnt].sz=1;
return Nodecnt;
}
void pushup(int o){
FHQ[o].sz=FHQ[FHQ[o].lson].sz+FHQ[FHQ[o].rson].sz+1;
}
void split(int val,int now,int &x,int &y){//sz
if(!now){
x=y=0;
return;
}
if(val<=FHQ[FHQ[now].lson].sz){
y=now;
split(val,FHQ[now].lson,x,FHQ[now].lson);
}
else{
x=now;
split(val-FHQ[FHQ[now].lson].sz-1,FHQ[now].rson,FHQ[now].rson,y);
}
pushup(now);
}
int merge(int x,int y){//x.w<y.w
if(x*y==0)
return x+y;
if(FHQ[x].ran<FHQ[y].ran){
FHQ[x].rson=merge(FHQ[x].rson,y);
pushup(x);
return x;
}
else{
FHQ[y].lson=merge(x,FHQ[y].lson);
pushup(y);
return y;
}
}
int build(int l,int r){
if(l>r)
return 0;
int mid=(l+r)>>1;
int t=new_Node(a[mid]);
FHQ[t].lson=build(l,mid-1);
FHQ[t].rson=build(mid+1,r);
pushup(t);
return t;
}
void query(int o){
if(!o)
return;
query(FHQ[o].lson);
printf("%c",FHQ[o].num);
query(FHQ[o].rson);
}
char opt[20];
int main(){
root=new_Node(-1);
nowpos=1;
scanf("%d",&m);
for(int i=1;i<=m;i++){
scanf("%s",opt);
if(opt[0]=='M'){
int x;
scanf("%d",&x);
nowpos=x+1;
}
else if(opt[0]=='I'){
int lroot,rroot,newroot;
split(nowpos,root,lroot,rroot);
scanf("%d",&n);
int cnt=1;
char c=getchar();
while(cnt<=n){
c=getchar();
if(c>=32&&c<=126){
a[cnt]=c;
cnt++;
}
}
newroot=build(1,n);
root=merge(merge(lroot,newroot),rroot);
}
else if(opt[0]=='D'){
int lroot,tmproot,midroot,rroot;
scanf("%d",&n);
split(nowpos,root,lroot,tmproot);
split(n,tmproot,midroot,rroot);
root=merge(lroot,rroot);
}
else if(opt[0]=='G'){
int lroot,tmproot,midroot,rroot;
scanf("%d",&n);
split(nowpos,root,lroot,tmproot);
split(n,tmproot,midroot,rroot);
query(midroot);
printf("\n");
root=merge(lroot,merge(midroot,rroot));
}
else if(opt[0]=='P'){
nowpos--;
}
else if(opt[0]=='N'){
nowpos++;
}
}
return 0;
}
P4008 [NOI2003]文本编辑器的更多相关文章
- 洛谷 P4008 [NOI2003]文本编辑器 解题报告
P4008 [NOI2003]文本编辑器 题目描述 很久很久以前,\(DOS3.x\)的程序员们开始对 \(EDLIN\) 感到厌倦.于是,人们开始纷纷改用自己写的文本编辑器⋯⋯ 多年之后,出于偶然的 ...
- luogu P4008 [NOI2003]文本编辑器 splay 块状链表
LINK:文本编辑器 这个东西感觉块状链表写细节挺多 (块状链表本来就难写 解释一下块状链表的做法:其实是一个个数组块 然后利用链表给链接起来 每个块的大小为sqrt(n). 这样插入删除的时候直接暴 ...
- 洛谷 P4008 [NOI2003]文本编辑器
先推广一下 求赞 我们考虑这样的一个问题 给你一个序列,要求你支持插入,删除,查询单点值 如果用数组,查询O(1),插入删除最坏O(n) 如果用链表,插入删除O(1),查询最坏O(n) 如果用平衡树- ...
- [NOI2003] 文本编辑器 (splay)
复制炸格式了,就不贴题面了 [NOI2003] 文本编辑器 Solution 对于光标的移动,我们只要记录一下现在在哪里就可以了 Insert操作:手动维护中序遍历结果,即每次取中点像线段树一样一样递 ...
- [NOI2003]文本编辑器 [Fhq Treap]
[NOI2003]文本编辑器 没啥好说的 就是个板子 #include <bits/stdc++.h> // #define int long long #define rep(a , b ...
- cogs 330. [NOI2003] 文本编辑器
★★★ 输入文件:editor2003.in 输出文件:editor2003.out 简单对比 时间限制:2 s 内存限制:128 MB [问题描述] 很久很久以前,DOS3.x的程序 ...
- 题解 P4008 【[NOI2003]文本编辑器】
块状链表及其应用 思路楼上已经说的很清楚了 看代码注释 代码很丑 #include<cstdio> #include<cctype> #include<cstring&g ...
- 【洛谷 P4008】 [NOI2003]文本编辑器 (Splay)
题目链接 \(Splay\)先练到这吧(好像还有道毒瘤的维护数列诶,算了吧) 记录下光标的编号,维护就是\(Splay\)基操了. 另外数据有坑,数据是\(Windows\)下生成了,回车是'\n\r ...
- NOI2003 文本编辑器editor
1507: [NOI2003]Editor Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 1908 Solved: 738[Submit][Statu ...
随机推荐
- css实现文字太长,显示省略号
/*显示为省略号*/ overflow:hidden;/*隐藏*/ white-space:nowrap;/*文本不进行换行*/text-overflow:ellipsis;/*省略号*/ /*强制 ...
- Widget Factory (高斯消元解线性方程组)
The widget factory produces several different kinds of widgets. Each widget is carefully built by a ...
- Windows10上安装Keras 和 TensorFlow-GPU
安装环境: Windows 10 64bit GPU: GeForce gt 720 Python: 3.5.3 CUDA: 8 首先下载Anaconda3的Win10 64bit版,安装Python ...
- Python读取excel数据类型处理
一.python xlrd读取datetime类型数据:https://blog.csdn.net/y1535766478/article/details/78128574 (1)使用xlrd读取出来 ...
- [NOIP2005普及组]采药(01背包)
题目描述 描述 辰辰是个很有潜能.天资聪颖的孩子,他的梦想是称为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个难题.医师把他带到个到处都是草药的山洞里对他说 ...
- android dumpsys
dumpsys dumpsys is a tool that runs on Android devices and provides information about system service ...
- mac电脑使用,开发环境配置指南
mac电脑使用,开发环境配置指南 前端工具链,mac下都很好用 用brew来装软件 用brew cask来装应用 Introduction · macOS Setup Guidehttp://sour ...
- NodeJS遍历文件生产文件列表
本文实例讲述了NodeJS遍历文件生产文件列表功能.分享给大家供大家参考,具体如下: 功能需求:在工作中我们可能经常需要知道项目中静态文件列表发布,一个一个去检索写,那就太苦逼了. 要想知道里面的文件 ...
- Web开发相关笔记 #05# MySQL中文无法匹配
2018-06-02 在 Class.forName 的时候记得先尝试 import 一下. 2018-06-04 1.JDBC SELECT 查询,中文条件查不出东西,可能是字符编码问题: Stri ...
- P3804 【模板】后缀自动机
P3804 [模板]后缀自动机 后缀自动机模板 详情可见luogu题解板块 #include<iostream> #include<cstdio> #include<cs ...