mr_dot_convict

792D-Paths_in_a_complete_binary_tree-Codeforces-mr.convict

Sep 7th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.41 KB | None | 0 0
  1. // Hack it and have it ;) //
  2. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  3.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  4.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  5.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  6.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  7. When I wrote this, only God and I understood what I was doing
  8.  ** * * * * * * * Now, only God knows * * * * * * */
  9. #include      <bits/stdc++.h>
  10. #include      <ext/pb_ds/assoc_container.hpp>
  11. #include      <ext/pb_ds/tree_policy.hpp>
  12. using namespace std;
  13. using namespace __gnu_pbds;
  14. #pragma GCC   optimize ("Ofast")
  15. #pragma GCC   optimize ("unroll-loops")
  16. #pragma GCC   target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  17.  
  18. #define IOS   ios_base::sync_with_stdio(false); cin.tie (nullptr)
  19. #define PREC  cout.precision (10); cout << fixed
  20. #define x     first
  21. #define y     second
  22. #define bg(x) " [ " << #x << " : " << (x) << " ] "
  23. #define un(x) sort(x.begin(), x.end()), \
  24.    x.erase(unique(x.begin(), x.end()), x.end())
  25. using   ll  = long long;
  26. using   ull = unsigned long long;
  27. using   ff  = long double;
  28. using   pii = pair<int,int>;
  29. using   pil = pair<int,ll>;
  30. typedef tree
  31. < int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
  32. ordered_set;
  33.  
  34. struct chash {
  35.    int operator()(pii x) const { return x.x*31 + x.y; }
  36. };
  37. gp_hash_table <pii, int, chash> mp;
  38.  
  39. #if __cplusplus > 201103L
  40. seed_seq seq{
  41.    (uint64_t) chrono::duration_cast<chrono::nanoseconds>
  42.       (chrono::high_resolution_clock::now().time_since_epoch()).count(),
  43.       (uint64_t) __builtin_ia32_rdtsc(),
  44.       (uint64_t) (uintptr_t) make_unique<char>().get()
  45. };
  46. mt19937 rng(seq); //uniform_int_distribution<int> (l, h)(rng); //[low, high]
  47. #else
  48. auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
  49. mt19937 rng(seed);
  50. #endif
  51.  
  52. #define debug(args...) { \
  53.    /* WARNING : do NOT compile this debug func calls with following flags: // \
  54.     * // -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2*/ \
  55.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  56.    stringstream _ss(_s); \
  57.    istream_iterator<string> _it(_ss); err(_it, args); \
  58. }
  59. void err(istream_iterator<string> it) {
  60.    it->empty(); cerr << " (Line : " << __LINE__ << ")" << '\n';
  61. }
  62. template<typename T, typename... Args>
  63. void err(istream_iterator<string> it, T a, Args... args) {
  64.    cerr << fixed << setprecision(15)
  65.       << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  66.    err(++it, args...);
  67. }
  68. /*****************************************************************************/
  69.  
  70. ll n, n_root;
  71. int q;
  72. string s;
  73.  
  74. void solve() {
  75.    ll x = n_root;
  76.  
  77.    for (char ch : s) {
  78.       ll pw2k = x & (-x); // 2 ^ k
  79.       if (ch == 'L') {
  80.          pw2k >>= 1; // 2^(k - 1)
  81.          x -= pw2k;
  82.       }
  83.       else if (ch == 'R') {
  84.          pw2k >>= 1; // 2^(k - 1)
  85.          x += pw2k;
  86.       }
  87.       else {
  88.          if (x == (n + 1)/2) continue;
  89.          if ((x + pw2k) % (pw2k << 1) == 0 && (x + pw2k) % (pw2k << 2) != 0) {
  90.             x += pw2k;
  91.          }
  92.          else if ((x - pw2k) % (pw2k << 1) == 0 && (x - pw2k) % (pw2k << 2) != 0) {
  93.             x -= pw2k;
  94.          }
  95.       }
  96.    }
  97.    cout << x << '\n';
  98. }
  99.  
  100. signed main() {
  101.    IOS; PREC;
  102.    cin >> n >> q;
  103.    while (q--) {
  104.       cin >> n_root >> s;
  105.       solve();
  106.    }
  107.  
  108.    return EXIT_SUCCESS;
  109. }
  110.  
  111. /*
  112.  
  113. void solve_my_way() {
  114.    stack <ll> st;
  115.    int mx_depth = (log2(n + 1));
  116.    ll low = 1, high = n;
  117.    ll mid = (low + high)/2;
  118.    int depth = 1;
  119.    st.push(mid);
  120.  
  121.    while (mid != n_root) {
  122.       if (n_root < mid) {
  123.          high = mid - 1;
  124.       }
  125.       else low = mid + 1;
  126.       mid = (low + high)/2;
  127.       depth += 1;
  128.       st.push(mid);
  129.    }
  130.  
  131.    for (char ch : s) {
  132.       if ((depth == mx_depth && (ch == 'L' || ch == 'R'))
  133.             || (depth == 1 && ch == 'U')) {
  134.          continue;
  135.       }
  136.  
  137.       if (ch == 'L') {
  138.          high = mid - 1;
  139.          mid = (low + high)/2;
  140.          depth += 1;
  141.          st.push(mid);
  142.       }
  143.       else if (ch == 'R') {
  144.          low = mid + 1;
  145.          mid = (low + high)/2;
  146.          depth += 1;
  147.          st.push(mid);
  148.       }
  149.  
  150.       if (ch == 'U') {
  151.          ll t = st.top();
  152.          st.pop();
  153.          mid = st.top();
  154.          depth -= 1;
  155.          if (t < mid) high = low + 2*(high - low + 1);
  156.          else low = high - 2*(high - low + 1);
  157.       }
  158.    }
  159.    cout << mid << '\n';
  160. }
  161. */
Add Comment
Please, Sign In to add comment