QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#498863 | #6556. Text Editor | pandapythoner | 0 | 1ms | 3896kb | C++23 | 7.3kb | 2024-07-30 20:49:10 | 2024-08-09 23:07:27 |
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ll long long
#define flt double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define rep(i, n) for(int i = 0; i < (n); i += 1)
#define len(a) ((int)(a).size())
#ifdef LOCAL
bool local = true;
#else
bool local = false;
#endif
const int max_nodes = 1e6;
mt19937_64 rnd(122343);
string container;
struct node {
int xl = 0, xr = 0;
int l = -1;
int r = -1;
ull size = 0;
};
int cur_size = 0;
node t[max_nodes];
int new_node() {
assert(cur_size < max_nodes);
t[cur_size] = node();
return cur_size++;
}
int new_node(int v) {
assert(cur_size < max_nodes and v < cur_size and v >= 0);
t[cur_size] = t[v];
return cur_size++;
}
ull size(int v) {
return v == -1 ? 0 : t[v].size;
}
void upd(int v) {
if (v == -1) return;
t[v].size = size(t[v].l) + size(t[v].r) + t[v].xr - t[v].xl + 1;
}
int merge(int u, int v) {
if (u == -1 or v == -1) return u + v + 1;
ull szu = size(u);
ull szv = size(v);
if (rnd() % (szu + szv) < szu) {
u = new_node(u);
t[u].r = merge(t[u].r, v);
upd(u);
return u;
} else {
v = new_node(v);
t[v].l = merge(u, t[v].l);
upd(v);
return v;
}
}
pair<int, int> split(int v, ull k) {
if (v == -1) return make_pair(-1, -1);
if (k == 0) return make_pair(-1, v);
if (k >= size(v)) return make_pair(v, -1);
v = new_node(v);
ull vlen = (t[v].xr - t[v].xl + 1);
ull lsize = size(t[v].l);
if (k <= lsize) {
auto [a, b] = split(t[v].l, k);
t[v].l = b;
upd(v);
return make_pair(a, v);
}
if (k >= lsize + vlen) {
auto [a, b] = split(t[v].r, k - lsize - vlen);
t[v].r = a;
upd(v);
return make_pair(v, b);
}
int u = new_node(v);
t[u].xr = t[u].xl + (k - lsize) - 1;
t[v].xl = t[u].xr + 1;
t[u].r = -1;
t[v].l = -1;
upd(u);
upd(v);
return make_pair(u, v);
}
array<int, 3> split(int v, ull l, ull r) {
auto [ab, c] = split(v, r);
auto [a, b] = split(ab, l);
return { a, b, c };
}
void print_rec(int v) {
if (v == -1) return;
assert(size(v) <= 1e6);
print_rec(t[v].l);
for (int i = t[v].xl; i <= t[v].xr; i += 1) {
cout << container[i];
}
print_rec(t[v].r);
}
void print(int v) {
print_rec(v);
cout << "\n";
}
vector<int> checkpoints;
int cur = 0;
int clipboard = -1;
string encode(string s) {
string t;
for (auto x : s) {
if (x == ' ') {
t += "\\_";
continue;
}
if (x == '\\') {
t += "\\\\";
continue;
}
t += x;
}
return t;
}
string decode(string s) {
string t;
bool special = false;
for (auto x : s) {
if (special) {
assert(x == '\\' or x == '_');
if (x == '\\') t += '\\';
else if (x == '_') t += ' ';
else assert(0);
special = false;
continue;
}
if (x == '\\') {
special = true;
continue;
}
t += x;
}
assert(!special);
return t;
}
template<class T>
void do_some_things(T& in, bool silent = false) {
int n;
in >> n;
stringstream commands;
commands << n << " ";
rep(i, n) {
string command;
in >> command;
commands << command << " ";
if (command == "deserialize") {
string serialized;
cin >> serialized;
stringstream super;
super << decode(serialized);
do_some_things(super, true);
int v = checkpoints[cur];
checkpoints = { -1, v };
cur = 1;
clipboard = -1;
} else if (command == "insert") {
ull p;
in >> p;
string s;
in >> s;
commands << p << " " << s << " ";
int xl = len(container);
container += s;
int xr = len(container);
checkpoints.resize(cur + 1);
int v = checkpoints[cur];
auto [a, c] = split(v, p);
int b = new_node();
t[b].xl = xl; t[b].xr = xr; upd(b);
v = merge(merge(a, b), c);
checkpoints.push_back(v);
cur++;
} else if (command == "erase") {
ull l, r;
in >> l >> r;
commands << l << " " << r << " ";
checkpoints.resize(cur + 1);
int v = checkpoints[cur];
auto [a, b, c] = split(v, l, r);
v = merge(a, c);
checkpoints.push_back(v);
cur++;
} else if (command == "print") {
ull l, r;
in >> l >> r;
commands << l << " " << r << " ";
int v = checkpoints[cur];
auto [a, b, c] = split(v, l, r);
if (!silent) {
print(b);
}
} else if (command == "copy") {
ull l, r;
in >> l >> r;
commands << l << " " << r << " ";
int v = checkpoints[cur];
auto [a, b, c] = split(v, l, r);
clipboard = b;
} else if (command == "cut") {
ull l, r;
in >> l >> r;
commands << l << " " << r << " ";
checkpoints.resize(cur + 1);
int v = checkpoints[cur];
auto [a, b, c] = split(v, l, r);
clipboard = b;
v = merge(a, c);
checkpoints.push_back(v);
cur++;
} else if (command == "paste") {
ull p;
in >> p;
commands << p << " ";
checkpoints.resize(cur + 1);
int v = checkpoints[cur];
auto [a, c] = split(v, p);
int b = clipboard;
v = merge(merge(a, b), c);
checkpoints.push_back(v);
cur++;
} else if (command == "undo") {
if (cur > 0) {
cur--;
}
} else if (command == "redo") {
if (cur + 1 < len(checkpoints)) {
cur++;
}
} else if (command == "serialize") {
if (!silent) {
string serialized = encode(commands.str());
cout << serialized << "\n";
}
}
}
}
int32_t main() {
if (local) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
checkpoints = { -1 };
cur = 0;
container = "";
clipboard = -1;
do_some_things(cin);
return 0;
}
/*
17
insert 0 abcdef
print 0 6
erase 4 5
print 0 5
copy 0 3
paste 1
print 0 8
cut 2 4
print 0 6
undo
print 0 8
paste 6
print 0 10
redo
redo
print 0 10
serialize
2
deserialize 17\_insert\_0\_abcdef\_print\_0\_6\_erase\_4\_5\_print\_0\_5\_copy\_0\_3\_paste\_1\_print\_0\_8\_cut\_2\_4\_print\_0\_6\_undo\_print\_0\_8\_paste\_6\_print\_0\_10\_redo\_redo\_print\_0\_10\_serialize\_
print 0 10
*/
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3664kb
First Run Input
17 insert 0 abcdef print 0 6 erase 4 5 print 0 5 copy 0 3 paste 1 print 0 8 cut 2 4 print 0 6 undo print 0 8 paste 6 print 0 10 redo redo print 0 10 serialize
First Run Output
abcdef abcdf aabcbcdf aabcdf aabcbcdf aabcbcbcdf aabcbcbcdf 17\_insert\_0\_abcdef\_print\_0\_6\_erase\_4\_5\_print\_0\_5\_copy\_0\_3\_paste\_1\_print\_0\_8\_cut\_2\_4\_print\_0\_6\_undo\_print\_0\_8\_paste\_6\_print\_0\_10\_redo\_redo\_print\_0\_10\_serialize\_
Second Run Input
2 deserialize 17\_insert\_0\_abcdef\_print\_0\_6\_erase\_4\_5\_print\_0\_5\_copy\_0\_3\_paste\_1\_print\_0\_8\_cut\_2\_4\_print\_0\_6\_undo\_print\_0\_8\_paste\_6\_print\_0\_10\_redo\_redo\_print\_0\_10\_serialize\_ print 0 10
Second Run Output
aabcbcbcdf
result:
ok stage 2 is ok!
Test #2:
score: 100
Accepted
time: 0ms
memory: 3664kb
First Run Input
1 serialize
First Run Output
1\_serialize\_
Second Run Input
1 deserialize 1\_serialize\_
Second Run Output
result:
ok stage 2 is ok!
Test #3:
score: 100
Accepted
time: 0ms
memory: 3896kb
First Run Input
31 undo redo redo undo undo undo redo undo redo undo undo undo undo undo redo redo undo undo redo undo redo redo redo undo undo undo redo undo redo redo serialize
First Run Output
31\_undo\_redo\_redo\_undo\_undo\_undo\_redo\_undo\_redo\_undo\_undo\_undo\_undo\_undo\_redo\_redo\_undo\_undo\_redo\_undo\_redo\_redo\_redo\_undo\_undo\_undo\_redo\_undo\_redo\_redo\_serialize\_
Second Run Input
31 deserialize 31\_undo\_redo\_redo\_undo\_undo\_undo\_redo\_undo\_redo\_undo\_undo\_undo\_undo\_undo\_redo\_redo\_undo\_undo\_redo\_undo\_redo\_redo\_redo\_undo\_undo\_undo\_redo\_undo\_redo\_redo\_serialize\_ redo undo redo redo redo redo redo undo undo undo redo redo undo redo undo redo undo redo...
Second Run Output
result:
ok stage 2 is ok!
Test #4:
score: 100
Accepted
time: 0ms
memory: 3640kb
First Run Input
31 undo redo redo undo undo redo redo redo undo undo undo redo redo undo redo undo redo undo redo redo redo undo redo undo redo redo undo undo redo undo serialize
First Run Output
31\_undo\_redo\_redo\_undo\_undo\_redo\_redo\_redo\_undo\_undo\_undo\_redo\_redo\_undo\_redo\_undo\_redo\_undo\_redo\_redo\_redo\_undo\_redo\_undo\_redo\_redo\_undo\_undo\_redo\_undo\_serialize\_
Second Run Input
31 deserialize 31\_undo\_redo\_redo\_undo\_undo\_redo\_redo\_redo\_undo\_undo\_undo\_redo\_redo\_undo\_redo\_undo\_redo\_undo\_redo\_redo\_redo\_undo\_redo\_undo\_redo\_redo\_undo\_undo\_redo\_undo\_serialize\_ undo redo undo redo redo redo undo undo undo redo undo redo undo redo undo undo redo undo...
Second Run Output
result:
ok stage 2 is ok!
Test #5:
score: 100
Accepted
time: 0ms
memory: 3684kb
First Run Input
31 undo undo undo redo redo redo redo undo undo redo redo undo undo undo redo redo redo redo undo redo redo undo undo undo redo undo undo undo undo redo serialize
First Run Output
31\_undo\_undo\_undo\_redo\_redo\_redo\_redo\_undo\_undo\_redo\_redo\_undo\_undo\_undo\_redo\_redo\_redo\_redo\_undo\_redo\_redo\_undo\_undo\_undo\_redo\_undo\_undo\_undo\_undo\_redo\_serialize\_
Second Run Input
31 deserialize 31\_undo\_undo\_undo\_redo\_redo\_redo\_redo\_undo\_undo\_redo\_redo\_undo\_undo\_undo\_redo\_redo\_redo\_redo\_undo\_redo\_redo\_undo\_undo\_undo\_redo\_undo\_undo\_undo\_undo\_redo\_serialize\_ redo redo undo undo undo redo redo redo redo redo redo redo redo redo undo undo redo undo...
Second Run Output
result:
ok stage 2 is ok!
Test #6:
score: 0
Wrong Answer on the first run
First Run Input
1001 insert 0 u]^rGH]V+A3/VCu}4-dod,hyG&]WkL1)rg$W4\W0XL7sfyA[GNpfX2rx8Sc6$fmzW&x3E//Q0M\7=?Io7mupWV9Y4z6aY4E9ia$S{1KfioW29lST';?emw,Upk.b`^tl.O^btxvAx>:=&rC@6k`[GQCv;s[myKiSV1tp!Z)bj: copy 0 170 paste 170 copy 0 340 paste 340 cut 415 652 copy 0 443 paste 443 cut 282 500 erase 0 576 copy 0 92 paste ...
First Run Output
:/VCu}4-dod,hyG&]WkL1)rg)bj/VCu}4-dyKlPm}+uRyG&]WkL1)rg)bj:/V)bj:/VCug)bj:/)rg)bj:/VyG&]WkL1)rg)bj/VCu}4-dyKlPm}+uRyG&]WkL1)rg)bj:/V /VVCu}/VVCu} }/VVCu}/VVCu}/VVCu}/VVCu}/VVCu}/}/VVCu}/VVCu}/VVCu}/VVCu}/VVCu}/VVCu}/VVCu}/VVCu}/VVCu}/VVCu VCu}//VVCVCu}/VVCu}/CVCu}/VVCu}/VVCuVCVCu}/VCu}/VVCu}/VVCuVCV...
Second Run Input
Second Run Output
result:
wrong answer wrong answer on query 1 (in the first run)