[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 这些日子,可可不和卡卡一起玩了,原来可可正 ...
随机推荐
- windows 安装 keras
pip install keras 报错了,看报错信息是卡在scipy上了,查了一下 https://stackoverflow.com/questions/42240720/python-scipy ...
- 使用Sandcastle工具为.NET项目工程生成一份项目帮助文档chm
Sandcastle的,由Microsoft创建的,是从创建MSDN风格的文档中使用的工具.NET程序集和关联的XML注释文件.目前的版本是2010年6月发布.这是命令行并没有GUI前端,项目管理功能 ...
- Maven远程仓库的各种配置
1.远程仓库的配置 在平时的开发中,我们往往不会使用默认的中央仓库,默认的中央仓库访问的速度比较慢,访问的人或许很多,有时候也无法满足我们项目的需求,可能项目需要的某些构件中央仓库中是没有的,而在其他 ...
- 【github&&git】5、使用Git拉取GitLab上的项目
一.安装Git(windows版.其他平台参阅) 去Git的官网,下载安装包,安装时,一路默认 二.配置Git 2.1 在任意地方,创建一个文件夹,保证该文件夹的目录全部是英文 2.2 打开新建的文件 ...
- 如何靠谱地查到Tomcat的版本
Tomcat版本获取 一般找jdk的版本的时候,我们直接执行如下命令就可以得知了 java -version 但是Tomcat的版本呢? 除了Tomcat安装目录路径里包含的版本号,还有其他靠谱的获取 ...
- Mybatis关联查询之一对多和多对一XML配置详解
平时在开发过程中dao.bean和XML文件都是自动生成的,很少写XML的配置关系,今天记录一下mybatis的关联查询中的多对一和一对多的情况. 首先是有两张表(学生表Student和老师Teach ...
- Why does the C# compiler translate this != comparison as if it were a > comparison?
Question: I have by pure chance discovered that the C# compiler turns this method: static bool IsNot ...
- DOM.getBoundingClientRect()
- Breeze 部署 Kubernetes 1.12.1高可用集群
今天看文章介绍了一个开源部署 K8S 的工具,有空研究下~ Github 地址: https://github.com/wise2c-devops/breeze
- Harbor api 操作
harbor 的版本为 1.5.2 为 Harbor 配置 swagger 官网参考: https://github.com/goharbor/harbor/blob/v1.5.2/docs/conf ...