[NOI2003]文本编辑器

没啥好说的 就是个板子

#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]的更多相关文章

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

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

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

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

  3. P4008 [NOI2003]文本编辑器

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

  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]文本编辑器 (Splay)

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

随机推荐

  1. SPH液面重构过程中的问题

    使用粒子方法进行流体特效模拟需要进行液面重构,构造出流体的自由表面,液面重构方法也是一个独立的研究方向,针对其的研究已经有了很多成果,包括液面的平滑度.精度和并行效率等. 在这里,主要是记录一下我在液 ...

  2. [MP3]MP3固件持续分享(2019.1.25)

    转载自我的博客:https://blog.ljyngup.com/archives/179.html/ 所有的固件到我的博客就可以下载哦 最后更新于2019.2.1 前言 这篇文章会持续更新不同型号的 ...

  3. 导出Chrome浏览器中保存的密码

    title: 导出Chrome浏览器中保存的密码 date: 2018-02-11 17:54:51 tags: --- 导出Chrome浏览器中保存的密码 先知看到的,挺有意思,记录一下 不同浏览器 ...

  4. 查看php相关信息

    1.最常见的就是 创建一个  php页面  ,例如 test.php,  内容如下 <?php phpinfo();?> 直接访问 这个页面,就可以看到php的 信息了 2.其它方法  直 ...

  5. Windwos日志分析

    Windows日志分析工具 查看系统日志方法: 在“开始”菜单上,依次指向“所有程序”.“管理工具”,然后单击“事件查看器” 按 "Window+R",输入 ”eventvwr.m ...

  6. leetcode--js--Median of Two Sorted Arrays

     问题描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of ...

  7. 表关联使用INNER JOIN实现更新功能

    准备一些数据,创建2张表,表1为学生表: CREATE TABLE [dbo].[Student] ( [SNO] INT NOT NULL PRIMARY KEY, ) NOT NULL, ,) N ...

  8. 使用ffmpeg为影片添加字幕

    ffmpeg -i infile.mp4 -i infile.srt -c copy -c:s mov_text outfile.mp4 The order of -c copy -c:s mov_t ...

  9. Codeforces Round 450 D 隔板法+容斥

    题意: Count the number of distinct sequences a1, a2, ..., an (1 ≤ ai) consisting of positive integers ...

  10. 常用类String的总结

    /* String:字符串,使用一对""引起来表示. 1.String声明为final的,不可被继承 2.String实现了Serializable接口:表示字符串是支持序列化的. ...