Advertisement
Galebickosikasa

Untitled

Apr 22nd, 2021
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. // #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
  2. // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx")
  3.  
  4. #include <iostream>
  5. #include <vector>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <set>
  9. #include <map>
  10. #include <queue>
  11. #include <random>
  12. #include <chrono>
  13. #include <cassert>
  14. #include <sstream>
  15. #include <fstream>
  16. #include <iomanip>
  17. #include <tuple>
  18.  
  19. #define fi first
  20. #define se second
  21. #define pb push_back
  22. #define ll long long
  23. #define ld long double
  24. #define hm unordered_map
  25. #define pii pair<int, int>
  26. #define sz(a) (int)a.size()
  27. #define all(a) a.begin(), a.end()
  28. #define cinv(v) for (auto& x: v) cin >> x
  29. #define fr(i, n) for (int i = 0; i < (n); ++i)
  30. #define fl(i, l, n) for (int i = (l); i < (n); ++i)
  31.  
  32. // #define int ll
  33.  
  34. template <typename T1, typename T2> inline bool chkmin(T1 &x, const T2 &y) {if (x > y) {x = y; return 1;} return 0;}
  35. template <typename T1, typename T2> inline bool chkmax(T1 &x, const T2 &y) {if (x < y) {x = y; return 1;} return 0;}
  36.  
  37. using namespace std;
  38.  
  39. #ifdef LOCAL
  40. #define dbg(x) cerr << #x << " : " << x << endl
  41. #else
  42. #define dbg(x)
  43. #endif
  44.  
  45. //tg: @runningcherry
  46.  
  47. template <typename Collection> string Join (const Collection& col) {
  48. bool first = true;
  49. stringstream ss;
  50. for (const auto& x: col) {
  51. if (!first) ss << ", ";
  52. first = false;
  53. ss << x;
  54. }
  55. return ss.str ();
  56. }
  57.  
  58. template <typename T1, typename T2> ostream& operator << (ostream& out, const pair<T1, T2>& v) {
  59. return out << '{' << v.fi << ", " << v.se << '}';
  60. }
  61.  
  62. template<typename T> ostream& operator << (ostream& out, const vector<T>& v) {
  63. return out << '[' << Join (v) << ']';
  64. }
  65.  
  66. template <typename T1, typename T2> ostream& operator << (ostream& out, const map<T1, T2>& v) {
  67. return out << '{' << Join (v) << '}';
  68. }
  69.  
  70. template<typename T> ostream& operator << (ostream& out, const set<T>& v) {
  71. return out << '(' << Join (v) << ')';
  72. }
  73.  
  74. template<typename T> ostream& operator << (ostream& out, const multiset<T>& v) {
  75. return out << '(' << Join (v) << ')';
  76. }
  77.  
  78. template <typename T1, typename T2> istream& operator >> (istream& in, pair<T1, T2>& a) {
  79. return in >> a.fi >> a.se;
  80. }
  81.  
  82. const ll inf = (ll) 2e9;
  83. const ll mod = (ll)1e9 + 7;
  84.  
  85. const int maxn = 2e5 + 20;
  86.  
  87. mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
  88.  
  89. bool g[40000 + 100][4];
  90. int d[40000 + 100][4][2];
  91. array<int, 3> pr[40000 + 100][4][2];
  92. int n;
  93. vector <int> moo;
  94. vector <pii> kek;
  95.  
  96. inline int corr (int i, int j) {
  97. return i >= 0 && i < n && j >= 0 && j < 4;
  98. }
  99.  
  100. int next (int i) {
  101. if (i == sz (moo) - 1) return -1;
  102. return i + 1;
  103. }
  104.  
  105. void bfs () {
  106. fr (i, sz (moo)) fr (j, 4) fr (k, 2) d[i][j][k] = inf;
  107. d[0][0][0] = 0;
  108. d[0][0][1] = 1;
  109. deque <vector <int>> a;
  110. a.pb ({0, 0, 0});
  111. a.pb ({0, 0, 1});
  112. while (!a.empty ()) {
  113. auto v = a.front ();
  114. a.pop_front ();
  115. int i = v[0], j = v[1], k = v[2];
  116. auto ptt = next (i);
  117. if (ptt != -1) {
  118. int ii = ptt;
  119. int dop = k == 0;
  120. if (!g[ii][j] && chkmin (d[ii][j][1], d[i][j][k] + dop)) {
  121. a.pb ({ii, j, 1});
  122. pr[ii][j][1] = {i, j, k};
  123. }
  124. }
  125. for (int t = -1; t <= 1; t += 2) {
  126. int jj = j + t;
  127. if (corr (i, jj) && !g[i][jj] && chkmin (d[i][jj][0], d[i][j][k] + 1)) {
  128. a.pb ({i, jj, 0});
  129. pr[i][jj][0] = {i, j, k};
  130. }
  131. }
  132. }
  133. int ans = inf;
  134. int i = sz (moo) - 1, j, k;
  135. fr (jj, 4) if (!g[i][jj]) {
  136. fr (kk, 2) {
  137. if (chkmin (ans, d[i][jj][kk])) {
  138. j = jj, k = kk;
  139. }
  140. }
  141. }
  142. vector<pair <char, int>> res;
  143. int last = 0;
  144. while (!(i == 0 && j == 0)) {
  145. auto ptt = pr[i][j][k];
  146. int ii = ptt[0], jj = ptt[1], kk = ptt[2];
  147. if (res.empty ()) {
  148. if (i == ii) {
  149. if (j > jj) {
  150. res.pb ({'D', -1});
  151. } else if (j < jj) {
  152. res.pb ({'U', -1});
  153. } else {
  154. assert (0);
  155. }
  156. } else {
  157. res.pb ({'R', moo[i] - moo[ii]});
  158. last = 1;
  159. }
  160. } else {
  161. if (j == jj && last) {
  162. res.back ().se += moo[i] - moo[ii];
  163. } else if (j == jj) {
  164. res.pb ({'R', moo[i] - moo[ii]});
  165. last = 1;
  166. } else {
  167. last = 0;
  168. if (j > jj) {
  169. res.pb ({'D', -1});
  170. } else if (j < jj) {
  171. res.pb ({'U', -1});
  172. } else {
  173. assert (0);
  174. }
  175. }
  176. }
  177. i = ii, j = jj, k = kk;
  178. }
  179. reverse (all (res));
  180. cout << sz (res) << '\n';
  181. for (auto& x: res) {
  182. if (x.fi == 'R') cout << x.fi << ' ' << x.se << '\n';
  183. else cout << x.fi << '\n';
  184. }
  185. }
  186.  
  187. void solve () {
  188. int k;
  189. cin >> n >> k;
  190. moo.clear ();
  191. kek.clear ();
  192. fr (i, k) {
  193. int a, b;
  194. cin >> a >> b;
  195. --a, --b;
  196. moo.pb (a);
  197. if (a != n - 1) moo.pb (a + 1);
  198. kek.pb ({a, b});
  199. }
  200. moo.pb (0);
  201. moo.pb (1);
  202. moo.pb (n - 1);
  203. sort (all (moo));
  204. moo.resize (unique (all (moo)) - moo.begin ());
  205. for (auto& x: kek) x.fi = (int)(lower_bound (all (moo), x.fi) - moo.begin ());
  206. fr (i, sz (moo)) fr (j, 4) {
  207. g[i][j] = 0;
  208. }
  209. for (auto& x: kek) g[x.fi][x.se] = 1;
  210. bfs ();
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217. }
  218.  
  219. signed main () {
  220. ios_base::sync_with_stdio (false);
  221. cin.tie (nullptr);
  222. int q;
  223. cin >> q;
  224. while (q--) solve ();
  225.  
  226.  
  227.  
  228.  
  229.  
  230. }
  231.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement