Advertisement
yungyao

tioj 1927

Jul 3rd, 2021
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.09 KB | None | 0 0
  1. /*
  2.  
  3. weak        weak  we      ak   we  akwea        weak  we
  4.   weak    weak    we      ak   weak    weak    we  ak we
  5.     weakweak      we      ak   wea       ak   we    akwe
  6.       wea         we      ak   we        ak   we    akwe
  7.       wea         we      ak   we        ak   we    akwe
  8.       wea          eak  weak   we        ak    we  ak we
  9.       wea            wea  ak   we        ak     weak  we
  10.                                                       we
  11. we      ak     wea  ak       weak                     we
  12.  we    ak    wea  weak     wea  eak                   we
  13.   we  ak    we      ak   wea      wea         we      we
  14.    weak     we      ak   we        we         we      we
  15.     we      we      ak   we        we         we      we
  16.    we        wea  weak    wea    wea          weak  weak
  17. weak           wea  akw      weak                weak
  18.  
  19.  
  20. */
  21. using namespace std;
  22. #include <vector>
  23. #include <queue>
  24. #include <algorithm>
  25. #include <cmath>
  26. #include <utility>
  27. #include <bitset>
  28. #include <set>
  29. #include <string>
  30. #include <stack>
  31. #include <iomanip>
  32. #include <map>
  33. #include <memory.h>
  34. #include <deque>
  35.  
  36. typedef long long LL;
  37. typedef pair<int,int> pii;
  38. typedef pair<LL,LL> pll;
  39. typedef vector<int> vi;
  40. typedef vector<LL> vl;
  41. typedef vector<vector<int>> vvi;
  42. typedef vector<vector<LL>> vvl;
  43.  
  44. #define pb push_back
  45. #define F first
  46. #define S second
  47. #define mid (LB+RB+1)/2
  48. #define mkp make_pair
  49.  
  50. //iterators
  51. #define iter(x) x.begin(),x.end()
  52. #define aiter(a,n) a,a+n
  53.  
  54. //loops
  55. #define REP(n) for (int ___=n;___--;)
  56. #define REP0(i,n) for (int i=0;i<n;++i)
  57. #define REP1(i,n) for (int i=1;i<=n;++i)
  58. #define FOR(i,b,l,k) for (int i=b;i!=l;i+=k)
  59. #define MEM(e,val) memset (e,val,sizeof(e))
  60. #define FE(i,v) for (auto i:v)
  61.  
  62. /*
  63. yungyao so weeeeeeeeeeeeeeeeeeeeeeeeeeak
  64. 8e7 so dian
  65. FHVirus so dian
  66. youou so dian
  67. KYW so dian
  68. hubert so dian
  69. jass so dian
  70. tingyu so dian
  71. panda so dian
  72. */
  73.  
  74. //IO
  75. #include <cstdio>
  76. #include <iostream>
  77. #include <fstream> //mainly for USACO, sometimes for benjidabiao
  78. #define theyRSOOOOOOOOODIAN ios_base::sync_with_stdio(false),cin.tie(0);
  79. //testing some stuff, still under construction
  80. //a lot more useful while debug
  81. inline void print(vector <int> v){
  82.     for (auto i:v)
  83.         cout << i << ' ';
  84.     cout << '\n';
  85. }
  86. inline void print(vector <int> v,char sep,char end){
  87.     for (auto i:v)
  88.         cout << i << sep;
  89.     cout << end;
  90. }
  91.  
  92. //pbds
  93. /*
  94. #include <ext/pb_ds/tree_policy.hpp>
  95. #include <ext/pb_ds/assoc_container.hpp>
  96. #include <ext/pb_ds/priority_queue.hpp>
  97. using namespace __gnu_pbds;
  98. */
  99.  
  100. //constants
  101. #include <climits>
  102. const int maxn = 1e5+100,mod = 1e9+7;
  103.  
  104. //workspace
  105.  
  106. //pow
  107. inline long long pow_(long long n,long long k){
  108.     long long val = 1;
  109.     for (;k;k>>=1){
  110.         if (k & 1)
  111.             val = val * n % mod;
  112.         n = n*n % mod;
  113.     }
  114.     return val;
  115. }
  116.  
  117. LL pow2[maxn],pre[maxn];
  118. bool arr[maxn];
  119.  
  120. bool isSame(int l1,int r1,int l2,int r2){
  121.     int const len = r1 - l1 + 1;
  122.     return (pre[r1] - (pre[l1-1] * pow2[len] % mod) + mod) % mod ==
  123.             (pre[r2] - (pre[l2-1] * pow2[len] % mod) + mod) % mod;
  124. }
  125.  
  126. inline void solve(){
  127.     int n,q;
  128.  
  129.     cin >> n >> q;
  130.     REP1(i,n){
  131.         int x;
  132.         cin >> x;
  133.  
  134.         arr[i] = pow_(x,(mod-1)>>1) == 1;
  135.         pre[i] = ((pre[i-1] << 1) + arr[i]) % mod;
  136.     }
  137.  
  138.     while (q--){
  139.         int a,b;
  140.  
  141.         cin >> a >> b;
  142.         ++a; ++b;
  143.         int LB = 0,RB = n - max(a,b);
  144.         if (arr[a] ^ arr[b]) cout << "0\n";
  145.         else{
  146.             while (LB != RB){
  147.                 if (isSame(a,a+mid,b,b+mid))
  148.                     LB = mid;
  149.                 else
  150.                     RB = mid-1;
  151.             }
  152.             cout << LB + 1 << '\n';
  153.         }
  154.     }
  155. }
  156.  
  157. signed main(){
  158.     theyRSOOOOOOOOODIAN
  159.     pow2[0] = 1;
  160.     REP1(i,int(1e5)){pow2[i] = (pow2[i-1] << 1) % mod;}
  161.     //for (int ;cin;)//use in multi-testcases and end in EOF problems
  162.     //int t,i=1;for (cin >> t;i<=t;++i)//use in multi-testcases problems
  163.     //cout << "Case #" << i << ": ",//use in Google, FB competitions
  164.     solve();//always used
  165.     return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement