Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
- // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx")
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <algorithm>
- #include <set>
- #include <map>
- #include <queue>
- #include <random>
- #include <chrono>
- #include <cassert>
- #include <sstream>
- #include <fstream>
- #include <iomanip>
- #include <tuple>
- #define fi first
- #define se second
- #define pb push_back
- #define ll long long
- #define ld long double
- #define hm unordered_map
- #define pii pair<int, int>
- #define sz(a) (int)a.size()
- #define all(a) a.begin(), a.end()
- #define cinv(v) for (auto& x: v) cin >> x
- #define fr(i, n) for (int i = 0; i < (n); ++i)
- #define fl(i, l, n) for (int i = (l); i < (n); ++i)
- // #define int ll
- template <typename T1, typename T2> inline bool chkmin(T1 &x, const T2 &y) {if (x > y) {x = y; return 1;} return 0;}
- template <typename T1, typename T2> inline bool chkmax(T1 &x, const T2 &y) {if (x < y) {x = y; return 1;} return 0;}
- using namespace std;
- #ifdef LOCAL
- #define dbg(x) cerr << #x << " : " << x << endl
- #else
- #define dbg(x)
- #endif
- //tg: @runningcherry
- template <typename Collection> string Join (const Collection& col) {
- bool first = true;
- stringstream ss;
- for (const auto& x: col) {
- if (!first) ss << ", ";
- first = false;
- ss << x;
- }
- return ss.str ();
- }
- template <typename T1, typename T2> ostream& operator << (ostream& out, const pair<T1, T2>& v) {
- return out << '{' << v.fi << ", " << v.se << '}';
- }
- template<typename T> ostream& operator << (ostream& out, const vector<T>& v) {
- return out << '[' << Join (v) << ']';
- }
- template <typename T1, typename T2> ostream& operator << (ostream& out, const map<T1, T2>& v) {
- return out << '{' << Join (v) << '}';
- }
- template<typename T> ostream& operator << (ostream& out, const set<T>& v) {
- return out << '(' << Join (v) << ')';
- }
- template<typename T> ostream& operator << (ostream& out, const multiset<T>& v) {
- return out << '(' << Join (v) << ')';
- }
- template <typename T1, typename T2> istream& operator >> (istream& in, pair<T1, T2>& a) {
- return in >> a.fi >> a.se;
- }
- const ll inf = (ll) 2e9;
- const ll mod = (ll)1e9 + 7;
- const int maxn = 2e5 + 20;
- mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
- bool g[40000 + 100][4];
- int d[40000 + 100][4][2];
- array<int, 3> pr[40000 + 100][4][2];
- int n;
- vector <int> moo;
- vector <pii> kek;
- inline int corr (int i, int j) {
- return i >= 0 && i < n && j >= 0 && j < 4;
- }
- int next (int i) {
- if (i == sz (moo) - 1) return -1;
- return i + 1;
- }
- void bfs () {
- fr (i, sz (moo)) fr (j, 4) fr (k, 2) d[i][j][k] = inf;
- d[0][0][0] = 0;
- d[0][0][1] = 1;
- deque <vector <int>> a;
- a.pb ({0, 0, 0});
- a.pb ({0, 0, 1});
- while (!a.empty ()) {
- auto v = a.front ();
- a.pop_front ();
- int i = v[0], j = v[1], k = v[2];
- auto ptt = next (i);
- if (ptt != -1) {
- int ii = ptt;
- int dop = k == 0;
- if (!g[ii][j] && chkmin (d[ii][j][1], d[i][j][k] + dop)) {
- a.pb ({ii, j, 1});
- pr[ii][j][1] = {i, j, k};
- }
- }
- for (int t = -1; t <= 1; t += 2) {
- int jj = j + t;
- if (corr (i, jj) && !g[i][jj] && chkmin (d[i][jj][0], d[i][j][k] + 1)) {
- a.pb ({i, jj, 0});
- pr[i][jj][0] = {i, j, k};
- }
- }
- }
- int ans = inf;
- int i = sz (moo) - 1, j, k;
- fr (jj, 4) if (!g[i][jj]) {
- fr (kk, 2) {
- if (chkmin (ans, d[i][jj][kk])) {
- j = jj, k = kk;
- }
- }
- }
- vector<pair <char, int>> res;
- int last = 0;
- while (!(i == 0 && j == 0)) {
- auto ptt = pr[i][j][k];
- int ii = ptt[0], jj = ptt[1], kk = ptt[2];
- if (res.empty ()) {
- if (i == ii) {
- if (j > jj) {
- res.pb ({'D', -1});
- } else if (j < jj) {
- res.pb ({'U', -1});
- } else {
- assert (0);
- }
- } else {
- res.pb ({'R', moo[i] - moo[ii]});
- last = 1;
- }
- } else {
- if (j == jj && last) {
- res.back ().se += moo[i] - moo[ii];
- } else if (j == jj) {
- res.pb ({'R', moo[i] - moo[ii]});
- last = 1;
- } else {
- last = 0;
- if (j > jj) {
- res.pb ({'D', -1});
- } else if (j < jj) {
- res.pb ({'U', -1});
- } else {
- assert (0);
- }
- }
- }
- i = ii, j = jj, k = kk;
- }
- reverse (all (res));
- cout << sz (res) << '\n';
- for (auto& x: res) {
- if (x.fi == 'R') cout << x.fi << ' ' << x.se << '\n';
- else cout << x.fi << '\n';
- }
- }
- void solve () {
- int k;
- cin >> n >> k;
- moo.clear ();
- kek.clear ();
- fr (i, k) {
- int a, b;
- cin >> a >> b;
- --a, --b;
- moo.pb (a);
- if (a != n - 1) moo.pb (a + 1);
- kek.pb ({a, b});
- }
- moo.pb (0);
- moo.pb (1);
- moo.pb (n - 1);
- sort (all (moo));
- moo.resize (unique (all (moo)) - moo.begin ());
- for (auto& x: kek) x.fi = (int)(lower_bound (all (moo), x.fi) - moo.begin ());
- fr (i, sz (moo)) fr (j, 4) {
- g[i][j] = 0;
- }
- for (auto& x: kek) g[x.fi][x.se] = 1;
- bfs ();
- }
- signed main () {
- ios_base::sync_with_stdio (false);
- cin.tie (nullptr);
- int q;
- cin >> q;
- while (q--) solve ();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement