Advertisement
paulomiranda98

Pizzeria Queries

Apr 27th, 2021
1,062
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define F first
  3. #define S second
  4. #define all(x) x.begin(),x.end()
  5. #define endl '\n'
  6.  
  7. using namespace std;
  8.  
  9. using ll = long long;
  10. using pii = pair<int, int>;
  11. const int INF = 0x3f3f3f3f;
  12. const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
  13. const int MOD = 1000000007;
  14. const int dx[] = { 0, 0, -1, 1, 1, -1,  1, -1};
  15. const int dy[] = {-1, 1,  0, 0, 1, -1, -1,  1};
  16.  
  17. class SegTreeIterative{
  18. private:
  19.   typedef int Node;
  20.   Node neutral = INF;
  21.   vector<Node> st;
  22.   int n;
  23.   inline Node join(Node a, Node b){
  24.     return min(a, b);
  25.   }
  26. public:
  27.   SegTreeIterative(int sz){
  28.     for (n = 1; n < sz; n <<= 1);
  29.     st.assign(n << 1, neutral);
  30.   }
  31.   //0-indexed
  32.   void update(int i, Node x){
  33.     st[i += n] = x;
  34.     for (i >>= 1; i; i >>= 1)
  35.       st[i] = join(st[i << 1], st[(i << 1) + 1]);
  36.   }
  37.   //0-indexed [l, r]
  38.   Node query(int l, int r){
  39.     Node ansL = neutral, ansR = neutral;
  40.     for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1){
  41.       if (l & 1)
  42.         ansL = join(ansL, st[l++]);
  43.       if (r & 1)
  44.         ansR = join(st[--r], ansR);
  45.     }
  46.     return join(ansL, ansR);
  47.   }
  48. };
  49.  
  50. int main() {
  51.   ios_base::sync_with_stdio(false); cin.tie(NULL);
  52.  
  53.   int n, q;
  54.   cin >> n >> q;
  55.   SegTreeIterative left(n), right(n);
  56.   for(int i=0; i<n; i++){
  57.     int x;
  58.     cin >> x;
  59.     left.update(i, x - i);
  60.     right.update(i, x + i);
  61.   }
  62.   for(int i=1; i<=q; i++){
  63.     int op;
  64.     cin >> op;
  65.     if(op == 1){
  66.       int k, x;
  67.       cin >> k >> x; k--;
  68.       left.update(k, x - k);
  69.       right.update(k, x + k);
  70.     }else{
  71.       int k;
  72.       cin >> k; k--;
  73.       int a = left.query(0, k) + k; // x - i + k = x + | k - i |
  74.       int b = right.query(k, n-1) - k; // x + i - k = x + | i - k |
  75.       cout << min(a, b) << endl;
  76.     }
  77.   }
  78.   return 0;
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement