#include using namespace std; #define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rrep(i, a, b) for (int i = (int)(a); i > (int)(b); i--) #define ll long long #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define PQ priority_queue, greater> #define PQ_g priority_queue, vector>, greater>> #define chmin(a, b) a = min(a, b) #define chmax(a, b) a = max(a, b) const int d4[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; const int d8[8][2] = {{0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}}; void Yes(bool b) {cout << (b ? "Yes" : "No") << endl;} ll isqrt(ll n) { ll l = 0LL, r = (1LL << 16); while (r - l > 1LL) { ll m = (l + r) >> 1; if (n < m * m) r = m; else l = m; } return l; } typedef struct { //表面上の数(区間和含む) ll cur_num = 0LL; //葉にある0でない数 ll not_0_cnt = 0LL; ll l_ind = 0LL, r_ind = 0LL; //isqrt済みの数を前計算したら、あとは線形 //data[i] = floor(cur_num ^ (1 / 2 ^ i)) ll data[6] = {0, 0, 0, 0, 0, 0}; void my_isqrt() { for (int i = 0; i < 5; i++) data[i] = data[i + 1]; data[5] = not_0_cnt; cur_num = data[0]; } void update_num(ll n, ll l0, ll r0) { l_ind = l0; r_ind = r0; cur_num = n * (r_ind - l_ind); data[0] = n; for (int i = 0; i < 5; i++) data[i + 1] = isqrt(data[i]); for (int i = 0; i <= 5; i++) data[i] *= (r_ind - l_ind); if (cur_num == 0LL) { not_0_cnt = 0LL; } else { not_0_cnt = (r_ind - l_ind); } } } Node; Node add(Node node1, Node node2) { Node ret; ret.l_ind = node1.l_ind; ret.r_ind = node1.r_ind; ret.cur_num = node1.cur_num + node2.cur_num; for (int i = 0; i <= 5; i++) ret.data[i] = node1.data[i] + node2.data[i]; ret.not_0_cnt = node1.not_0_cnt + node2.not_0_cnt; return ret; } typedef struct { ll isqrt_cnt = 0LL; ll lazy_num = -1LL; void update_num(ll num) { isqrt_cnt = 0LL; lazy_num = num; } void update_isqrt() { if (lazy_num == -1LL) { if (isqrt_cnt < 5) isqrt_cnt++; } else lazy_num = isqrt(lazy_num); } //(更新する数か? そうでないときは、isqrtの数, 更新する数(区間和でない) or isqrtの数) pair consume_lazy(){ pair ret; if (lazy_num == -1LL) { ret.first = false; ret.second = isqrt_cnt; isqrt_cnt = 0LL; lazy_num = -1LL; } else { ret.first = true; ret.second = lazy_num; isqrt_cnt = 0LL; lazy_num = -1LL; } return ret; } } LazyStock; class LazySegtree { private: int siz; vector data; vector lazy; void eval(int ind, int l, int r) { pair lazy_p = lazy[ind].consume_lazy(); if (lazy_p.first) { data[ind].update_num(lazy_p.second, (ll)l, (ll)r); if (ind < siz) { lazy[ind << 1].update_num(lazy_p.second); lazy[(ind << 1) + 1].update_num(lazy_p.second); } } else { for (int i = 0; i < lazy_p.second; i++) data[ind].my_isqrt(); if (ind < siz) { for (int i = 0; i < lazy_p.second; i++) lazy[ind << 1].update_isqrt(); for (int i = 0; i < lazy_p.second; i++) lazy[(ind << 1) + 1].update_isqrt(); } } } //x == -1ならisqrt、そうでないならnum int sub_update(int a, int b, int ind, int l, int r, ll x) { eval(ind, a, b); if (r <= a || b <= l) return 0; //何もしない if (l <= a && b <= r) { if (x != -1LL) lazy[ind].update_num(x); else lazy[ind].update_isqrt(); eval(ind, a, b); return 0; } int m = (a + b) >> 1; sub_update(a, m, (ind << 1), l, r, x); sub_update(m, b, (ind << 1) + 1, l, r, x); data[ind] = add(data[(ind << 1)], data[(ind << 1) + 1]); return 0; } ll sub_calc(int a, int b, int ind, int l, int r) { eval(ind, a, b); if (r <= a || b <= l) return 0; //単位元 if (l <= a && b <= r) return data[ind].cur_num; int m = (a + b) >> 1; //モノイドの演算 return (sub_calc(a, m, (ind << 1), l, r) + sub_calc(m, b, (ind << 1) + 1, l, r)); } public: LazySegtree(int n) { siz = 1; while (siz < n) siz <<= 1; data.resize(siz << 1); lazy.resize(siz << 1); } //半開区間 [l, r) void update(int l, int r, ll x) {sub_update(0, siz, 1, l, r, x);} //半開区間 [l, r) int calc(int l, int r) {return sub_calc(0, siz, 1, l, r);} }; int main() { int n, q; cin >> n >> q; LazySegtree A(n); rep(i, 0, n) { ll a; cin >> a; A.update(i, i + 1, a); } rep(i, 0, q) { int mode, l, r; cin >> mode >> l >> r; if (mode == 0) { cout << A.calc(l, r) << endl; } else { ll x; if (mode == 1) cin >> x; else x = -1LL; A.update(l, r, x); } } }