Submission #38015595
Source Code Expand
#if IN_LOCAL
import local;
#endif // IN_LOCAL
#include<random>
#include<chrono>
#include<string>
#include<iostream>
using namespace std;
using namespace chrono;
mt19937 rng(static_cast<unsigned>(high_resolution_clock::now().time_since_epoch().count()));
struct Tree
{
unsigned root, counter, init_weight[500005];
struct Node
{
bool reversed;
unsigned lc, rc, siz, weight, father;
}node[500005];
inline void Init(const unsigned n)
{
for (unsigned i = 1; i <= n; init_weight[i++] = rng());
sort(init_weight + 1, init_weight + (n + 1));
root = Build(1, n);
return;
}
inline unsigned Build(const unsigned l, const unsigned r)
{
const unsigned mid = (l + r) / 2;
node[mid].weight = init_weight[++counter];
node[mid].siz = r - l + 1;
if (l < mid)
{
node[node[mid].lc = Build(l, mid - 1)].father = mid;
}
if (mid < r)
{
node[node[mid].rc = Build(mid + 1, r)].father = mid;
}
return mid;
}
inline void AddTag(const unsigned pos)
{
node[pos].reversed = !node[pos].reversed;
swap(node[pos].lc, node[pos].rc);
return;
}
inline void DownTag(const unsigned pos)
{
if (node[pos].reversed)
{
if (node[pos].lc)
{
AddTag(node[pos].lc);
}
if (node[pos].rc)
{
AddTag(node[pos].rc);
}
node[pos].reversed = 0;
}
return;
}
inline void Maintain(const unsigned pos)
{
node[pos].siz = node[node[pos].lc].siz + node[node[pos].rc].siz + 1;
return;
}
inline unsigned Split(unsigned& root, unsigned limit)
{
unsigned p = root;
for (DownTag(p); limit != node[node[p].lc].siz; DownTag(p))
{
if (limit < node[node[p].lc].siz)
{
p = node[p].lc;
}
else
{
limit -= node[node[p].lc].siz + 1;
p = node[p].rc;
}
}
if (p)
{
unsigned a = node[p].lc;
if (a)
{
node[a].father = node[p].lc = 0;
node[p].siz -= node[a].siz;
}
for (unsigned i = node[p].father, t = p; i; t = i, i = node[i].father)
{
node[t].father = 0;
if (t == node[i].lc)
{
node[node[i].lc = p].father = i;
Maintain(p = i);
}
else
{
node[node[i].rc = a].father = i;
Maintain(a = i);
}
}
root = a;
}
return p;
}
inline unsigned Merge(const unsigned a, const unsigned b)
{
if (a && b)
{
DownTag(a);
DownTag(b);
if (node[a].weight < node[b].weight)
{
node[a].siz += node[b].siz;
return node[node[a].rc = Merge(node[a].rc, b)].father = a;
}
node[b].siz += node[a].siz;
return node[node[b].lc = Merge(a, node[b].lc)].father = b;
}
return a | b;
}
inline void Print(const unsigned pos)
{
DownTag(pos);
if (node[pos].lc)
{
Print(node[pos].lc);
}
init_weight[++counter] = pos;
if (node[pos].rc)
{
Print(node[pos].rc);
}
return;
}
inline void Reverse(const unsigned l, const unsigned r)
{
unsigned p = Split(root, l - 1), q = Split(p, r - l + 1);
AddTag(p);
root = Merge(Merge(root, p), q);
return;
}
}t1, t2;
string a[500005];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
unsigned h, w;
cin >> h >> w;
t1.counter = t2.counter = 0;
t1.Init(h);
t2.Init(w);
for (unsigned i = 1; i <= h; cin >> a[i], a[i] = " " + a[i], ++i);
unsigned q;
cin >> q;
for (unsigned i = 1; i <= q; ++i)
{
unsigned a, b;
cin >> a >> b;
t1.Reverse(1, a);
t1.Reverse(a + 1, h);
t2.Reverse(1, b);
t2.Reverse(b + 1, w);
}
t1.counter = t2.counter = 0;
t1.Print(t1.root);
t2.Print(t2.root);
const auto& x = t1.init_weight, & y = t2.init_weight;
for (unsigned i = 1; i <= h; ++i)
{
for (unsigned j = 1; j <= w; cout.put(a[x[i]][y[j]]), ++j);
cout.put('\n');
}
return 0;
}
Submission Info
| Submission Time |
|
| Task |
B - Grid Rotations |
| User |
LXl491214 |
| Language |
C++ (Clang 10.0.0) |
| Score |
500 |
| Code Size |
3816 Byte |
| Status |
AC |
| Exec Time |
516 ms |
| Memory |
22228 KiB |
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
500 / 500 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
01_sample_01.txt, 01_sample_02.txt, 01_sample_03.txt |
| All |
01_sample_01.txt, 01_sample_02.txt, 01_sample_03.txt, 02_small_01.txt, 02_small_02.txt, 02_small_03.txt, 02_small_04.txt, 02_small_05.txt, 02_small_06.txt, 02_small_07.txt, 02_small_08.txt, 02_small_09.txt, 03_rand_01.txt, 03_rand_02.txt, 03_rand_03.txt, 03_rand_04.txt, 03_rand_05.txt, 04_square_01.txt, 04_square_02.txt, 04_square_03.txt, 04_square_04.txt, 04_square_05.txt, 05_max_H_01.txt, 05_max_H_02.txt, 05_max_H_03.txt, 05_max_H_04.txt, 05_max_H_05.txt, 06_max_W_01.txt, 06_max_W_02.txt, 06_max_W_03.txt, 06_max_W_04.txt, 06_max_W_05.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 01_sample_01.txt |
AC |
24 ms |
14708 KiB |
| 01_sample_02.txt |
AC |
15 ms |
14708 KiB |
| 01_sample_03.txt |
AC |
15 ms |
14708 KiB |
| 02_small_01.txt |
AC |
16 ms |
14780 KiB |
| 02_small_02.txt |
AC |
14 ms |
14752 KiB |
| 02_small_03.txt |
AC |
14 ms |
14660 KiB |
| 02_small_04.txt |
AC |
12 ms |
14756 KiB |
| 02_small_05.txt |
AC |
19 ms |
14836 KiB |
| 02_small_06.txt |
AC |
13 ms |
14752 KiB |
| 02_small_07.txt |
AC |
14 ms |
14756 KiB |
| 02_small_08.txt |
AC |
13 ms |
14744 KiB |
| 02_small_09.txt |
AC |
13 ms |
14780 KiB |
| 03_rand_01.txt |
AC |
411 ms |
15096 KiB |
| 03_rand_02.txt |
AC |
420 ms |
15128 KiB |
| 03_rand_03.txt |
AC |
340 ms |
14900 KiB |
| 03_rand_04.txt |
AC |
427 ms |
15692 KiB |
| 03_rand_05.txt |
AC |
384 ms |
14880 KiB |
| 04_square_01.txt |
AC |
427 ms |
15304 KiB |
| 04_square_02.txt |
AC |
430 ms |
15164 KiB |
| 04_square_03.txt |
AC |
425 ms |
15328 KiB |
| 04_square_04.txt |
AC |
429 ms |
15296 KiB |
| 04_square_05.txt |
AC |
429 ms |
15204 KiB |
| 05_max_H_01.txt |
AC |
510 ms |
21456 KiB |
| 05_max_H_02.txt |
AC |
511 ms |
21452 KiB |
| 05_max_H_03.txt |
AC |
516 ms |
21672 KiB |
| 05_max_H_04.txt |
AC |
508 ms |
21440 KiB |
| 05_max_H_05.txt |
AC |
512 ms |
21620 KiB |
| 06_max_W_01.txt |
AC |
475 ms |
22228 KiB |
| 06_max_W_02.txt |
AC |
485 ms |
22120 KiB |
| 06_max_W_03.txt |
AC |
474 ms |
22072 KiB |
| 06_max_W_04.txt |
AC |
477 ms |
22216 KiB |
| 06_max_W_05.txt |
AC |
471 ms |
22172 KiB |