func_code_string: #include <bits/stdc++.h>   using namespace std;   template <typename T>   int size(T &a) {     return (int)a.size();   }   template <typename T>   T sqr(T a) {     return a * a;   }   const int INF = int(1e9);   struct pt {     int cnt;     long long sumr;     long long suml;     long long f;     int pr;     int x;     int lefm;     int rim;     pt *L, *R;   };   const int MAXN = int(1e5);   pt buf[MAXN * 2];   int sz = 0;   pt *init(int x) {     buf[sz].cnt = 1;     buf[sz].L = buf[sz].R = NULL;     buf[sz].x = x;     buf[sz].pr = (rand() << 20) + rand();     buf[sz].suml = 0;     buf[sz].sumr = 0;     buf[sz].f = 0;     buf[sz].lefm = buf[sz].rim = x;     return &buf[sz++];   }   int getcnt(pt *p) { return p == NULL ? 0 : p->cnt; }   int getlefm(pt *p) { return p == NULL ? 2 * INF : p->lefm; }   int getrim(pt *p) { return p == NULL ? -2 * INF : p->rim; }   long long getf(pt *p) { return p == NULL ? 0 : p->f; }   long long getsuml(pt *p) { return p == NULL ? 0 : p->suml; }   long long getsumr(pt *p) { return p == NULL ? 0 : p->sumr; }   void update(pt *&p) {     if (p == NULL) return;     p->cnt = getcnt(p->L) + getcnt(p->R) + 1;     p->lefm = min(getlefm(p->L), p->x);     p->rim = max(getrim(p->R), p->x);     int n1 = getcnt(p->L);     int n2 = getcnt(p->R);     p->f = getf(p->L) + getf(p->R);     p->f += getsumr(p->L) + (long long)n1 * (p->x - getrim(p->L));     p->f += getsuml(p->R) + (long long)n2 * (getlefm(p->R) - p->x);     p->f += (long long)n1 * n2 * ((p->x - getrim(p->L)) + (getlefm(p->R) - p->x));     p->f += getsumr(p->L) * n2 + getsuml(p->R) * n1;     p->suml = getsuml(p->L) + (p->x - p->lefm) + getsuml(p->R) +               (long long)(n2 ? (getlefm(p->R) - p->lefm) : 0) * n2;     p->sumr = getsumr(p->R) + (p->rim - p->x) + getsumr(p->L) +               (long long)(n1 ? (p->rim - getrim(p->L)) : 0) * n1;   }   void merge(pt *a, pt *b, pt *&p) {     if (a == NULL) {       p = b;     } else if (b == NULL) {       p = a;     } else if (a->pr < b->pr) {       merge(a, b->L, b->L);       p = b;       update(p);     } else {       merge(a->R, b, a->R);       p = a;       update(p);     }   }   void split(pt *t, pt *&a, pt *&b, int by) {     if (t == NULL) {       a = b = NULL;     } else {       if (t->x <= by) {         split(t->R, t->R, b, by);         a = t;         update(a);       } else {         split(t->L, a, t->L, by);         b = t;         update(b);       }     }   }   int curx[MAXN];   void remove(pt *&t, int x) {     pt *p, *q;     pt *a, *b;     split(t, p, q, x);     split(p, a, b, x - 1);     merge(a, q, t);   }   void insert(pt *&t, pt *i) {     pt *p, *q;     split(t, p, q, i->x);     merge(p, i, t);     merge(t, q, t);   }   long long query(pt *&t, int l, int r) {     pt *p, *q, *a, *b;     split(t, p, q, r);     split(p, a, b, l - 1);     long long res = getf(b);     merge(a, b, t);     merge(t, q, t);     return res;   }   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     for (int i = (0); i < (n); ++i) cin >> curx[i];     pt *root = NULL;     for (int i = (0); i < (n); ++i) {       insert(root, init(curx[i]));     }     int q;     cin >> q;     for (int iter = (0); iter < (q); ++iter) {       int t;       cin >> t;       if (t == 1) {         int p, d;         cin >> p >> d;         --p;         remove(root, curx[p]);         curx[p] += d;         insert(root, init(curx[p]));       } else {         int l, r;         cin >> l >> r;         cout << query(root, l, r) <<   n ;       }     }     return 0;   }   