#include <bits/stdc++.h>   using namespace std;   const long long MXN = 2e5 + 10;   const long long MXS = MXN * 4;   const long long INF = 1e18;   struct Typ {     long long Sum, ValL, ValR, dpL, dpR, Maxdp, sz, Mindis, Val2L, Val2R;     pair<long long, long long> Valley;     void init() {       Mindis = INF;       Val2L = Val2R = Valley.first = Valley.second = 0;       Sum = dpL = dpR = Maxdp = ValL = ValR = sz = 0;     }   };   long long n, q, k;   long long A[MXN], Lazy[MXS];   bool Ok;   Typ seg[MXS], M;   Typ Merge(Typ a, Typ b) {     Typ now;     now.Mindis = INF;     now.Sum = a.Sum + b.Sum + abs(a.ValR - b.ValL);     now.ValL = a.ValL, now.ValR = b.ValR;     now.dpL = (a.sz > 1 ? a.dpL : (a.ValR >= b.ValL));     now.dpR = (b.sz > 1 ? b.dpR : (b.ValL >= a.ValR));     long long x = a.dpR + (a.ValR >= b.ValL);     long long y = b.dpL + (b.ValL >= a.ValR);     now.Maxdp = max(a.Maxdp, b.Maxdp);     if (a.sz > 1) now.Maxdp = max(now.Maxdp, x);     if (b.sz > 1) now.Maxdp = max(now.Maxdp, y);     if (a.sz > 1 && x == 1) {       now.Mindis =           min(now.Mindis, (b.ValL > a.ValR ? b.ValL - a.ValR : a.Val2R - a.ValR));     }     if (a.sz > 1 && x == 0) {       now.Valley = {a.Val2R - a.ValR, b.ValL - a.ValR};     }     if (b.sz > 1 && y == 1) {       now.Mindis =           min(now.Mindis, (a.ValR > b.ValL ? a.ValR - b.ValL : b.Val2L - b.ValL));     }     if (b.sz > 1 && y == 0) {       now.Valley = {a.ValR - b.ValL, b.Val2L - b.ValL};     }     if (a.Valley.first != 0) {       now.Valley = a.Valley;     }     if (b.Valley.first != 0) {       now.Valley = b.Valley;     }     now.Mindis = min(now.Mindis, a.Mindis);     now.Mindis = min(now.Mindis, b.Mindis);     if (a.sz == 1)       now.Val2L = b.ValL;     else       now.Val2L = a.Val2L;     if (b.sz == 1)       now.Val2R = a.ValR;     else       now.Val2R = b.Val2R;     now.sz = a.sz + b.sz;     return now;   }   void Build(long long id = 1, long long s = 1, long long e = n) {     seg[id].init();     seg[id].sz = (e - s + 1);     if ((e - s + 1) == 1) {       seg[id].ValL = seg[id].ValR = A[s];       return;     }     Build((id * 2), s, ((s + e) / 2)), Build((id * 2 + 1), ((s + e) / 2 + 1), e);     seg[id] = Merge(seg[(id * 2)], seg[(id * 2 + 1)]);   }   void Shift(long long id, long long s, long long e) {     if (!Lazy[id]) return;     seg[id].ValL += Lazy[id], seg[id].ValR += Lazy[id];     if ((e - s + 1) > 1) {       seg[id].Val2L += Lazy[id], seg[id].Val2R += Lazy[id];       Lazy[(id * 2)] += Lazy[id];       Lazy[(id * 2 + 1)] += Lazy[id];     }     Lazy[id] = 0;   }   void Upd(long long l, long long r, long long x, long long id = 1,            long long s = 1, long long e = n) {     Shift(id, s, e);     if (e < l || s > r) return;     if (l <= s && e <= r) {       Lazy[id] += x;       Shift(id, s, e);       return;     }     Upd(l, r, x, (id * 2), s, ((s + e) / 2)),         Upd(l, r, x, (id * 2 + 1), ((s + e) / 2 + 1), e);     seg[id] = Merge(seg[(id * 2)], seg[(id * 2 + 1)]);   }   void Calc(long long l, long long r, long long id = 1, long long s = 1,             long long e = n) {     Shift(id, s, e);     if (e < l || s > r) return;     if (l <= s && e <= r) {       if (!Ok)         M = seg[id], Ok = true;       else {         M = Merge(M, seg[id]);       }       return;     }     Calc(l, r, (id * 2), s, ((s + e) / 2)),         Calc(l, r, (id * 2 + 1), ((s + e) / 2 + 1), e);     seg[id] = Merge(seg[(id * 2)], seg[(id * 2 + 1)]);   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) cin >> A[i];     Build();     cin >> q;     while (q--) {       long long t, l, r, x, ans;       cin >> t >> l >> r >> x;       if (t == 2)         Upd(l, r, x);       else {         Ok = false;         Calc(max(l - 1, 1ll), min(r + 1, n));         if (M.Maxdp == 2) ans = 2 * x + seg[1].Sum;         if (M.Maxdp == 0) {           ans = seg[1].Sum;           ans += (M.Valley.first >= x ? -x : x - M.Valley.first * 2);           ans += (M.Valley.second >= x ? -x : x - M.Valley.second * 2);         }         if (M.Maxdp == 1) {           ans = x + (M.Mindis >= x ? -x : x - M.Mindis * 2);           if (M.Valley.first != 0) {             ans = max(ans,                       (M.Valley.first >= x ? -x : x - M.Valley.first * 2) +                           (M.Valley.second >= x ? -x : x - M.Valley.second * 2));           }           ans += seg[1].Sum;         }         cout << ans <<   n ;       }     }     return 0;   }   