#pragma GCC diagnostic warning "-Wextra" #pragma GCC diagnostic warning "-Wshadow" #include using namespace std; #include #include using namespace std; using namespace __gnu_pbds; template bool cmin(A& a, B b) { return a > b && (a = b, true); } template bool cmax(A& a, B b) { return a < b && (a = b, true); } template ostream& operator<<(ostream& os, pair& p) { return os << '(' << p.first << ", " << p.second << ')'; } static bool debug = false; void dump() {} template void dump(A&& a, B&&... b) { if (debug) cout << a << (sizeof...(b) ? ' ' : '\n'), dump(b...); } template void dump1D(A& a) { if (debug) for (auto i = a.begin(); i != a.end(); i++) cout << *i << (next(i) != a.end() ? ' ' : '\n'); } template void dump2D(A& a) { if (debug) for (auto i = a.begin(); i != a.end(); i++) dump1D(*i), cout << (next(i) != a.end() ? "" : "\n"); } template class FenwickTree { private: long N, M; vector B; public: FenwickTree() : N(0) {} FenwickTree(long n) : N(n + 1), M(1), B(N, 0) { while (M * 2 <= N) M *= 2; } FenwickTree(const vector &v) : N((long)v.size() + 1), M(1), B(N, 0) { for (long i = 0; i < N - 1; i++) add(i, v.at(i)); while (M * 2 <= N) M *= 2; } void add(long i, T x) { i++; while (i < N) B.at(i) += x, i += i & -i; } void add(long l, long r, T x) { add(l, x); add(r + 1, -x); } T sum(long i) { i++; T ret = 0; while (i > 0) ret += B.at(i), i -= i & -i; return ret; } T sum(long l, long r) { return sum(r) - sum(l - 1); } T at(long i) { return sum(i) - sum(i - 1); } long lower_bound(T w) { if (w <= 0) return 0; long x = 0; for (long k = M; k > 0; k /= 2) { if (x + k <= N - 1 && B.at(x + k) < w) { w -= B.at(x + k); x += k; } } return x; } long upper_bound(T w) { if (w < 0) return 0; long x = 0; for (long k = M; k > 0; k /= 2) { if (x + k <= N - 1 && B.at(x + k) <= w) { w -= B.at(x + k); x += k; } } return x; } }; signed main() { cin.tie(nullptr)->sync_with_stdio(false); debug = true; long N, Q; cin >> N >> Q; vector A(N); for (long i = 0; i < N; i++) cin >> A.at(i); vector> V(Q); for (long i = 0; i < Q; i++) { long a, b, c; cin >> a >> b >> c, b--, c--; V.at(i) = {a, b, c}; } FenwickTree FT(A); tree, rb_tree_tag, tree_order_statistics_node_update> T; for (long i = 0; i < N; i++) T.insert(i); for (const auto& [a, l, r] : V) { if (a == 1) { for (long i = r; i > l; i--) T.erase(*T.find_by_order(i)); } else { cout << FT.sum(*T.find_by_order(l), *T.find_by_order(r)) << '\n'; } } }