[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 这些日子,可可不和卡卡一起玩了,原来可可正 ...
随机推荐
- cJSON填坑记
1. 艰辛的过程 最近做了一个嵌入式的项目,需要与服务器进行连接.为了方便服务器处理数据,经商定后统一采用JSON形式进行数据的传输. 以前没有用过JSON格式进行数据处理,所以上网搜索了一下,很多人 ...
- python中的函数对象的内存地址是多少
今天和同学讨论一个问题,发现了函数的内存地址和我想象的不一样. 我以为同一个函数,假如给的参数不一样,那么这两个函数的id就不一样. 然后经过实验,发现python为了便于管理函数,所有的函数都放在同 ...
- ModBus通信协议的【传输方式】
1.Modbus 传输方式 标准的Modbus口是使用一RS-232C兼容串行接口,它定义了连接口的针脚.电缆.信号位.传输波特率.奇偶校验.控制器能直接或经由Modem组网. 控制器通信使用 ...
- 使用Maven搭建JFinal环境
使用Maven搭建JFinal环境 工具:IDEA 2017 JFinal版本:3.4 一.Maven项目创建 选择maven模板进行创建 填写GroupId和ArtifactId 一路Next即可 ...
- 两个inline-block消除间距和对齐(vertical-align)
一.神奇的两个inline-block 很初级的问题,无聊决定写一个故事. 故事的主人公很简单,两个inline-block元素.代码如下,为了看起来简单明了,写得很简陋.效果图如右.发现有两个问题. ...
- H5调拨打电话界面
<a href=”tel:15771791266 ”>拨打电话</a> 切记不要用js调用 直接用a标签 苹果安卓塞班都能调起来
- sigar获取Windows系统的硬件信息进行JAVA后台系统资源监控
windows下,将sigar-amd64-winnt.dll复制到jdk的bin目录下或者拷贝到WEB-INF/lib下 linux下,将libsigar-amd64-linux.so拷贝到jdk的 ...
- 【代码笔记】Web-HTML-表格
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- map标签
map和area 标签配合img标签制作分区超链接效果 http://www.w3school.com.cn/tiy/t.asp?f=html_areamap
- SuperMap iServer 扩展/JAVA API 系列博客整理
转载:http://blog.csdn.net/supermapsupport/article/details/70158940 SuperMap iServer为广大用户提供了整套 SDK,应用开发 ...