Language: C++

template<typename T>
class SegtreeBeats {
    int n, rr;
    vector<T>mx_v, mn_v, smx_v, smn_v, sigm;
    vector<int>mx_c, mn_c;
    T mn = numeric_limits<T>::min(), mx = numeric_limits<T>::max();

public:
    SegtreeBeats(vector<T>v) { build(v); }
    void build(vector<T>v) {
        n = v.size(), rr = 1;
        while (rr < n) rr *= 2;

        mx_v.assign(2 * rr - 1, mn); mn_v.assign(2 * rr - 1, mx);
        smx_v.assign(2 * rr - 1, mn); smn_v.assign(2 * rr - 1, mx);
        mx_c.assign(2 * rr - 1, 0); mn_c.assign(2 * rr - 1, 0);
        sigm.assign(2 * rr - 1, 0);

        for (int i = rr - 1; i < 2 * rr - 1; i++) {
            if (i < rr - 1 + n) {
                mx_v[i] = mn_v[i] = sigm[i] = v[i];
                mn_c[i] = mx_c[i] = 1;
            }
        }
        for (int i = rr - 1; i >= 0; i--) proc(i);
    }

private:
    void eval(int i) {
        if (mx_v[i] < mx_v[2 * i + 1]) update_node_max(2 * i + 1, mx_v[i]);
        if (mn_v[i] > mn_v[2 * i + 1]) update_node_min(2 * i + 1, mn_v[i]);

        if (mx_v[i] < mx_v[2 * i + 2]) update_node_max(2 * i + 2, mx_v[i]);
        if (mn_v[i] > mn_v[2 * i + 2]) update_node_min(2 * i + 2, mn_v[i]);
    }
    void proc(int i) {
        if (mx_v[i * 2 + 1] < mx_v[i * 2 + 2]) {
            mx_v[i] = mx_v[i * 2 + 2];
            mx_c[i] = mx_c[i * 2 + 2];
            smx_v[i] = max(smx_v[i * 2 + 2], mx_v[2 * i + 1]);
        }
        else if (mx_v[i * 2 + 1] > mx_v[i * 2 + 2]) {
            mx_v[i] = mx_v[i * 2 + 1];
            mx_c[i] = mx_c[i * 2 + 1];
            smx_v[i] = max(smx_v[i * 2 + 1], mx_v[2 * i + 2]);
        }
        else {
            mx_v[i] = mx_v[i * 2 + 1];
            mx_c[i] = mx_c[i * 2 + 1] + mx_c[i * 2 + 2];
            smx_v[i] = max(smx_v[i * 2 + 1], smx_v[i * 2 + 2]);
        }


        if (mn_v[i * 2 + 1] < mn_v[i * 2 + 2]) {
            mn_v[i] = mn_v[i * 2 + 2];
            mn_c[i] = mn_c[i * 2 + 2];
            smn_v[i] = max(smn_v[i * 2 + 2], mn_v[2 * i + 1]);
        }
        else if (mn_v[i * 2 + 1] > mn_v[i * 2 + 2]) {
            mn_v[i] = mn_v[i * 2 + 1];
            mn_c[i] = mn_c[i * 2 + 1];
            smn_v[i] = max(smn_v[i * 2 + 1], mn_v[2 * i + 2]);
        }
        else {
            mn_v[i] = mn_v[i * 2 + 1];
            mn_c[i] = mn_c[i * 2 + 1] + mn_c[i * 2 + 2];
            smx_v[i] = max(smx_v[i * 2 + 1], smx_v[i * 2 + 2]);
        }
    }
    //change maximam by chmin
    void update_node_max(int i, T x) {
        sigm[i] += (x - mx_v[i]) * mx_c[i];

        if (mx_v[i] == mn_v[i]) mx_v[i] = mn_v[i] = x;
        else if (mx_v[i] == smn_v[i]) mx_v = smn_v[i] = x;
        else mx_v[i] = x;
    }
    void update_node_min(int i, T x) {
        sigm[i] += (x - mn_v[i]) * mn_c[i];

        if (mn_v[i] == mx_v[i]) mn_v[i] = mx_v[i] = x;
        else if (mn_v[i] == smx_v[i]) mn_v = smx_v[i] = x;
        else mn_v[i] = x;
    }

    void chmin(int i, int a, int b, int l, int r, T x) {
        if (b <= l || r <= a || mx_v[i] <= x) return;
        if (a <= l && r <= b && smx_v[i] < x) {
            update_node_max(i, x); return;
        }

        eval(i);
        chmin(i * 2 + 1, a, b, l, (l + r) / 2, x);
        chmin(i * 2 + 2, a, b, (l + r) / 2, r, x);
        proc(i);
    }
    void chmax(int i, int a, int b, int l, int r, T x) {
        if (b <= l || r <= a || mn_v[i] >= x) return;
        if (a <= l && r <= b && smn_v[i] > x) {
            update_node_min(i, x); return;
        }

        eval(i);
        chmax(i * 2 + 1, a, b, l, (l + r) / 2, x);
        chmax(i * 2 + 2, a, b, (l + r) / 2, r, x);
        proc(i);
    }

};

Compiler output:
a.cc:4:5: error: 'vector' does not name a type
    4 |     vector<T>mx_v, mn_v, smx_v, smn_v, sigm;
      |     ^~~~~~
a.cc:5:5: error: 'vector' does not name a type
    5 |     vector<int>mx_c, mn_c;
      |     ^~~~~~
a.cc:9:24: error: expected ')' before '<' token
    9 |     SegtreeBeats(vector<T>v) { build(v); }
      |                 ~      ^
      |                        )
a.cc:10:16: error: 'vector' has not been declared
   10 |     void build(vector<T>v) {
      |                ^~~~~~
a.cc:10:22: error: expected ',' or '...' before '<' token
   10 |     void build(vector<T>v) {
      |                      ^
a.cc:6:12: error: 'numeric_limits' was not declared in this scope
    6 |     T mn = numeric_limits<T>::min(), mx = numeric_limits<T>::max();
      |            ^~~~~~~~~~~~~~
a.cc:6:28: error: expected primary-expression before '>' token
    6 |     T mn = numeric_limits<T>::min(), mx = numeric_limits<T>::max();
      |                            ^
a.cc:6:31: error: '::min' has not been declared; did you mean 'mn'?
    6 |     T mn = numeric_limits<T>::min(), mx = numeric_limits<T>::max();
      |                               ^~~
      |                               mn
a.cc:6:43: error: 'numeric_limits' was not declared in this scope
    6 |     T mn = numeric_limits<T>::min(), mx = numeric_limits<T>::max();
      |                                           ^~~~~~~~~~~~~~
a.cc:6:59: error: expected primary-expression before '>' token
    6 |     T mn = numeric_limits<T>::min(), mx = numeric_limits<T>::max();
      |                                                           ^
a.cc:6:62: error: '::max' has not been declared; did you mean 'mx'?
    6 |     T mn = numeric_limits<T>::min(), mx = numeric_limits<T>::max();
      |                                                              ^~~
      |                                                              mx
a.cc: In member function 'void SegtreeBeats<T>::build(int)':
a.cc:11:13: error: 'v' was not declared in this scope
   11 |         n = v.size(), rr = 1;
      |             ^
a.cc:14:9: error: 'mx_v' was not declared in this scope; did you mean 'mx'?
   14 |         mx_v.assign(2 * rr - 1, mn); mn_v.assign(2 * rr - 1, mx);
      |         ^~~~
      |         mx
a.cc:14:38: error: 'mn_v' was not declared in this scope; did you mean 'mn'?
   14 |         mx_v.assign(2 * rr - 1, mn); mn_v.assign(2 * rr - 1, mx);
      |                                      ^~~~
      |                                      mn
a.cc:15:9: error: 'smx_v' was not declared in this scope
   15 |         smx_v.assign(2 * rr - 1, mn); smn_v.assign(2 * rr - 1, mx);
      |         ^~~~~
a.cc:15:39: error: 'smn_v' was not declared in this scope
   15 |         smx_v.assign(2 * rr - 1, mn); smn_v.assign(2 * rr - 1, mx);
      |                                       ^~~~~
a.cc:16:9: error: 'mx_c' was not declared in this scope; did you mean 'mx'?
   16 |         mx_c.assign(2 * rr - 1, 0); mn_c.assign(2 * rr - 1, 0);
      |         ^~~~
      |         mx
a.cc:16:37: error: 'mn_c' was not declared in this scope; did you mean 'mn'?
   16 |         mx_c.assign(2 * rr - 1, 0); mn_c.assign(2 * rr - 1, 0);
      |                                     ^~~~
      |                                     mn
a.cc:17:9: error: 'sigm' was not declared in this scope
   17 |         sigm.assign(2 * rr - 1, 0);
      |         ^~~~
a.cc: In member function 'void SegtreeBeats<T>::eval(int)':
a.cc:30:13: error: 'mx_v' was not declared in this scope; did you mean 'mx'?
   30 |         if (mx_v[i] < mx_v[2 * i + 1]) update_node_max(2 * i + 1, mx_v[i]);
      |             ^~~~
      |             mx
a.cc:31:13: error: 'mn_v' was not declared in this scope; did you mean 'mn'?
   31 |         if (mn_v[i] > mn_v[2 * i + 1]) update_node_min(2 * i + 1, mn_v[i]);
      |             ^~~~
      |             mn
a.cc:33:13: error: 'mx_v' was not declared in this scope; did you mean 'mx'?
   33 |         if (mx_v[i] < mx_v[2 * i + 2]) update_node_max(2 * i + 2, mx_v[i]);
      |             ^~~~
      |             mx
a.cc:34:13: error: 'mn_v' was not declared in this scope; did you mean 'mn'?
   34 |         if (mn_v[i] > mn_v[2 * i + 2]) update_node_min(2 * i + 2, mn_v[i]);
      |             ^~~~
      |             mn
a.cc: In member function 'void SegtreeBeats<T>::proc(int)':
a.cc:37:13: error: 'mx_v' was not declared in this scope; did you mean 'mx'?
   37 |         if (mx_v[i * 2 + 1] < mx_v[i * 2 + 2]) {
      |             ^~~~
      |             mx
a.cc:39:13: error: 'mx_c' was not declared in this scope; did you mean 'mx'?
   39 |             mx_c[i] = mx_c[i * 2 + 2];
      |             ^~~~
      |             mx
a.cc:40:13: error: 'smx_v' was not declared in this scope
   40 |             smx_v[i] = max(smx_v[i * 2 + 2], mx_v[2 * i + 1]);
      |             ^~~~~
a.cc:40:24: error: there are no arguments to 'max' that depend on a template parameter, so a declaration of 'max' must be available [-fpermissive]
   40 |             smx_v[i] = max(smx_v[i * 2 + 2], mx_v[2 * i + 1]);
      |                        ^~~
a.cc:40:24: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
a.cc:44:13: error: 'mx_c' was not declared in this scope; did you mean 'mx'?
   44 |             mx_c[i] = mx_c[i * 2 + 1];
      |             ^~~~
      |             mx
a.cc:45:13: error: 'smx_v' was not declared in this scope
   45 |             smx_v[i] = max(smx_v[i * 2 + 1], mx_v[2 * i + 2]);
      |             ^~~~~
a.cc:45:24: error: there are no arguments to 'max' that depend on a template parameter, so a declaration of 'max' must be available [-fpermissive]
   45 |             smx_v[i] = max(smx_v[i * 2 + 1], mx_v[2 * i + 2]);
      |                        ^~~
a.cc:49:13: error: 'mx_c' was not declared in this scope; did you mean 'mx'?
   49 |             mx_c[i] = mx_c[i * 2 + 1] + mx_c[i * 2 + 2];
      |             ^~~~
      |             mx
a.cc:50:13: error: 'smx_v' was not declared in this scope
   50 |             smx_v[i] = max(smx_v[i * 2 + 1], smx_v[i * 2 + 2]);
      |             ^~~~~
a.cc:50:24: error: there are no arguments to 'max' that depend on a template parameter, so a declaration of 'max' must be available [-fpermissive]
   50 |             smx_v[i] = max(smx_v[i * 2 + 1], smx_v[i * 2 + 2]);
      |                        ^~~
a.cc:54:13: error: 'mn_v' was not declared in this scope; did you mean 'mn'?
   54 |         if (mn_v[i * 2 + 1] < mn_v[i * 2 + 2]) {
      |             ^~~~
      |             mn
a.cc:56:13: error: 'mn_c' was not declared in this scope; did you mean 'mn'?
   56 |             mn_c[i] = mn_c[i * 2 + 2];
      |             ^~~~
      |             mn
a.cc:57:13: error: 'smn_v' was not declared in this scope
   57 |             smn_v[i] = max(smn_v[i * 2 + 2], mn_v[2 * i + 1]);
      |             ^~~~~
a.cc:57:24: error: there are no arguments to 'max' that depend on a template parameter, so a declaration of 'max' must be available [-fpermissive]
   57 |             smn_v[i] = max(smn_v[i * 2 + 2], mn_v[2 * i + 1]);
      |                        ^~~
a.cc:61:13: error: 'mn_c' was not declared in this scope; did you mean 'mn'?
   61 |             mn_c[i] = mn_c[i * 2 + 1];
      |             ^~~~
      |             mn
a.cc:62:13: error: 'smn_v' was not declared in this scope
   62 |             smn_v[i] = max(smn_v[i * 2 + 1], mn_v[2 * i + 2]);
      |             ^~~~~
a.cc:62:24: error: there are no arguments to 'max' that depend on a template parameter, so a declaration of 'max' must be available [-fpermissive]
   62 |             smn_v[i] = max(smn_v[i * 2 + 1], mn_v[2 * i + 2]);
      |                        ^~~
a.cc:66:13: error: 'mn_c' was not declared in this scope; did you mean 'mn'?
   66 |             mn_c[i] = mn_c[i * 2 + 1] + mn_c[i * 2 + 2];
      |             ^~~~
      |             mn
a.cc:67:13: error: 'smx_v' was not declared in this scope
   67 |             smx_v[i] = max(smx_v[i * 2 + 1], smx_v[i * 2 + 2]);
      |             ^~~~~
a.cc:67:24: error: there are no arguments to 'max' that depend on a template parameter, so a declaration of 'max' must be available [-fpermissive]
   67 |             smx_v[i] = max(smx_v[i * 2 + 1], smx_v[i * 2 + 2]);
      |                        ^~~
a.cc: In member function 'void SegtreeBeats<T>::update_node_max(int, T)':
a.cc:72:9: error: 'sigm' was not declared in this scope
   72 |         sigm[i] += (x - mx_v[i]) * mx_c[i];
      |         ^~~~
a.cc:72:25: error: 'mx_v' was not declared in this scope; did you mean 'mx'?
   72 |         sigm[i] += (x - mx_v[i]) * mx_c[i];
      |                         ^~~~
      |                         mx
a.cc:72:36: error: 'mx_c' was not declared in this scope; did you mean 'mx'?
   72 |         sigm[i] += (x - mx_v[i]) * mx_c[i];
      |                                    ^~~~
      |                                    mx
a.cc:74:24: error: 'mn_v' was not declared in this scope; did you mean 'mn'?
   74 |         if (mx_v[i] == mn_v[i]) mx_v[i] = mn_v[i] = x;
      |                        ^~~~
      |                        mn
a.cc:75:29: error: 'smn_v' was not declared in this scope
   75 |         else if (mx_v[i] == smn_v[i]) mx_v = smn_v[i] = x;
      |                             ^~~~~
a.cc: In member function 'void SegtreeBeats<T>::update_node_min(int, T)':
a.cc:79:9: error: 'sigm' was not declared in this scope
   79 |         sigm[i] += (x - mn_v[i]) * mn_c[i];
      |         ^~~~
a.cc:79:25: error: 'mn_v' was not declared in this scope; did you mean 'mn'?
   79 |         sigm[i] += (x - mn_v[i]) * mn_c[i];
      |                         ^~~~
      |                         mn
a.cc:79:36: error: 'mn_c' was not declared in this scope; did you mean 'mn'?
   79 |         sigm[i] += (x - mn_v[i]) * mn_c[i];
      |                                    ^~~~
      |                                    mn
a.cc:81:24: error: 'mx_v' was not declared in this scope; did you mean 'mx'?
   81 |         if (mn_v[i] == mx_v[i]) mn_v[i] = mx_v[i] = x;
      |                        ^~~~
      |                        mx
a.cc:82:29