//#pragma GCC optimize("Ofast") //#pragma GCC target("avx") //#undef LOCAL #include using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } template using V = vector; template using VV = V>; #ifdef LOCAL struct PrettyOS { ostream& os; bool first; template auto operator<<(T&& x) { if (!first) os << ", "; first = false; os << x; return *this; } }; template void dbg0(T&&... t) { (PrettyOS{cerr, true} << ... << t); } #define dbg(...) \ do { \ cerr << __LINE__ << " : " << #__VA_ARGS__ << " = "; \ dbg0(__VA_ARGS__); \ cerr << endl; \ } while (false); #else #define dbg(...) #endif template ostream& operator<<(ostream& os, const pair& p) { return os << "P(" << p.first << ", " << p.second << ")"; } template ostream& operator<<(ostream& os, const V& v) { os << "["; for (auto d : v) os << d << ", "; return os << "]"; } ll gcd(ll a, ll b) { while (b) { ll c = b; b = a % b; a = c; } return a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } struct Node { using NP = Node*; NP l, r; ll sm, ma, sm_lcm, lz = -1; bool same = true; int sz; void update() { assert(lz == -1); sm = l->sm + r->sm; ma = max(l->ma, r->ma); sm_lcm = min(TEN(9) + 1, lcm(l->sm_lcm, r->sm_lcm)); same = l->same && r->same; if (l->ma != r->ma) same = false; } void lzdata(ll x) { sm = x * sz; ma = sm_lcm = x; same = true; lz = x; } void push() { if (lz != -1) { l->lzdata(lz); r->lzdata(lz); lz = -1; } } Node(const V& v, int _sz, int off) : sz(_sz) { if (sz == 1) { sm = ma = sm_lcm = v[off]; return; } l = new Node(v, sz / 2, off); r = new Node(v, sz - sz / 2, off + sz / 2); update(); } void set(int a, int b, ll x) { if (b <= 0 || sz <= a) return; if (a <= 0 && sz <= b) { lzdata(x); return; } push(); l->set(a, b, x); r->set(a - sz / 2, b - sz / 2, x); update(); } void down(int a, int b, ll x) { if (b <= 0 || sz <= a) return; if (x % sm_lcm == 0) return; if (a <= 0 && sz <= b && same) { lzdata(gcd(ma, x)); return; } push(); l->down(a, b, x); r->down(a - sz / 2, b - sz / 2, x); update(); } pair que(int a, int b) { if (b <= 0 || sz <= a) return {-1, 0}; if (a <= 0 && sz <= b) return {ma, sm}; push(); auto lq = l->que(a, b); auto rq = r->que(a - sz / 2, b - sz / 2); return {max(lq.first, rq.first), lq.second + rq.second}; } ~Node() { if (sz == 1) return; delete l; delete r; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; V a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } auto tr = new Node(a, n, 0); for (int ph = 0; ph < q; ph++) { int ty, l, r; cin >> ty >> l >> r; l--; if (ty == 1) { ll x; cin >> x; tr->set(l, r, x); } else if (ty == 2) { ll x; cin >> x; tr->down(l, r, x); } else if (ty == 3) { auto q = tr->que(l, r); cout << q.first << "\n"; } else { auto q = tr->que(l, r); cout << q.second << "\n"; } /*for (int i = 0; i < n; i++) { cout << tr->que(i, i + 1).first << " "; } cout << endl;*/ } delete tr; return 0; }