#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Node {
ll sum_salary; // Sum of salaries in the segment
ll sum_happiness; // Sum of happiness in the segment
ll uniform_salary; // Uniform salary value if all salaries are the same
bool is_uniform; // Flag indicating if the segment is uniform
ll lazy_set; // Pending set operation (None if -1)
ll lazy_add; // Pending add operation
Node() : sum_salary(0), sum_happiness(0), uniform_salary(0), is_uniform(true), lazy_set(-1), lazy_add(0) {}
};
const int MAXN = 200005;
int N, Q;
ll a[MAXN];
Node tree[4 * MAXN];
void build(int node, int l, int r) {
if (l == r) {
tree[node].sum_salary = a[l];
tree[node].sum_happiness = 0;
tree[node].uniform_salary = a[l];
tree[node].is_uniform = true;
tree[node].lazy_set = -1;
tree[node].lazy_add = 0;
} else {
int mid = (l + r) / 2;
build(2 * node, l, mid);
build(2 * node + 1, mid + 1, r);
tree[node].sum_salary = tree[2 * node].sum_salary + tree[2 * node + 1].sum_salary;
tree[node].sum_happiness = tree[2 * node].sum_happiness + tree[2 * node + 1].sum_happiness;
tree[node].is_uniform = tree[2 * node].is_uniform && tree[2 * node + 1].is_uniform &&
tree[2 * node].uniform_salary == tree[2 * node + 1].uniform_salary;
if (tree[node].is_uniform) {
tree[node].uniform_salary = tree[2 * node].uniform_salary;
}
tree[node].lazy_set = -1;
tree[node].lazy_add = 0;
}
}
void push(int node, int l, int r) {
if (tree[node].lazy_set != -1) {
// Apply the set operation
ll c = tree[node].lazy_set;
ll cnt = r - l + 1;
if (tree[node].is_uniform) {
ll s_prev = tree[node].uniform_salary;
ll happiness_change = 0;
if (c > s_prev) happiness_change = cnt;
else if (c < s_prev) happiness_change = -cnt;
tree[node].sum_happiness += happiness_change;
} else {
// Can't compute directly; need to propagate
push(2 * node, l, (l + r) / 2);
push(2 * node + 1, (l + r) / 2 + 1, r);
}
tree[node].sum_salary = c * cnt;
tree[node].uniform_salary = c;
tree[node].is_uniform = true;
// Clear lazy tags
tree[node].lazy_set = -1;
tree[node].lazy_add = 0;
if (l != r) {
tree[2 * node].lazy_set = c;
tree[2 * node].lazy_add = 0;
tree[2 * node + 1].lazy_set = c;
tree[2 * node + 1].lazy_add = 0;
}
}
if (tree[node].lazy_add != 0) {
ll c = tree[node].lazy_add;
ll cnt = r - l + 1;
if (tree[node].is_uniform) {
ll s_prev = tree[node].uniform_salary;
ll s_new = s_prev + c;
ll happiness_change = 0;
if (c > 0) happiness_change = cnt;
else if (c < 0) happiness_change = -cnt;
tree[node].sum_happiness += happiness_change;
tree[node].uniform_salary = s_new;
} else {
// Can't compute directly; need to propagate
push(2 * node, l, (l + r) / 2);
push(2 * node + 1, (l + r) / 2 + 1, r);
}
tree[node].sum_salary += c * cnt;
if (l != r) {
if (tree[2 * node].lazy_set != -1) {
tree[2 * node].lazy_set += c;
} else {
tree[2 * node].lazy_add += c;
}
if (tree[2 * node + 1].lazy_set != -1) {
tree[2 * node + 1].lazy_set += c;
} else {
tree[2 * node + 1].lazy_add += c;
}
}
tree[node].lazy_add = 0;
}
}
void range_set(int node, int l, int r, int ql, int qr, ll c) {
push(node, l, r);
if (r < ql || l > qr) return;
if (ql <= l && r <= qr) {
tree[node].lazy_set = c;
push(node, l, r);
} else {
int mid = (l + r) / 2;
range_set(2 * node, l, mid, ql, qr, c);
range_set(2 * node + 1, mid + 1, r, ql, qr, c);
tree[node].sum_salary = tree[2 * node].sum_salary + tree[2 * node + 1].sum_salary;
tree[node].sum_happiness = tree[2 * node].sum_happiness + tree[2 * node + 1].sum_happiness;
tree[node].is_uniform = tree[2 * node].is_uniform && tree[2 * node + 1].is_uniform &&
tree[2 * node].uniform_salary == tree[2 * node + 1].uniform_salary;
if (tree[node].is_uniform) {
tree[node].uniform_salary = tree[2 * node].uniform_salary;
} else {
tree[node].uniform_salary = 0;
}
}
}
void range_add(int node, int l, int r, int ql, int qr, ll c) {
push(node, l, r);
if (r < ql || l > qr) return;
if (ql <= l && r <= qr) {
tree[node].lazy_add += c;
push(node, l, r);
} else {
int mid = (l + r) / 2;
range_add(2 * node, l, mid, ql, qr, c);
range_add(2 * node + 1, mid + 1, r, ql, qr, c);
tree[node].sum_salary = tree[2 * node].sum_salary + tree[2 * node + 1].sum_salary;
tree[node].sum_happiness = tree[2 * node].sum_happiness + tree[2 * node + 1].sum_happiness;
tree[node].is_uniform = tree[2 * node].is_uniform && tree[2 * node + 1].is_uniform &&
tree[2 * node].uniform_salary == tree[2 * node + 1].uniform_salary;
if (tree[node].is_uniform) {
tree[node].uniform_salary = tree[2 * node].uniform_salary;
} else {
tree[node].uniform_salary = 0;
}
}
}
pair<ll, ll> query_salary(int node, int l, int r, int ql, int qr) {
push(node, l, r);
if (r < ql || l > qr) return {0, 0};
if (ql <= l && r <= qr) {
return {tree[node].sum_salary, r - l + 1};
} else {
int mid = (l + r) / 2;
auto left = query_salary(2 * node, l, mid, ql, qr);
auto right = query_salary(2 * node + 1, mid + 1, r, ql, qr);
return {left.first + right.first, left.second + right.second};
}
}
pair<ll, ll> query_happiness(int node, int l, int r, int ql, int qr) {
push(node, l, r);
if (r < ql || l > qr) return {0, 0};
if (ql <= l && r <= qr) {
return {tree[node].sum_happiness, r - l + 1};
} else {
int mid = (l + r) / 2;
auto left = query_happiness(2 * node, l, mid, ql, qr);
auto right = query_happiness(2 * node + 1, mid + 1, r, ql, qr);
return {left.first + right.first, left.second + right.second};
}
}
ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a;
}
void print_fraction(ll num, ll denom) {
ll g = gcd(abs(num), denom);
num /= g;
denom /= g;
cout << num << "/" << denom << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> Q;
for (int i = 1; i <= N; ++i) {
cin >> a[i];
}
build(1, 1, N);
while (Q--) {
int type;
cin >> type;
if (type == 0) {
int l, r;
ll c;
cin >> l >> r >> c;
range_set(1, 1, N, l, r, c);
} else if (type == 1) {
int l, r;
ll c;
cin >> l >> r >> c;
range_add(1, 1, N, l, r, c);
} else if (type == 2) {
int l, r;
cin >> l >> r;
auto res = query_salary(1, 1, N, l, r);
ll sum = res.first;
ll cnt = res.second;
print_fraction(sum, cnt);
} else if (type == 3) {
int l, r;
cin >> l >> r;
auto res = query_happiness(1, 1, N, l, r);
ll sum = res.first;
ll cnt = res.second;
print_fraction(sum, cnt);
}
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKCnN0cnVjdCBOb2RlIHsKICAgIGxsIHN1bV9zYWxhcnk7ICAgICAgICAvLyBTdW0gb2Ygc2FsYXJpZXMgaW4gdGhlIHNlZ21lbnQKICAgIGxsIHN1bV9oYXBwaW5lc3M7ICAgICAvLyBTdW0gb2YgaGFwcGluZXNzIGluIHRoZSBzZWdtZW50CiAgICBsbCB1bmlmb3JtX3NhbGFyeTsgICAgLy8gVW5pZm9ybSBzYWxhcnkgdmFsdWUgaWYgYWxsIHNhbGFyaWVzIGFyZSB0aGUgc2FtZQogICAgYm9vbCBpc191bmlmb3JtOyAgICAgIC8vIEZsYWcgaW5kaWNhdGluZyBpZiB0aGUgc2VnbWVudCBpcyB1bmlmb3JtCiAgICBsbCBsYXp5X3NldDsgICAgICAgICAgLy8gUGVuZGluZyBzZXQgb3BlcmF0aW9uIChOb25lIGlmIC0xKQogICAgbGwgbGF6eV9hZGQ7ICAgICAgICAgIC8vIFBlbmRpbmcgYWRkIG9wZXJhdGlvbgoKICAgIE5vZGUoKSA6IHN1bV9zYWxhcnkoMCksIHN1bV9oYXBwaW5lc3MoMCksIHVuaWZvcm1fc2FsYXJ5KDApLCBpc191bmlmb3JtKHRydWUpLCBsYXp5X3NldCgtMSksIGxhenlfYWRkKDApIHt9Cn07Cgpjb25zdCBpbnQgTUFYTiA9IDIwMDAwNTsKaW50IE4sIFE7CmxsIGFbTUFYTl07Ck5vZGUgdHJlZVs0ICogTUFYTl07Cgp2b2lkIGJ1aWxkKGludCBub2RlLCBpbnQgbCwgaW50IHIpIHsKICAgIGlmIChsID09IHIpIHsKICAgICAgICB0cmVlW25vZGVdLnN1bV9zYWxhcnkgPSBhW2xdOwogICAgICAgIHRyZWVbbm9kZV0uc3VtX2hhcHBpbmVzcyA9IDA7CiAgICAgICAgdHJlZVtub2RlXS51bmlmb3JtX3NhbGFyeSA9IGFbbF07CiAgICAgICAgdHJlZVtub2RlXS5pc191bmlmb3JtID0gdHJ1ZTsKICAgICAgICB0cmVlW25vZGVdLmxhenlfc2V0ID0gLTE7CiAgICAgICAgdHJlZVtub2RlXS5sYXp5X2FkZCA9IDA7CiAgICB9IGVsc2UgewogICAgICAgIGludCBtaWQgPSAobCArIHIpIC8gMjsKICAgICAgICBidWlsZCgyICogbm9kZSwgbCwgbWlkKTsKICAgICAgICBidWlsZCgyICogbm9kZSArIDEsIG1pZCArIDEsIHIpOwogICAgICAgIHRyZWVbbm9kZV0uc3VtX3NhbGFyeSA9IHRyZWVbMiAqIG5vZGVdLnN1bV9zYWxhcnkgKyB0cmVlWzIgKiBub2RlICsgMV0uc3VtX3NhbGFyeTsKICAgICAgICB0cmVlW25vZGVdLnN1bV9oYXBwaW5lc3MgPSB0cmVlWzIgKiBub2RlXS5zdW1faGFwcGluZXNzICsgdHJlZVsyICogbm9kZSArIDFdLnN1bV9oYXBwaW5lc3M7CiAgICAgICAgdHJlZVtub2RlXS5pc191bmlmb3JtID0gdHJlZVsyICogbm9kZV0uaXNfdW5pZm9ybSAmJiB0cmVlWzIgKiBub2RlICsgMV0uaXNfdW5pZm9ybSAmJiAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB0cmVlWzIgKiBub2RlXS51bmlmb3JtX3NhbGFyeSA9PSB0cmVlWzIgKiBub2RlICsgMV0udW5pZm9ybV9zYWxhcnk7CiAgICAgICAgaWYgKHRyZWVbbm9kZV0uaXNfdW5pZm9ybSkgewogICAgICAgICAgICB0cmVlW25vZGVdLnVuaWZvcm1fc2FsYXJ5ID0gdHJlZVsyICogbm9kZV0udW5pZm9ybV9zYWxhcnk7CiAgICAgICAgfQogICAgICAgIHRyZWVbbm9kZV0ubGF6eV9zZXQgPSAtMTsKICAgICAgICB0cmVlW25vZGVdLmxhenlfYWRkID0gMDsKICAgIH0KfQoKdm9pZCBwdXNoKGludCBub2RlLCBpbnQgbCwgaW50IHIpIHsKICAgIGlmICh0cmVlW25vZGVdLmxhenlfc2V0ICE9IC0xKSB7CiAgICAgICAgLy8gQXBwbHkgdGhlIHNldCBvcGVyYXRpb24KICAgICAgICBsbCBjID0gdHJlZVtub2RlXS5sYXp5X3NldDsKICAgICAgICBsbCBjbnQgPSByIC0gbCArIDE7CiAgICAgICAgaWYgKHRyZWVbbm9kZV0uaXNfdW5pZm9ybSkgewogICAgICAgICAgICBsbCBzX3ByZXYgPSB0cmVlW25vZGVdLnVuaWZvcm1fc2FsYXJ5OwogICAgICAgICAgICBsbCBoYXBwaW5lc3NfY2hhbmdlID0gMDsKICAgICAgICAgICAgaWYgKGMgPiBzX3ByZXYpIGhhcHBpbmVzc19jaGFuZ2UgPSBjbnQ7CiAgICAgICAgICAgIGVsc2UgaWYgKGMgPCBzX3ByZXYpIGhhcHBpbmVzc19jaGFuZ2UgPSAtY250OwogICAgICAgICAgICB0cmVlW25vZGVdLnN1bV9oYXBwaW5lc3MgKz0gaGFwcGluZXNzX2NoYW5nZTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAvLyBDYW4ndCBjb21wdXRlIGRpcmVjdGx5OyBuZWVkIHRvIHByb3BhZ2F0ZQogICAgICAgICAgICBwdXNoKDIgKiBub2RlLCBsLCAobCArIHIpIC8gMik7CiAgICAgICAgICAgIHB1c2goMiAqIG5vZGUgKyAxLCAobCArIHIpIC8gMiArIDEsIHIpOwogICAgICAgIH0KICAgICAgICB0cmVlW25vZGVdLnN1bV9zYWxhcnkgPSBjICogY250OwogICAgICAgIHRyZWVbbm9kZV0udW5pZm9ybV9zYWxhcnkgPSBjOwogICAgICAgIHRyZWVbbm9kZV0uaXNfdW5pZm9ybSA9IHRydWU7CiAgICAgICAgLy8gQ2xlYXIgbGF6eSB0YWdzCiAgICAgICAgdHJlZVtub2RlXS5sYXp5X3NldCA9IC0xOwogICAgICAgIHRyZWVbbm9kZV0ubGF6eV9hZGQgPSAwOwogICAgICAgIGlmIChsICE9IHIpIHsKICAgICAgICAgICAgdHJlZVsyICogbm9kZV0ubGF6eV9zZXQgPSBjOwogICAgICAgICAgICB0cmVlWzIgKiBub2RlXS5sYXp5X2FkZCA9IDA7CiAgICAgICAgICAgIHRyZWVbMiAqIG5vZGUgKyAxXS5sYXp5X3NldCA9IGM7CiAgICAgICAgICAgIHRyZWVbMiAqIG5vZGUgKyAxXS5sYXp5X2FkZCA9IDA7CiAgICAgICAgfQogICAgfQogICAgaWYgKHRyZWVbbm9kZV0ubGF6eV9hZGQgIT0gMCkgewogICAgICAgIGxsIGMgPSB0cmVlW25vZGVdLmxhenlfYWRkOwogICAgICAgIGxsIGNudCA9IHIgLSBsICsgMTsKICAgICAgICBpZiAodHJlZVtub2RlXS5pc191bmlmb3JtKSB7CiAgICAgICAgICAgIGxsIHNfcHJldiA9IHRyZWVbbm9kZV0udW5pZm9ybV9zYWxhcnk7CiAgICAgICAgICAgIGxsIHNfbmV3ID0gc19wcmV2ICsgYzsKICAgICAgICAgICAgbGwgaGFwcGluZXNzX2NoYW5nZSA9IDA7CiAgICAgICAgICAgIGlmIChjID4gMCkgaGFwcGluZXNzX2NoYW5nZSA9IGNudDsKICAgICAgICAgICAgZWxzZSBpZiAoYyA8IDApIGhhcHBpbmVzc19jaGFuZ2UgPSAtY250OwogICAgICAgICAgICB0cmVlW25vZGVdLnN1bV9oYXBwaW5lc3MgKz0gaGFwcGluZXNzX2NoYW5nZTsKICAgICAgICAgICAgdHJlZVtub2RlXS51bmlmb3JtX3NhbGFyeSA9IHNfbmV3OwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIC8vIENhbid0IGNvbXB1dGUgZGlyZWN0bHk7IG5lZWQgdG8gcHJvcGFnYXRlCiAgICAgICAgICAgIHB1c2goMiAqIG5vZGUsIGwsIChsICsgcikgLyAyKTsKICAgICAgICAgICAgcHVzaCgyICogbm9kZSArIDEsIChsICsgcikgLyAyICsgMSwgcik7CiAgICAgICAgfQogICAgICAgIHRyZWVbbm9kZV0uc3VtX3NhbGFyeSArPSBjICogY250OwogICAgICAgIGlmIChsICE9IHIpIHsKICAgICAgICAgICAgaWYgKHRyZWVbMiAqIG5vZGVdLmxhenlfc2V0ICE9IC0xKSB7CiAgICAgICAgICAgICAgICB0cmVlWzIgKiBub2RlXS5sYXp5X3NldCArPSBjOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgdHJlZVsyICogbm9kZV0ubGF6eV9hZGQgKz0gYzsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAodHJlZVsyICogbm9kZSArIDFdLmxhenlfc2V0ICE9IC0xKSB7CiAgICAgICAgICAgICAgICB0cmVlWzIgKiBub2RlICsgMV0ubGF6eV9zZXQgKz0gYzsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHRyZWVbMiAqIG5vZGUgKyAxXS5sYXp5X2FkZCArPSBjOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHRyZWVbbm9kZV0ubGF6eV9hZGQgPSAwOwogICAgfQp9Cgp2b2lkIHJhbmdlX3NldChpbnQgbm9kZSwgaW50IGwsIGludCByLCBpbnQgcWwsIGludCBxciwgbGwgYykgewogICAgcHVzaChub2RlLCBsLCByKTsKICAgIGlmIChyIDwgcWwgfHwgbCA+IHFyKSByZXR1cm47CiAgICBpZiAocWwgPD0gbCAmJiByIDw9IHFyKSB7CiAgICAgICAgdHJlZVtub2RlXS5sYXp5X3NldCA9IGM7CiAgICAgICAgcHVzaChub2RlLCBsLCByKTsKICAgIH0gZWxzZSB7CiAgICAgICAgaW50IG1pZCA9IChsICsgcikgLyAyOwogICAgICAgIHJhbmdlX3NldCgyICogbm9kZSwgbCwgbWlkLCBxbCwgc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CiAgICB9Cn0KCmxsIGdjZChsbCBhLCBsbCBiKSB7CiAgICByZXR1cm4gYiA/IGdjZChiLCBhICUgYikgOiBhOwp9Cgp2b2lkIHByaW50X2ZyYWN0aW9uKGxsIG51bSwgbGwgZGVub20pIHsKICAgIGxsIGcgPSBnY2QoYWJzKG51bSksIGRlbm9tKTsKICAgIG51bSAvPSBnOwogICAgZGVub20gLz0gZzsKICAgIGNvdXQgPDwgbnVtIDw8ICIvIiA8PCBkZW5vbSA8PCAiXG4iOwp9CgppbnQgbWFpbigpIHsKICAgIGlvczo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50aWUobnVsbHB0cik7CiAgICBjaW4gPj4gTiA+PiBROwogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gTjsgKytpKSB7CiAgICAgICAgY2luID4+IGFbaV07CiAgICB9CiAgICBidWlsZCgxLCAxLCBOKTsKICAgIHdoaWxlIChRLS0pIHsKICAgICAgICBpbnQgdHlwZTsKICAgICAgICBjaW4gPj4gdHlwZTsKICAgICAgICBpZiAodHlwZSA9PSAwKSB7CiAgICAgICAgICAgIGludCBsLCByOwogICAgICAgICAgICBsbCBjOwogICAgICAgICAgICBjaW4gPj4gbCA+PiByID4+IGM7CiAgICAgICAgICAgIHJhbmdlX3NldCgxLCAxLCBOLCBsLCByLCBjKTsKICAgICAgICB9IGVsc2UgaWYgKHR5cGUgPT0gMSkgewogICAgICAgICAgICBpbnQgbCwgcjsKICAgICAgICAgICAgbGwgYzsKICAgICAgICAgICAgY2luID4+IGwgPj4gciA+PiBjOwogICAgICAgICAgICByYW5nZV9hZGQoMSwgMSwgTiwgbCwgciwgYyk7CiAgICAgICAgfSBlbHNlIGlmICh0eXBlID09IDIpIHsKICAgICAgICAgICAgaW50IGwsIHI7CiAgICAgICAgICAgIGNpbiA+PiBsID4+IHI7CiAgICAgICAgICAgIGF1dG8gcmVzID0gcXVlcnlfc2FsYXJ5KDEsIDEsIE4sIGwsIHIpOwogICAgICAgICAgICBsbCBzdW0gPSByZXMuZmlyc3Q7CiAgICAgICAgICAgIGxsIGNudCA9IHJlcy5zZWNvbmQ7CiAgICAgICAgICAgIHByaW50X2ZyYWN0aW9uKHN1bSwgY250KTsKICAgICAgICB9IGVsc2UgaWYgKHR5cGUgPT0gMykgewogICAgICAgICAgICBpbnQgbCwgcjsKICAgICAgICAgICAgY2luID4+IGwgPj4gcjsKICAgICAgICAgICAgYXV0byByZXMgPSBxdWVyeV9oYXBwaW5lc3MoMSwgMSwgTiwgbCwgcik7CiAgICAgICAgICAgIGxsIHN1bSA9IHJlcy5maXJzdDsKICAgICAgICAgICAgbGwgY250ID0gcmVzLnNlY29uZDsKICAgICAgICAgICAgcHJpbnRfZnJhY3Rpb24oc3VtLCBjbnQpOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9Cg==