mr_dot_convict

E-Lynyrd-Skynyrd-codeforces-mr.convict

Oct 23rd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.63 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.  
  15. #ifndef CONVICTION
  16. #pragma GCC       optimize ("Ofast")
  17. #pragma GCC       optimize ("unroll-loops")
  18. #pragma GCC       target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  19. #endif
  20.  
  21. #define IOS       ios_base::sync_with_stdio(false); cin.tie (nullptr)
  22. #define PREC      cout.precision (10); cout << fixed
  23. #define x         first
  24. #define y         second
  25. #define fr(i,x,y) for (int i = x; i <= y; ++i)
  26. #define fR(i,x,y) for (int i = x; i >= y; --i)
  27. #define cnt(x)    __buildin_popcount(x)
  28. #define cntll(x)  __buildin_popcountll(x)
  29. #define bg(x)     " [ " << #x << " : " << (x) << " ] "
  30. #define un(x)     sort(x.begin(), x.end()), \
  31.                   x.erase(unique(x.begin(), x.end()), x.end())
  32. using   ll  =     long long;
  33. using   ull =     unsigned long long;
  34. using   ff  =     long double;
  35. using   pii =     pair<int,int>;
  36. using   pil =     pair<int,ll>;
  37. using   vi  =     vector <int>;
  38. using   vvi =     vector <vi>;
  39. using   vp  =     vector <pii>;
  40. using   vl  =     vector<ll>;
  41. typedef tree
  42. < int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
  43. ordered_set;
  44.  
  45. struct chash {
  46.    int operator () (pii x) const { return x.x*31 + x.y; }
  47. };
  48. gp_hash_table <pii, int, chash> mp;
  49.  
  50. #if __cplusplus > 201103L
  51. seed_seq seq{
  52.    (uint64_t) chrono::duration_cast<chrono::nanoseconds>
  53.       (chrono::high_resolution_clock::now().time_since_epoch()).count(),
  54.       (uint64_t) __builtin_ia32_rdtsc(),
  55.       (uint64_t) (uintptr_t) make_unique<char>().get()
  56. };
  57. mt19937 rng(seq); //uniform_int_distribution<int> (l, h)(rng); //[low, high]
  58. #else
  59. auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
  60. mt19937 rng(seed);
  61. #endif
  62.  
  63. #define debug(args...) { \
  64.    /* WARNING : do NOT compile this debug func calls with following flags: // \
  65.     * // -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2*/ \
  66.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  67.    stringstream _ss(_s); \
  68.    istream_iterator<string> _it(_ss); err(_it, args); \
  69. }
  70. void err(istream_iterator<string> it) {
  71.    it->empty(); cerr << " (Line : " << __LINE__ << ")" << '\n';
  72. }
  73. template<typename T, typename... Args>
  74. void err(istream_iterator<string> it, T a, Args... args) {
  75.    cerr << fixed << setprecision(15)
  76.       << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  77.    err(++it, args...);
  78. }
  79. /*}}}*/
  80. /*****************************************************************************/
  81. //Don’t practice until you get it right. Practice until you can’t get it wrong
  82. //
  83.  
  84. const int N = (int)2e5 + 10;
  85. int seg[4 * N];
  86. void create (int l, int r, int x, vi &A) {
  87.    if (l == r) {
  88.       seg[x] = A[l];
  89.       return;
  90.    }
  91.    int mid = (l+r)/2;
  92.    create(l, mid, x + x + 1, A);
  93.    create(mid+1, r, x + x + 2, A);
  94.    seg[x] = min(seg[x + x + 1], seg[x + x + 2]);
  95. }
  96.  
  97. int query (int l, int r, int x, int ql, int qr, int m) {
  98.    if (l > qr || r < ql) return m+1;
  99.    if (l >= ql && r <= qr) return seg[x];
  100.    int mid = (l+r)/2;
  101.    int lq = query(l, mid, x + x + 1, ql, qr, m);
  102.    int rq = query(mid+1, r, x + x + 2, ql, qr, m);
  103.    return min(lq, rq);
  104. }
  105.  
  106. signed main() {
  107.    IOS; PREC;
  108.  
  109.    const int D = 21;
  110.    int n, m, q;
  111.    cin >> n >> m >> q;
  112.    vi p(n), A(m);
  113.    vvi table(m, vi(D+1, m+1));
  114.    fr(i, 0, n-1) cin >> p[i];
  115.    fr(i, 0, m-1) cin >> A[i];
  116.  
  117.    vi nxt(n+1, -1);
  118.    fr(i, 0, n-2) nxt[p[i]] = p[i+1];
  119.    nxt[p[n-1]] = p[0];
  120.  
  121.    vi recent_idx(n+1, m+1);
  122.    fR(i, m-1, 0) {
  123.       table[i][0] = recent_idx[nxt[A[i]]];
  124.       recent_idx[A[i]] = i;
  125.    }
  126.  
  127.    fr(k, 1, D) fR(i, m-1, 0)
  128.       if (table[i][k-1] < m)
  129.          table[i][k] = table[table[i][k-1]][k-1];
  130.  
  131.    vi idx(m, m+1);
  132.    fr(i, 0, m-1) {
  133.       int j = i;
  134.       for (int d = 0; d <= D && j < m; ++d)
  135.          if ((1 << d) & (n-1)) j = table[j][d];
  136.       idx[i] = j;
  137.    }
  138.  
  139.    create(0, m-1, 0, idx);
  140.  
  141.    while (q--) {
  142.       int l, r;
  143.       cin >> l >> r;
  144.       --l, --r;
  145.       int r_min_idx = query (0, m-1, 0, l, r, m);
  146.       if (r_min_idx <= r) cout << "1";
  147.       else cout << "0";
  148.    }
  149.    cout << '\n';
  150.    return EXIT_SUCCESS;
  151. }
Add Comment
Please, Sign In to add comment