[NOI2003]文本编辑器 [Fhq Treap]
没啥好说的 就是个板子
#include <bits/stdc++.h>
// #define int long long
#define rep(a , b , c) for(int a = b ; a <= c ; ++ a)
#define Rep(a , b , c) for(int a = b ; a >= c ; -- a)
#define go(u) for(int i = G.head[u] , v = G.to[i] , w = G.dis[i] ; i ; v = G.to[i = G.nxt[i]] , w = G.dis[i])
using namespace std ;
using ll = long long ;
using pii = pair < int , int > ;
using vi = vector < int > ;
int read() {
int x = 0 ; bool f = 1 ; char c = getchar() ;
while(c < 48 || c > 57) { if(c == '-') f = 0 ; c = getchar() ; }
while(c > 47 && c < 58) { x = (x << 1) + (x << 3) + (c & 15) ; c = getchar() ; }
return f ? x : -x ;
}
template <class T> void print(T x , char c = '\n') {
static char st[100] ; int stp = 0 ;
if(! x) { putchar('0') ; }
if(x < 0) { x = -x ; putchar('-') ; }
while(x) { st[++ stp] = x % 10 ^ 48 ; x /= 10 ; }
while(stp) { putchar(st[stp --]) ; } putchar(c) ;
}
template <class T> void cmax(T & x , T y) { x < y ? x = y : 0 ; }
template <class T> void cmin(T & x , T y) { x > y ? x = y : 0 ; }
const int _N = 1e6 + 10 ;
struct Group {
int head[_N] , nxt[_N << 1] , to[_N] , dis[_N] , cnt = 1 ;
Group () { memset(head , 0 , sizeof(head)) ; }
void add(int u , int v , int w = 1) { nxt[++ cnt] = head[u] ; to[cnt] = v ; dis[cnt] = w ; head[u] = cnt ; }
} ;
const int N = 1e7 + 10 ;
typedef int arr[N] ;
int GB ;
int rt = 0 , cnt = 0 ;
char val[N] ;
int sz[N] , rnd[N] ;
int ch[N][2] ;
#define ls(x) ch[x][0]
#define rs(x) ch[x][1]
int NewNode(char c) {
val[++ cnt] = c ;
sz[cnt] = 1 ;
rnd[cnt] = rand() ;
return cnt ;
}
void pushup(int x) {
sz[x] = sz[ls(x)] + sz[rs(x)] + 1 ;
}
int merge(int x , int y) {
if(! x || ! y) return x | y ;
if(rnd[x] < rnd[y]) {
rs(x) = merge(rs(x) , y) ;
pushup(x) ;
return x ;
}
else {
ls(y) = merge(x , ls(y)) ;
pushup(y) ;
return y ;
}
}
void split(int cur , int k , int & x , int & y) {
if(! cur) {
x = y = 0 ;
return ;
}
if(sz[ls(cur)] < k) {
x = cur ;
split(rs(x) , k - sz[ls(x)] - 1 , rs(x) , y) ;
pushup(x) ;
return ;
}
else {
y = cur ;
split(ls(y) , k , x , ls(y)) ;
pushup(y) ;
return ;
}
}
void Move(int x) {
GB = x ;
}
void Insert(int len) {
int x , y ;
split(rt , GB , x , y) ;
int _cnt = 0 ; char c = getchar() ;
int Newroot = 0 ;
while(_cnt < len) {
if(c >= 32 && c <= 126 && c != '\n') ++ _cnt , Newroot = merge(Newroot , NewNode(c)) ;
c = getchar() ;
}
rt = merge(x , merge(Newroot , y)) ;
}
void Delete(int len) {
int x , y , z ;
split(rt , GB , x , y) ;
split(y , len , y , z) ;
rt = merge(x , z) ;
}
void dfs(int o) {
if(ls(o)) dfs(ls(o)) ;
putchar(val[o]) ;
if(rs(o)) dfs(rs(o)) ;
}
void Get(int len) {
int x , y , z ;
split(rt , GB , x , y) ;
split(y , len , y , z) ;
dfs(y) ; putchar('\n') ;
rt = merge(merge(x , y) , z) ;
}
void Prev() {
-- GB ;
}
void Next() {
++ GB ;
}
string s ;
signed main() {
int q = read() ;
while(q --) {
cin >> s ;
if(s == "Move") Move(read()) ;
if(s == "Insert") Insert(read()) ;
if(s == "Delete") Delete(read()) ;
if(s == "Get") Get(read()) ;
if(s == "Prev") Prev() ;
if(s == "Next") Next() ;
}
return 0 ;
}
[NOI2003]文本编辑器 [Fhq Treap]的更多相关文章
- [NOI2003] 文本编辑器 (splay)
复制炸格式了,就不贴题面了 [NOI2003] 文本编辑器 Solution 对于光标的移动,我们只要记录一下现在在哪里就可以了 Insert操作:手动维护中序遍历结果,即每次取中点像线段树一样一样递 ...
- 洛谷 P4008 [NOI2003]文本编辑器 解题报告
P4008 [NOI2003]文本编辑器 题目描述 很久很久以前,\(DOS3.x\)的程序员们开始对 \(EDLIN\) 感到厌倦.于是,人们开始纷纷改用自己写的文本编辑器⋯⋯ 多年之后,出于偶然的 ...
- P4008 [NOI2003]文本编辑器
思路 FHQ Treap的板子 用FHQ Treap维护中序遍历序列即可 然后数组开够! 代码 #include <cstdio> #include <cstring> #in ...
- cogs 330. [NOI2003] 文本编辑器
★★★ 输入文件:editor2003.in 输出文件:editor2003.out 简单对比 时间限制:2 s 内存限制:128 MB [问题描述] 很久很久以前,DOS3.x的程序 ...
- luogu P4008 [NOI2003]文本编辑器 splay 块状链表
LINK:文本编辑器 这个东西感觉块状链表写细节挺多 (块状链表本来就难写 解释一下块状链表的做法:其实是一个个数组块 然后利用链表给链接起来 每个块的大小为sqrt(n). 这样插入删除的时候直接暴 ...
- NOI2003 文本编辑器editor
1507: [NOI2003]Editor Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 1908 Solved: 738[Submit][Statu ...
- NOI2003 文本编辑器
练手QAQ #include<iostream> #include<algorithm> #include<cstdio> #include<cstdlib& ...
- 题解 P4008 【[NOI2003]文本编辑器】
块状链表及其应用 思路楼上已经说的很清楚了 看代码注释 代码很丑 #include<cstdio> #include<cctype> #include<cstring&g ...
- 【洛谷 P4008】 [NOI2003]文本编辑器 (Splay)
题目链接 \(Splay\)先练到这吧(好像还有道毒瘤的维护数列诶,算了吧) 记录下光标的编号,维护就是\(Splay\)基操了. 另外数据有坑,数据是\(Windows\)下生成了,回车是'\n\r ...
随机推荐
- Go语言实现:【剑指offer】二叉搜索树的后序遍历序列
该题目来源于牛客网<剑指offer>专题. 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. Go ...
- 命令拼接符 || && 区别
&& 前面的命令执行成功,执行拼接符后面的命令. || 前面的命令执行失败,执行拼接符后面的命令. 举例: (1)whoami && net user 真 ...
- 20200223--python学习第15天
今日内容 模块知识 内置模块 time/datatime/json/其他 内容回顾&作业题 重点知识点 构造字典和函数对应的关系,避免重复的if else a = 1 b = 2 ----- ...
- -bash: warning: setlocale: LC_CTYPE: cannot change locale (zh_CN.UTF-8mb4): No such file or directory
前几天登录服务器发现出现了这些个警告,一直没时间去处理他,今天难得有空,处理一下并记录下来,希望可以帮助到有需要的朋友. 警告信息如下: Last login: Tue May :: from 192 ...
- 教你如何用Vue自己实现一个message插件
今天我们来自己动手用实现一个message插件: Vue.js 的插件应该暴露一个 install 方法.这个方法的第一个参数是 Vue 构造器,第二个参数是一个可选的选项对象: //message. ...
- Git push时不需要总输入密码
遇到问题: 最近因为换了自己的邮箱密码后,每次push的时候都需要填写密码,账号.很烦 解决方法: [戎马半生的答案] (http://www.cnblogs.com/zhaoyu1995/p/650 ...
- Java 中常见排序算法
经典的排序算法总结 冒泡排序算法 算法描述: 比较相邻的元素:如果第一个比第二个大,就交换它们两个: 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对,这样在最后的元素应该会是最大的数: 针 ...
- jQuery瀑布流插件masonry
项目要做荣誉证书的排版,宽度是统一的,但是高度不一致 采用瀑布流的效果来实现 默认先实现前15张,点击按钮再加载全部剩下的数据 效果图 首先是html部分,写好样式 <!-- 荣誉资质 --&g ...
- [.NET][C#] C#中的时间戳
Unix时间戳(Unix timestamp),或称Unix时间(Unix time).POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年1月1日0时0分0秒( ...
- 小白的linux笔记3:对外联通——开通ssh和ftp和smb共享
1.SSH的开通.https://www.cnblogs.com/DiDiao-Liang/articles/8283686.html 安装:yum install sshd或yum install ...