Advertisement
yungyao

tioj 1872

Feb 12th, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. using namespace std;
  2. #include <vector>
  3. #include <queue>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <utility>
  7. #include <bitset>
  8. #include <memory.h>
  9. #include <set>
  10. #include <string>
  11. #include <stack>
  12. #include <iomanip>
  13. #include <map>
  14.  
  15. #define pb push_back
  16. #define pii pair<int,int>
  17. #define F first
  18. #define S second
  19. #define LL long long
  20.  
  21. /*
  22. 8e7 so dian
  23. FHVirus so dian
  24. youou so dian
  25. KYW so dian
  26. hubert so dian
  27. jass so dian
  28. tingyu so dian
  29. */
  30.  
  31. //IO
  32. #include <iostream>
  33. #define theyRSOOOOOOOOODIAN ios_base::sync_with_stdio(false),cin.tie(0);
  34. #define endl '\n'
  35.  
  36. //workspace
  37. LL seg[4004000];
  38. #define mid (LB+RB)/2
  39. const LL mod = 1e9+7;
  40.  
  41. inline LL modInver(const int x){
  42.     LL val = 1,multi = x;
  43.     int k = mod - 2;
  44.     while (k){
  45.         if (k & 1){
  46.             val *= multi;
  47.             val %= mod;
  48.         }
  49.         multi *= multi;
  50.         multi %= mod;
  51.  
  52.         k >>= 1;
  53.     }
  54.     return val;
  55. }
  56.  
  57. LL gcd(LL a,LL b){
  58.     if (a % b)
  59.         return gcd(b,a%b);
  60.     return b;
  61. }
  62.  
  63. inline LL lcm(int a,int b){
  64.     return (a * b * modInver(gcd(a,b))) % mod;
  65. }
  66.  
  67. void modify (int i,int val,int cur,int LB,int RB){
  68.     if (LB == RB){
  69.         seg[cur] = val;
  70.         return;
  71.     }
  72.  
  73.     if (i <= mid)
  74.         modify (i,val,cur*2,LB,mid);
  75.     else
  76.         modify (i,val,cur*2+1,mid+1,RB);
  77.    
  78.     if (seg[cur*2] && seg[cur*2+1]){
  79.         seg[cur] = lcm(seg[cur*2],seg[cur*2+1]);
  80.     }
  81. }
  82.  
  83. LL query (int l,int r,int cur,int LB,int RB){
  84.     if (l == LB && r == RB)
  85.         return seg[cur];
  86.    
  87.     if (r <= mid)
  88.         return query(l,r,cur*2,LB,mid);
  89.     else if (l > mid)
  90.         return query(l,r,cur*2+1,mid+1,RB);
  91.     else
  92.         return lcm(query(l,mid,cur*2,LB,mid),query(mid+1,r,cur*2+1,mid+1,RB));
  93. }
  94.  
  95. int main(){
  96.     theyRSOOOOOOOOODIAN
  97.     int n,q;
  98.  
  99.     cin >> n >> q;
  100.     for (int i=1;i<=n;++i){
  101.         int x;
  102.  
  103.         cin >> x;
  104.         modify (i,x,1,1,n);
  105.     }
  106.  
  107.     while (q--){
  108.         int l,r;
  109.  
  110.         cin >> l >> r;
  111.         cout << query (l,r,1,1,n) << '\n';
  112.     }
  113.  
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement