Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P4008 [NOI2003] 文本编辑器

P4008 [NOI2003] 文本编辑器

本来想自己再用 splay 写一次,但是还是觉得块状链表更合适。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int N = 2010, M = 20010;
int n, x, y;
queue <int> q;
char str[20000010];
struct Node
{
char s[N];
int c, l, r;
}p[M];
void move(int k)
{
x = p[0].r;
while (k > p[x].c)
{
k -= p[x].c;
x = p[x].r;
}
y = k - 1;
}
void add(int u, int v)
{
p[v].r = p[u].r;
p[p[u].r].l = v;
p[u].r = v, p[v].l = u;
}
void del(int u)
{
p[p[u].l].r = p[u].r;
p[p[u].r].l = p[u].l;
p[u].l = p[u].r = p[u].c = 0;
q.push(u);
}
void insert(int k)
{

if (y < p[x].c - 1)
{
int u = q.front();
q.pop();
for (int i = y + 1; i < p[x].c; ++i)
p[u].s[p[u].c++] = p[x].s[i];
p[x].c = y + 1;
add(x, u);
}
int cur = x;
for (int i = 0; i < k;)
{
int u = q.front();
q.pop();
while (p[u].c < N && i < k)
p[u].s[p[u].c++] = str[i++];
add(cur, u);
cur = u;
}
}
void remove(int k)
{
if (p[x].c - 1 - y >= k)
{
for (int i = y + k + 1, j = y + 1; i < p[x].c; ++i, ++j)
p[x].s[j] = p[x].s[i];
p[x].c -= k;
}
else
{
k -= p[x].c - y - 1;
p[x].c = y + 1;
while (p[x].r && k >= p[p[x].r].c)
{
int u = p[x].r;
k -= p[u].c;
del(u);
}
int u = p[x].r;
for (int i = 0, j = k; j < p[u].c; ++i, ++j)
p[u].s[i] = p[u].s[j];
p[u].c -= k;
}
}
void get(int k)
{
if (p[x].c - 1 - y >= k)
{
for (int i = 0, j = y + 1; i < k; ++i, ++j)
cout << p[x].s[j];
cout << endl;
return;
}
k -= p[x].c - y - 1;
for (int i = y + 1; i < p[x].c; ++i)
cout << p[x].s[i];
int cur = x;
while (p[cur].r && k >= p[p[cur].r].c)
{
int u = p[cur].r;
for (int i = 0; i < p[u].c; ++i)
cout << p[u].s[i];
k -= p[u].c;
cur = u;
}
int u = p[cur].r;
for (int i = 0; i < k; ++i)
cout << p[u].s[i];
cout << endl;
}
void pre()
{
if (!y)
{
x = p[x].l;
y = p[x].c - 1;
}
else
y--;
}
void nxt()
{
if (y < p[x].c - 1)
y++;
else
{
x = p[x].r;
y = 0;
}
}
void merge()
{
for (int i = p[0].r; i; i = p[i].r)
while (p[i].r && p[i].c + p[p[i].r].c < N)
{
int r = p[i].r;
for (int j = p[i].c, k = 0; k < p[r].c; ++j, ++k)
p[i].s[j] = p[r].s[k];
if (x == r)
x = i, y += p[i].c;
p[i].c += p[r].c;
del(r);
}
}
int main()
{
for (int i = 1; i < M; ++i)
q.push(i);
cin >> n;
char op[10];
str[0] = '>';
insert(1);
move(1);
while (n--)
{
int a;
cin >> op;
if (!strcmp(op, "Move"))
{
cin >> a;
move(a + 1);
}
else if (!strcmp(op, "Insert"))
{
cin >> a;
int i = 0, k = a;
while (a)
{
str[i] = getchar();
if (str[i] >= 32 && str[i] <= 126)
i++, a--;
}
insert(k);
merge();
}
else if (!strcmp(op, "Delete"))
{
cin >> a;
remove(a);
merge();
}
else if (!strcmp(op, "Get"))
{
cin >> a;
get(a);
}
else if (!strcmp(op, "Prev"))
pre();
else
nxt();
}
return 0;
}