Advertisement
esraa_syam

segtree3

Jul 9th, 2023
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.95 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define nl "\n"
  3. #define fi first
  4. #define se second
  5. #define pi 3.14159
  6. #define ll long long
  7. #define odd(a)  (a&1)
  8. #define even(a)  !(a&1)
  9. #define Mod 1'000'000'007
  10. #define INF 2'000'000'000
  11. #define sz(x) int(x.size())
  12. #define charToInt(s) (s - '0')
  13. #define ull unsigned long long
  14. #define number_line iota(all(vec) , 1)
  15. #define all(s) s.begin(), s.end()
  16. #define rall(v) v.rbegin() , v.rend()
  17. #define fixed(n) fixed << setprecision(n)
  18. #define Num_of_Digits(n) ((int)log10(n) + 1)
  19. #define to_decimal(bin) stoll(bin, nullptr, 2)
  20. #define Ceil(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  21. #define Floor(n, m) (((n) / (m)) - ((n) % (m) ? 0 : 1))
  22. #define Upper(s) transform(all(s), s.begin(), ::toupper);
  23. #define Lower(s) transform(all(s), s.begin(), ::tolower);
  24. #define cout_map(mp) for(auto& [f, s] : mp) cout << f << "  " << s << "\n";
  25. //  ----- bits-------
  26. #define pcnt(n) __builtin_popcount(n)  
  27. #define pcntll(n) __builtin_popcountll(n)
  28. #define clz(n) __builtin_clz(n)    // <---100
  29. #define clzll(n) __builtin_clzll(n)
  30. #define ctz(n) __builtin_ctz(n)  // 0001---->
  31. #define ctzll(n) __builtin_ctzll(n)
  32.  
  33. using namespace std;
  34.  
  35. template < typename T = int > istream& operator >> (istream &in, vector < T > & v){
  36.     for(auto & x : v) in >> x;
  37.     return in;
  38. }
  39.  
  40. template < typename T = int > ostream& operator << (ostream &out, const vector < T > & v){
  41.     for(const T & x : v) out << x << nl;
  42.     return out;
  43. }
  44.  
  45. void esraa()
  46. {
  47.    // freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  48.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  49. #ifndef ONLINE_JUDGE
  50.     freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  51. #endif
  52. }
  53. struct segtree{
  54.     int size;
  55.     vector < ll > Min , cnt_min;
  56.  
  57.     void init(int n){
  58.         size = 1;
  59.         while(size < n) size *= 2;
  60.         Min.assign(2 * size, 1e9);
  61.         cnt_min.assign(2 * size, 0);
  62.     }
  63.  
  64.     void build(vector < ll > &a, int x, int lx, int rx){
  65.         if(rx - lx == 1){
  66.             if(lx < sz(a)){
  67.                 Min[x] = a[lx];
  68.                 cnt_min[x] = 1;
  69.             }
  70.             return;
  71.         }
  72.         int m = (lx + rx) / 2;
  73.         build(a, 2 * x + 1, lx, m);
  74.         build(a, 2 * x + 2, m, rx);
  75.         Min[x] = min(Min[2 * x + 1], Min[2 * x + 2]);
  76.         if(Min[2 * x + 1] == Min[2 * x + 2]){
  77.             cnt_min[x] = cnt_min[2 * x + 1] + cnt_min[2 * x + 2];
  78.         }else if(Min[2 * x + 1] < Min[2 * x + 2]){
  79.             cnt_min[x] = cnt_min[2 * x + 1];
  80.         }else{
  81.             cnt_min[x] = cnt_min[2 * x + 2];
  82.         }
  83.     }
  84.  
  85.     void build(vector < ll > &a){
  86.         build(a, 0, 0, size);
  87.     }
  88.  
  89.     void set(int i, int v, int x, int lx, int rx){
  90.         if(rx - lx == 1){
  91.             Min[x] = v;
  92.             cnt_min[x] = 1;
  93.             return;
  94.         }
  95.         int m = (lx + rx) / 2;
  96.         if(i < m){
  97.             set(i, v, 2 * x + 1, lx, m);
  98.         }else{
  99.             set(i, v, 2 * x + 2, m, rx);
  100.         }
  101.         Min[x] = min(Min[2 * x + 1], Min[2 * x + 2]);
  102.         if(Min[2 * x + 1] == Min[2 * x + 2]){
  103.             cnt_min[x] = cnt_min[2 * x + 1] + cnt_min[2 * x + 2];
  104.         }else if(Min[2 * x + 1] < Min[2 * x + 2]){
  105.             cnt_min[x] = cnt_min[2 * x + 1];
  106.         }else{
  107.             cnt_min[x] = cnt_min[2 * x + 2];
  108.         }
  109.     }
  110.  
  111.     void set(int i, int v){
  112.         set(i, v, 0, 0, size);
  113.     }
  114.  
  115.     ll mini(int l, int r, int x, int lx, int rx){
  116.         if(lx >= r or l >= rx) return 1e9;
  117.         if(lx >= l and rx <= r) return Min[x];
  118.         int m = (lx + rx) / 2;
  119.         ll s1 = mini(l, r, 2 * x + 1, lx, m);
  120.         ll s2 = mini(l, r, 2 * x + 2, m, rx);
  121.         return min(s1, s2);
  122.     }
  123.  
  124.     ll mini(int l, int r){
  125.         return mini(l, r, 0, 0, size);
  126.     }
  127.  
  128.     ll cnt(int l, int r, int x, int lx, int rx){
  129.         if(lx >= r or l >= rx) return 0;
  130.         if(lx >= l and rx <= r) return cnt_min[x];
  131.         int m = (lx + rx) / 2;
  132.         ll s1 = cnt(l, r, 2 * x + 1, lx, m);
  133.         ll s2 = cnt(l, r, 2 * x + 2, m, rx);
  134.         if(mini(l, r, 2 * x + 1, lx, m) == mini(l, r, 2 * x + 2, m, rx)){
  135.             return s1 + s2;
  136.         }else if(mini(l, r, 2 * x + 1, lx, m) < mini(l, r, 2 * x + 2, m, rx)){
  137.             return s1;
  138.         }else{
  139.             return s2;
  140.         }
  141.     }
  142.  
  143.     ll cnt(int l, int r){
  144.         return cnt(l, r, 0, 0, size);
  145.     }
  146.  
  147. };
  148. void solve(){
  149.     int n, m;
  150.     cin >> n >> m;
  151.     segtree st;
  152.     st.init(n);
  153.     vector < ll > a(n);
  154.     cin >> a;
  155.     st.build(a);
  156.     while(m--){
  157.         int op;
  158.         cin >> op;
  159.         if(op == 1){
  160.             int i, v;
  161.             cin >> i >> v;
  162.             st.set(i, v);
  163.         }else{
  164.             int l, r;
  165.             cin >> l >> r;
  166.             cout << st.mini(l, r) << " " << st.cnt(l, r) << nl;
  167.         }
  168.     }
  169.  
  170. }
  171. int main()
  172. {
  173.     esraa();
  174.     int t = 1;
  175.    // cin >> t;
  176.     //cin.ignore();
  177.     while(t--)
  178.       solve();
  179.     return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement