trafik

DO_min

Oct 29th, 2021
877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <deque>
  5. #include <algorithm>
  6. #define ll long long
  7.  
  8. using namespace std;
  9.  
  10. const ll inf = 1e9;
  11.  
  12. vector<int> A;
  13. int n = 1e9;
  14. vector<int> t(2 * n, inf);
  15.  
  16. void build() {
  17.   n = 1; // размер массива в дереве отрезков
  18.   while (n < A.size()) n *= 2;
  19.   for (int i = n; i < (n + A.size()); ++i) {
  20.     t[i] = A[i - n];
  21.   }
  22.   for (int i = (n - 1); i <= 1; --i) {
  23.     t[i] = min(t[i * 2], t[i * 2 + 1]);
  24.   }
  25. }
  26.  
  27. void set(int ind, int value) {
  28.   ind += n;
  29.   t[ind] = value;
  30.   ind /= 2;
  31.   while (ind >= 1) {
  32.     t[ind] = min(t[ind * 2], t[ind * 2 + 1]);
  33.     ind /= 2;
  34.   }
  35. }
  36.  
  37. int getsum(int l, int r) {
  38.   ll mins = inf;
  39.   l += n;
  40.   r += n;
  41.   int sum = 0;
  42.   for (; l <= r; l /= 2, r /= 2) {
  43.     if (l % 2 == 1) {
  44.       mins = min(mins, t[l]);
  45.       sum += t[l];
  46.       l++;
  47.     }
  48.     if (r % 2 == 0) {
  49.       mins = min(mins, t[r]);
  50.       sum += t[r];
  51.       r--;
  52.     }
  53.   }
  54.  
  55.   return sum;
  56. }
  57.  
  58. int main() {
  59.   ios::sync_with_stdio(0);
  60.   cin.tie(0);
  61.  
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment