#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

struct SegTree {
	struct Node {
		int min;
		int mincnt;
		int min2;
		int64_t sum;
		int chmax;
		int add;
		int l;
		int r;
		Node operator * (const Node &rhs) {
			Node res;
			if (min < rhs.min) res.min = min, res.mincnt = mincnt, res.min2 = std::min(rhs.min, min2);
			else if (min > rhs.min) res.min = rhs.min, res.mincnt = rhs.mincnt, res.min2 = std::min(min, rhs.min2);
			else res.min = min, res.mincnt = mincnt + rhs.mincnt, res.min2 = std::min(min2, rhs.min2);
			res.sum = sum + rhs.sum;
			res.chmax = -1;
			res.add = 0;
			res.l = l;
			res.r = rhs.r;
			return res;
		}
		Node () {
			min = 1000000000;
			mincnt = 0;
			min2 = 1000000000;
			sum = 0;
			chmax = -1;
			add = 0;
		}
	};
	std::vector<Node> data;
	int n;
	SegTree (const std::vector<int> &a) {
		for (n = 1; n < (int) a.size(); n <<= 1);
		data.resize(n << 1);
		for (int i = 0; i < (int) a.size(); i++) {
			data[i + n].min = a[i];
			data[i + n].mincnt = 1;
			data[i + n].sum = a[i];
		}
		for (int i = 0; i < n; i++) data[i + n].l = i, data[i + n].r = i + 1;
		for (int i = n; --i; ) data[i] = data[i << 1] * data[i << 1 | 1];
	}
	void flush(int i) {
		Node &cur = data[i];
		if (cur.add) {
			cur.min += cur.add;
			cur.min2 += cur.add;
			cur.sum += (int64_t) (cur.r - cur.l) * cur.add;
			
			if (i < n) {
				data[i << 1].add += cur.add;
				if (data[i << 1].chmax != -1) data[i << 1].chmax += cur.add;
				data[i << 1 | 1].add += cur.add;
				if (data[i << 1 | 1].chmax != -1) data[i << 1 | 1].chmax += cur.add;
			}
			
			cur.add = 0;
		}
		if (cur.chmax != -1) {
			if (cur.chmax >= cur.min2) {
				data[i << 1].chmax = std::max(data[i << 1].chmax, cur.chmax);
				data[i << 1 | 1].chmax = std::max(data[i << 1 | 1].chmax, cur.chmax);
				flush(i << 1);
				flush(i << 1 | 1);
				data[i] = data[i << 1] * data[i << 1 | 1];
			} else if (cur.chmax > cur.min) {
				cur.sum += (int64_t) (cur.chmax - cur.min) * cur.mincnt;
				cur.min = cur.chmax;
				if (i < n) {
					data[i << 1].chmax = std::max(data[i << 1].chmax, cur.chmax);
					data[i << 1 | 1].chmax = std::max(data[i << 1 | 1].chmax, cur.chmax);
				}
			}
			cur.chmax = -1;
		}
	}
	void add(int l, int r, int val, int node_ = 1) {
		Node &node = data[node_];
		flush(node_);
		if (l >= node.r || r <= node.l) return;
		if (l <= node.l && r >= node.r) {
			node.add += val;
			flush(node_);
		} else {
			add(l, r, val, node_ << 1);
			add(l, r, val, node_ << 1 | 1);
			data[node_] = data[node_ << 1] * data[node_ << 1 | 1];
		}
	}
	void chmax(int l, int r, int val, int node_ = 1) {
		Node &node = data[node_];
		flush(node_);
		if (l >= node.r || r <= node.l) return;
		if (l <= node.l && r >= node.r) {
			node.chmax = val;
			flush(node_);
		} else {
			chmax(l, r, val, node_ << 1);
			chmax(l, r, val, node_ << 1 | 1);
			data[node_] = data[node_ << 1] * data[node_ << 1 | 1];
		}
	}
	int64_t sum(int l, int r, int node_ = 1) {
		Node &node = data[node_];
		flush(node_);
		if (l >= node.r || r <= node.l) return 0;
		if (l <= node.l && r >= node.r) return node.sum;
		else {
			int64_t res = sum(l, r, node_ << 1) +
						  sum(l, r, node_ << 1 | 1);
			data[node_] = data[node_ << 1] * data[node_ << 1 | 1];
			return res;
		}
	}
	void debug() {
		for (int i = 1; i < 2 * n; i++) {
			std::cerr << i << " : (" << data[i].l << "," << data[i].r << ")  ";
			std::cerr << "min(" << data[i].min << "," << data[i].mincnt << ")  ";
			std::cerr << "min2 : " << data[i].min2 << " sum:" << data[i].sum;
			std::cerr << " add : " << data[i].add << " chmax:" << data[i].chmax << std::endl;
		}
		std::cerr << std::endl;
	}
};

int main() {
	int n = ri();
	std::vector<int> a(n);
	for (auto &i : a) i = ri();
	SegTree tree(a);
	int q = ri();
	for (int i = 0; i < q; i++) {
		int type = ri();
		int l = ri() - 1;
		int r = ri();
		int a = ri();
		if (type == 1) tree.add(l, r, a);
		else {
			int64_t old = tree.sum(l, r);
			tree.add(l, r, -a);
			tree.chmax(l, r, 0);
			int64_t newer = tree.sum(l, r);
			std::cout << old - newer << std::endl;
		}
	}
	return 0;
}



Measured memory_bytes: 13448