[NOI2003],[AHOI2006]文本编辑器
嘟嘟嘟
[NOI2003]的其实就是一个板子……所以我就不说啥了。
唯一需要注意的是读入字符(哎……):题中说“中间可能有空格,请忽略”的意思是要在程序里特判掉,不是不管他……
输出的时候暴力中序遍历就行。
然后[AHOI2006]的只是多了一个区间翻转,然后输出单个字符,其实还是板子。
只不过读入特别坑,推荐看luogu的讨论里的读入。
以及可能有些乱七八糟的操作,得手动忽略……
[NOI2003]
```c++
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 2e6 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans = 10) write(x / 10);
putchar(x % 10 + '0');
}
char s[20], a[maxn];
int Cur = 0;
struct Tree
{
int ch[2], fa;
int siz; char c;
}t[maxn];
define ls t[now].ch[0]
define rs t[now].ch[1]
int root, tcnt = 0;
int st[maxn], top = 0;
In void _PrintTr(int now)
{
if(!now) return;
printf("no:%d val:%c ls:%c rs:%c\n", now, t[now].c, t[ls].c, t[rs].c);
_PrintTr(ls); _PrintTr(rs);
}
In void init() //完全手动初始化……
{
t[root = ++tcnt].c = '\('; t[tcnt].siz = 2;
t[++tcnt].c = '\)'; t[tcnt].siz = 1;
t[root].ch[1] = tcnt; t[tcnt].fa = root;
}
In void pushup(int now)
{
t[now].siz = t[ls].siz + t[rs].siz + 1;
}
In void rotate(int x)
{
int y = t[x].fa, z = t[y].fa, k = (t[y].ch[1] == x);
t[z].ch[t[z].ch[1] == y] = x; t[x].fa = z;
t[y].ch[k] = t[x].ch[k ^ 1]; t[t[y].ch[k]].fa = y;
t[x].ch[k ^ 1] = y; t[y].fa = x;
pushup(y), pushup(x);
}
In void splay(int x, int s)
{
while(t[x].fa != s)
{
int y = t[x].fa, z = t[y].fa;
if(z != s) rotate(((t[y].ch[0] == x) ^ (t[z].ch[0] == y)) ? x : y);
rotate(x);
}
if(!s) root = x;
}
In int Find(int k)
{
int now = root;
while(1)
{
if(t[ls].siz >= k) now = ls;
else if(t[ls].siz + 1 == k) return now;
else k -= (t[ls].siz + 1), now = rs;
}
}
In int split(int L, int R)
{
int a = Find(L), b = Find(R + 2);
splay(a, 0); splay(b, a);
return t[root].ch[1];
}
In int build(int L, int R, int fa)
{
if(L > R) return 0;
int mid = (L + R) >> 1, now = top ? st[top--] : ++tcnt;
t[now].fa = fa;
t[now].siz = 1; t[now].c = a[mid];
ls = build(L, mid - 1, now);
rs = build(mid + 1, R, now);
pushup(now);
return now;
}
In void update_in()
{
int n = read();
char x = getchar(); while(x < 32 || x > 126) x = getchar();
for(int i = 1; i <= n; ++i, x = getchar())
{
if(x < 32 || x > 126) {--i; continue;}
a[i] = x;
}
int tp = build(1, n, 0);
int a = Find(Cur + 1), b = Find(Cur + 2);
splay(a, 0); splay(b, a);
int now = t[root].ch[1];
t[now].ch[0] = tp; t[tp].fa = now;
pushup(now); pushup(root);
}
In void rec(int now)
{
if(!now) return;
rec(ls); rec(rs);
st[++top] = now;
}
In void update_del(int L, int R)
{
int now = split(L, R);
rec(t[now].ch[0]);
t[t[now].ch[0]].fa = 0; t[now].ch[0] = 0;
pushup(now); pushup(root);
}
In void print(int now)
{
if(!now) return;
print(ls);
putchar(t[now].c);
print(rs);
}
In void query(int L, int R)
{
int now = split(L, R);
print(t[now].ch[0]);
}
int main()
{
int T = read();
init();
while(T--)
{
scanf("%s", s);
if(s[0] == 'M') Cur = read();
else if(s[0] == 'I') update_in();
else if(s[0] == 'D')
{
int n = read();
update_del(Cur + 1, Cur + n);
}
else if(s[0] == 'G')
{
int n = read();
query(Cur + 1, Cur + n); enter;
}
else if(s[0] == 'P') Cur--;
else Cur++;
}
return 0;
}
</br>
[AHOI2006]
```c++
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 2e6 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
char s[20], a[maxn];
int Cur = 0;
struct Tree
{
int ch[2], fa;
int siz, rev; char c;
}t[maxn];
#define ls t[now].ch[0]
#define rs t[now].ch[1]
int root, tcnt = 0;
int st[maxn], top = 0;
In void _PrintTr(int now)
{
if(!now) return;
printf("no:%d val:%c ls:%c rs:%c\n", now, t[now].c, t[ls].c, t[rs].c);
_PrintTr(ls); _PrintTr(rs);
}
In void init()
{
t[root = ++tcnt].c = '$'; t[tcnt].siz = 2;
t[++tcnt].c = '$'; t[tcnt].siz = 1;
t[root].ch[1] = tcnt; t[tcnt].fa = root;
}
In void c_rev(int now)
{
swap(ls, rs); t[now].rev ^= 1;
}
In void pushdown(int now)
{
if(t[now].rev)
{
if(ls) c_rev(ls);
if(rs) c_rev(rs);
t[now].rev = 0;
}
}
In void pushup(int now)
{
t[now].siz = t[ls].siz + t[rs].siz + 1;
}
In void rotate(int x)
{
int y = t[x].fa, z = t[y].fa, k = (t[y].ch[1] == x);
t[z].ch[t[z].ch[1] == y] = x; t[x].fa = z;
t[y].ch[k] = t[x].ch[k ^ 1]; t[t[y].ch[k]].fa = y;
t[x].ch[k ^ 1] = y; t[y].fa = x;
pushup(y), pushup(x);
}
In void splay(int x, int s)
{
while(t[x].fa != s)
{
int y = t[x].fa, z = t[y].fa;
if(z != s) rotate(((t[y].ch[0] == x) ^ (t[z].ch[0] == y)) ? x : y);
rotate(x);
}
if(!s) root = x;
}
In int Find(int k)
{
int now = root;
if(k > t[root].siz) {printf("ERR: %d %d\n", k, t[root].siz); exit(0);}
while(1)
{
pushdown(now);
if(t[ls].siz >= k) now = ls;
else if(t[ls].siz + 1 == k) return now;
else k -= (t[ls].siz + 1), now = rs;
}
}
In int split(int L, int R)
{
int a = Find(L), b = Find(R + 2);
splay(a, 0); splay(b, a);
pushdown(root); pushdown(t[root].ch[1]);
return t[root].ch[1];
}
In int build(int L, int R, int fa)
{
if(L > R) return 0;
int mid = (L + R) >> 1, now = top ? st[top--] : ++tcnt;
t[now].fa = fa; t[now].rev = 0;
t[now].siz = 1; t[now].c = a[mid];
ls = build(L, mid - 1, now);
rs = build(mid + 1, R, now);
pushup(now);
return now;
}
In void update_in()
{
int n = read();
char x;
for(int i = 1; i <= n; ++i)
{
x = getchar();
while(x < 32 || x > 126)
{
if(x == 10 || x == 13)
{
for(i = i; i <= n + 1; ++i) a[i] = 0;
break;
}
x = getchar();
}
a[i] = x;
}
int tp = build(1, n, 0);
int a = Find(Cur + 1), b = Find(Cur + 2);
splay(a, 0); splay(b, a);
pushdown(root); pushdown(t[root].ch[1]);
int now = t[root].ch[1];
t[now].ch[0] = tp; t[tp].fa = now;
pushup(now); pushup(root);
}
In void rec(int now)
{
if(!now) return;
rec(ls); rec(rs);
st[++top] = now;
}
In void update_del(int L, int R)
{
int now = split(L, R);
rec(t[now].ch[0]);
t[t[now].ch[0]].fa = 0; t[now].ch[0] = 0;
pushup(now); pushup(root);
}
In void update_rev(int L, int R)
{
int now = split(L, R);
c_rev(t[now].ch[0]);
pushup(now); pushup(root);
}
In char query(int x)
{
int now = Find(x + 1); splay(now, 0);
return t[root].c;
}
int main()
{
int T = read();
init();
while(T--)
{
scanf("%s", s);
if(s[0] == 'M') Cur = read();
else if(s[0] == 'I') update_in();
else if(s[0] == 'D')
{
int n = read();
update_del(Cur + 1, Cur + n);
}
else if(s[0] == 'R')
{
int n = read();
update_rev(Cur + 1, Cur + n);
}
else if(s[0] == 'G')
{
putchar(query(Cur + 1)); enter;
}
else if(s[0] == 'P') Cur--;
else if(s[0] == 'N') Cur++;
}
return 0;
}
[NOI2003],[AHOI2006]文本编辑器的更多相关文章
- BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor
BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor 题意: 分析: splay模拟即可 注意1507的读入格式 ...
- 【bzoj1507】[NOI2003]Editor /【bzoj1269】[AHOI2006]文本编辑器editor Splay
[bzoj1507][NOI2003]Editor 题目描述 输入 输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中 ...
- AHOI2006文本编辑器editor
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1885 Solved: 683[Submit ...
- 【BZOJ1269/1507】[AHOI2006]文本编辑器editor Splay
[BZOJ1269][AHOI2006]文本编辑器editor Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目 ...
- BZOJ 1269: [AHOI2006]文本编辑器editor( splay )
splay..( BZOJ 1507 题目基本相同..双倍经验 ) ------------------------------------------------------------------ ...
- BZOJ 1269: [AHOI2006]文本编辑器editor (splay tree)
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1213 Solved: 454[Submit ...
- BZOJ1269 [AHOI2006]文本编辑器editor 【82行splay】
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MB Submit: 4633 Solved: 1782 [Sub ...
- [AHOI2006] 文本编辑器editor
Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对"文本编辑器"做了一个抽象的定义: ...
- [bzoj1269][AHOI2006文本编辑器editor] (splay模版题 or pb_ds [rope]大法)
Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义: 文本:由0个或 ...
- Bzoj1269 [AHOI2006]文本编辑器editor
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3678 Solved: 1380 Description 这些日子,可可不和卡卡一起玩了,原来可可正 ...
随机推荐
- Redis持久化存储与复制功能简述
一.分布式系统基础理论 分布式系统的两个基础理论: 1.CAP理论 如图: Consistency(强一致性):数据一致更新,所有数据变动都是同步的.Availability(可用性):好的响应性能. ...
- SQL partition by的用法
今天群里看到一个问题,在这里概述下:查询出不同分类下的最新记录.一看这不是很简单的么,要分类那就用Group By; 要最新记录就用Order By呗.然后在自己的表中试着做出来: 首先呢我把表中的数 ...
- Oracle11g em启动报此网站的安全证书有问题的解决方案
http://blog.sina.com.cn/s/blog_a32eff280101cgje.html C:\>emctl status dbconsoleOracle Enterprise ...
- LINQ 【高级查询】
using (Data0216DataContext con = new Data0216DataContext()) { List<Users> ulist = con.Use ...
- 【转】Win10年度更新开发必备:VS2015 Update 3正式版下载汇总
微软在06月27日发布了Visual Studio 2015 Update 3 .在MSDN中微软也提供下载,而且MSDN的Visual Studio 2015 Update 3与官方免费下载的文件是 ...
- 站在DevOps肩膀上的TestOps(一)
一十一 发表于 2018-03-14 15:50:03 TestOps 摘要: DevOps团队的职责是“无摩擦发展”. 这是对“速度需求”驱动的发展理念的一种渴望,以及有意识地去除从概念到客户的 ...
- blfs(systemd版本)学习笔记-配置远程访问和管理lfs系统
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 要实现远程管理和配置lfs系统需要配置以下软件包: 前几页章节脚本的配置:https://www.cnblogs.com/ren ...
- linux学习笔记-软件包的相关知识
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 一.软件包种类 1.源码包 源码包即提供源代码的发行包 2.二进制包 二进制包即提供二进制文件的发行包 安装方法查找二进制包官网 ...
- docker第一章:docker核心概念及centos6下安装
Docker三大核心概念 镜像 容器 仓库 镜像 docker镜像类似于虚拟机镜像,可以将它理解为一个面向Docker引擎的只读模板,包含了文件系统. 容器 1.容器是从镜像创建的应用运行实例,容器和 ...
- 【转】用yum只下载rpm包而不安装
转自:http://liucheng.name/1950/ CentOS用yum安装软件是非常方便的,有时,我们只需要下载其中的rpm包,而不直接安装时咋办呢? 一般情况下,yum是不提供只下载的功能 ...