#include using namespace std; typedef long long ll; typedef pair l_l; typedef pair i_i; template inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } template inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } const ll INF = 1e9; //const ll mod = 1000000007; struct LazySegmentTree { private: int n; vector node, oddnum, evennum, Mod, Add; public: LazySegmentTree() { int sz = 100500; n = 1; while(n < sz) n *= 2; node.resize(2*n-1); oddnum.resize(2*n-1, 0); evennum.resize(2*n-1, 1); Add.resize(2*n-1, 0); Mod.resize(2*n-1, 0); for(int i=0; i=0; i--) { node[i] = (node[i*2+1] + node[i*2+2]); oddnum[i] = (oddnum[i*2+1] + oddnum[i*2+2]); evennum[i] = (evennum[i*2+1] + evennum[i*2+2]); } } void lazyEvaluate(int k, ll l, ll r) { //cerr << "evaluating: " << k << " " << l << " " << r << endl; if(Mod[k] != 0) { if(Mod[k] == 1) { node[k] = oddnum[k]; } else if(Mod[k] == 2) { node[k] = evennum[k]; swap(oddnum[k], evennum[k]); } else exit(0); if(r - l > 1) { for(int j = k*2+1; j <= k*2+2; j++) { if(Add[j] % 2 == 1) { if(Mod[j] == 0 || Mod[j] == 1) { Mod[j] = 3 - Mod[k]; } else if(Mod[j] == 2) { Mod[j] = Mod[k]; } } else { if(Mod[j] == 0 || Mod[j] == 1) { Mod[j] = Mod[k]; } else if(Mod[j] == 2) { Mod[j] = 3 - Mod[k]; } } Add[j] = 0; } } Mod[k] = 0; } if(Add[k]) { node[k] += Add[k] * (r - l); if(Add[k] % 2 == 1) { swap(evennum[k], oddnum[k]); } if(r - l > 1) { Add[k*2+1] += Add[k]; Add[k*2+1] += Add[k]; } Add[k] = 0; } } void setmod(int a, int b, int k=0, int l=0, int r=-1) { if(r < 0) r = n; lazyEvaluate(k, l, r); if(b <= l || r <= a) return; if(a <= l && r <= b) { Mod[k] = 1; lazyEvaluate(k, l, r); } else { setmod(a, b, 2*k+1, l, (l+r)/2); setmod(a, b, 2*k+2, (l+r)/2, r); node[k] = (node[2*k+1] + node[2*k+2]); oddnum[k] = oddnum[2*k+1] + oddnum[2*k+2]; evennum[k] = evennum[2*k+1] + evennum[2*k+2]; } } void add(int a, int b, ll x, int k=0, int l=0, int r=-1) { if(r < 0) r = n; lazyEvaluate(k, l, r); if(b <= l || r <= a) return; if(a <= l && r <= b) { Add[k] += x; lazyEvaluate(k, l, r); } else { add(a, b, x, 2*k+1, l, (l+r)/2); add(a, b, x, 2*k+2, (l+r)/2, r); node[k] = (node[2*k+1] + node[2*k+2]); oddnum[k] = oddnum[2*k+1] + oddnum[2*k+2]; evennum[k] = evennum[2*k+1] + evennum[2*k+2]; } } ll getsum(int a, int b, int k=0, int l=0, int r=-1) { if(r < 0) r = n; lazyEvaluate(k, l, r); if(b <= l || r <= a) return 0; if(a <= l && r <= b) return node[k]; ll vl = getsum(a, b, 2*k+1, l, (l+r)/2); ll vr = getsum(a, b, 2*k+2, (l+r)/2, r); return (vl + vr); } void print(int b) { for(int i = 0; i <= b + n - 1; i++) { cerr << i << " " << node[i] << " " << Add[i] << " " << oddnum[i] << " " << evennum[i] << " " << Mod[i] << endl; } } }; int main() { //cout.precision(10); cin.tie(0); ios::sync_with_stdio(false); int N, Q; cin >> N >> Q; LazySegmentTree seg; for(int i = 1; i <= N; i++) { int a; cin >> a; seg.add(i, i + 1, a); //cerr << "array: " << i << " " << a << endl; } for(int q = 1; q <= Q; q++) { /* cerr << "--------------" << endl; seg.print(N); cerr << "--------------" << endl; */ int ope; cin >> ope; //cerr << q << " " << ope << endl; if(ope == 1) { int l, r; cin >> l >> r; //cerr << "one: " << l << " " << r << endl; seg.setmod(l, r + 1); continue; } else if(ope == 2) { int l, r, x; cin >> l >> r >> x; seg.add(l, r + 1, x); continue; } else if(ope == 3) { //cerr << "A" << endl; int l, r; cin >> l >> r; cout << seg.getsum(l, r + 1) << endl; } } //seg.print(N); return 0; }