#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
class SegmentTree {
vector<ll> tree, lazy;
int n;
void push(int node, int start, int end) {
if (lazy[node] != 0) {
tree[node] += (end - start + 1) * lazy[node]; // Apply lazy value to the current segment
if (start != end) {
lazy[node * 2 + 1] += lazy[node]; // Propagate to left child
lazy[node * 2 + 2] += lazy[node]; // Propagate to right child
}
lazy[node] = 0; // Clear the lazy value
}
}
void updateRange(int node, int start, int end, int l, int r, ll val) {
push(node, start, end); // Apply pending updates
if (start > r || end < l) return; // Out of range
if (start >= l && end <= r) { // Completely within range
lazy[node] += val; // Mark for lazy propagation
push(node, start, end); // Apply the update now
return;
}
int mid = (start + end) / 2;
updateRange(node * 2 + 1, start, mid, l, r, val); // Update left child
updateRange(node * 2 + 2, mid + 1, end, l, r, val); // Update right child
tree[node] = tree[node * 2 + 1] + tree[node * 2 + 2]; // Update current node
}
ll queryRange(int node, int start, int end, int l, int r) {
push(node, start, end); // Apply pending updates
if (start > r || end < l) return 0; // Out of range
if (start >= l && end <= r) return tree[node]; // Completely within range
int mid = (start + end) / 2;
ll leftSum = queryRange(node * 2 + 1, start, mid, l, r);
ll rightSum = queryRange(node * 2 + 2, mid + 1, end, l, r);
return leftSum + rightSum; // Combine results
}
public:
SegmentTree(int size) {
n = size;
tree.assign(4 * n, 0);
lazy.assign(4 * n, 0);
}
void updateRange(int l, int r, ll val) {
updateRange(0, 0, n - 1, l, r, val);
}
ll queryRange(int l, int r) {
return queryRange(0, 0, n - 1, l, r);
}
};
void solve() {
int n, c;
cin >> n >> c;
SegmentTree segTree(n);
while (c--) {
int type;
cin >> type;
if (type == 0) { // Range update
int p, q;
ll v;
cin >> p >> q >> v;
segTree.updateRange(p - 1, q - 1, v);
} else if (type == 1) { // Range sum query
int p, q;
cin >> p >> q;
cout << segTree.queryRange(p - 1, q - 1) << '\n';
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;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