#pragma GCC optimize("Ofast") #include #include #include using namespace std; using namespace __gnu_pbds; using ll = long long; using ull = unsigned long long; using pii = pair; using pll = pair; #define rep(i, n) for(int i = 0; i < (n); ++i) #define all(x) (x).begin(),(x).end() constexpr char ln = '\n'; constexpr long long MOD = 1000000007; //constexpr long long MOD = 998244353; template inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true;} return false; } template inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true;} return false; } inline int popcount(int x) {return __builtin_popcount(x);} inline int popcount(long long x) {return __builtin_popcountll(x);} void print() { cout << "\n"; } template void print(const T &x, const Args &... args) { cout << x << " "; print(args...); } //////////////////////////////////////////////////////////////////////////////////////////////////////////// const int B = 565; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N,Q; cin >> N >> Q; vector A(N); rep(i,N) cin >> A[i]; int sz = (N-1)/B+1; vector> bucket(sz); vector> S(sz); rep(i,N) { bucket[i/B][A[i]]++; S[i/B].emplace_back(A[i]); } rep(i,sz) { int cur = 0; for (auto &j : bucket[i]) { j.second += cur; cur = j.second; } int M = S[i].size(); S[i].emplace_back(0); sort(all(S[i])); rep(j,M) S[i][j+1] += S[i][j]; } while (Q--) { int t,l,r,x; cin >> t >> l >> r >> x; --l; --r; ll ans = 0; if (l/B==r/B) { for (int i = l; i <= r; i++) ans += max(A[i]-x,0); } else { for (int i = l; i/B == l/B; i++) ans += max(A[i]-x,0); for (int i = l/B+1; i < r/B; i++) { auto it = bucket[i].upper_bound(x); if (it==bucket[i].begin()) { ans += S[i][B] - ll(x)*B; } else { --it; ans += S[i][B] - S[i][it->second] - ll(x)*(B - it->second); } } for (int i = r/B*B; i <= r; i++) ans += max(A[i]-x,0); } cout << ans << ln; } }