Dang_Quan_10_Tin

COW ON TREE

Jul 27th, 2020
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #define task ""
  4. using namespace std;
  5. using ll = long long;
  6. using ld = long double;
  7.  
  8. const int N = 2e5 + 2;
  9. int n, q;
  10. const int Inf = 1e9;
  11. ll a[N], st[N * 4];
  12.  
  13. void upd(int id, int l, int r, int &pos, ll &val){
  14.     if(l > pos || r < pos)
  15.         return;
  16.     if(l == r && r == pos){
  17.         st[id] = val;
  18.         return;
  19.     }
  20.     upd(id << 1, l, (l + r) / 2, pos, val);
  21.     upd(id << 1 | 1, (l + r) / 2 + 1, r, pos, val);
  22.     st[id] = min(st[id << 1], st[id << 1 | 1]);
  23. }
  24.  
  25. void Read(){
  26.     cin >> n >> q;
  27.     fill_n(st, N * 4, 1e18);
  28.     for(int i = 1; i <= n; ++i)
  29.         cin >> a[i],
  30.         upd(1, 1, n, i, a[i]);
  31. }
  32.  
  33. // Thứ gọi là "CON BÒ TRÊN CÂY" đây nhé
  34.  
  35. int que(int id, int l, int r, int &pos, ll &v){
  36.     if(r < pos || st[id] > v)
  37.         return Inf;
  38.     if(l == r && r >= pos)
  39.         return st[id] <= v ? l : Inf;
  40.     int mid = (l + r) / 2;
  41.     if(mid >= pos && st[id << 1] <= v){
  42.         int ans = que(id << 1, l, mid, pos, v);
  43.         if(ans != Inf)
  44.             return ans;
  45.         return que(id << 1 | 1, mid + 1, r, pos, v);
  46.     }
  47.     return que(id << 1 | 1, mid + 1, r, pos, v);
  48. }
  49.  
  50. --- END ---
  51.  
  52. void Solve(){
  53.     while(q--){
  54.         ll v;
  55.         int l, r, now;
  56.         cin >> v >> l >> r;
  57.         do{
  58.             now = que(1, 1, n, l, v);
  59.             if(now <= r)
  60.                 v %= a[now];
  61.             //cout << "chosen: " << now << "\n";
  62.             l = now + 1;
  63.         }while(l <= r);
  64.         cout << v << "\n";
  65.     }
  66. }
  67.  
  68. main(){
  69.     ios::sync_with_stdio(0);
  70.     cin.tie(0);
  71.     cout.tie(0); if(fopen(task".INP", "r"))
  72.     freopen(task".INP", "r", stdin),
  73.     freopen(task".OUT", "w", stdout);
  74.     Read();
  75.     Solve();
  76. }
Advertisement
Add Comment
Please, Sign In to add comment