#include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; template struct bit{ int n; vector data; bit(int n_){ n = 1; while(n < n_) n *= 2; data = vector(n+1); for(int i = 0; i <= n; i++) data[i] = 0; } T sum(int i){ T ret = 0; while(i > 0){ ret += data[i]; i -= i&-i; } return ret; } void add(int i, T x){ while(i <= n){ data[i] += x; i += i&-i; } } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int n, q; cin >> n >> q; vector a(n+1), cumsum(n+1); for(int i = 1; i <= n; i++) { cin >> a[i]; cumsum[i] = cumsum[i-1]+a[i]; } bit bt(n); for(int i = 1; i <= n; i++) bt.add(i, 1); auto sect_sum = [&](int l, int r){ return cumsum[r]-cumsum[l-1]; }; auto find_last = [&](int x){ if(x == 0) return 0; int l = 0, r = n; while(r-l > 1){ int c = (l+r)/2; if(bt.sum(c) < x) l = c; else r = c; } return r; }; set st; for(int i = 1; i <= n; i++) st.insert(i); while(q--){ int t, l, r; cin >> t >> l >> r; if(t == 1){ int a = find_last(l-1); int b = find_last(r); auto p = st.lower_bound(a+2); vector to_erase; while(p != st.end()){ if(*p > b) break; bt.add(*p, -1); to_erase.push_back(*p); p++; } for(int x : to_erase) st.erase(x); }else{ int a = find_last(l-1); int b = find_last(r); cout << sect_sum(a+1, b) << endl; } } }