//Luogu P3372 树状数组 区间加 + 区间和#include<bits/stdc++.h>#define N 100005#define int long long#define lowbit(x) ((x)&(-x))usingnamespace std;int n, m, op;int x, y, k;int a[N], ta[N], tb[N];voidadda(int x,int k){for(int i = x; i <= n; i +=lowbit(i)){
ta[i]+= k;}}voidaddb(int x,int k){for(int i = x; i <= n; i +=lowbit(i)){
tb[i]+= k;}}intquerya(int x){int res =0;for(int i = x; i; i -=lowbit(i)){
res += ta[i];}return res;}intqueryb(int x){int res =0;for(int i = x; i; i -=lowbit(i)){
res += tb[i];}return res;}signedmain(){scanf("%lld%lld",&n,&m);for(int i =1; i <= n;++i){scanf("%lld",&a[i]);}for(int i =1; i <= n;++i){adda(i, a[i]- a[i -1]);addb(i,(a[i]- a[i -1])*(i -1));}for(int i =1; i <= m;++i){scanf("%lld",&op);if(op ==1){scanf("%lld%lld%lld",&x,&y,&k);adda(x, k);adda(y +1,-k);addb(x, k *(x -1));addb(y +1,-k * y);}elseif(op ==2){scanf("%lld%lld",&x,&y);printf("%lld\n",(y *querya(y)-(x -1)*querya(x -1)-(queryb(y)-queryb(x -1))));}}return0;}
#include<bits/stdc++.h>#define N 500005#define ll long long#define lowbit(x) ((x)&(-x))usingnamespace std;int n, rank[N];
ll ans;struct Node
{int val, id;} a[N];boolcmp(Node x, Node y){if(x.val == y.val)return x.id < y.id;return x.val < y.val;}int tree[N];voidadd(int x,int k){for(int i = x; i <= n; i +=lowbit(i)){
tree[i]+= k;}}intquery(int x){int res =0;for(int i = x; i; i -=lowbit(i)){
res += tree[i];}return res;}intmain(){scanf("%d",&n);for(int i =1; i <= n;++i){scanf("%d",&a[i].val);
a[i].id = i;}sort(a +1, a + n +1, cmp);for(int i =1; i <= n;++i){
rank[a[i].id]= i;}for(int i = n; i >=1;--i){
ans +=query(rank[i]);add(rank[i],1);}printf("%lld\n", ans);return0;}