#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: int N, M; vector B; public: FenwickTree() : N(0) {} FenwickTree(int n) : N(n + 1), M(1), B(N, 0) { while (M * 2 <= N) M *= 2; } FenwickTree(const vector &v) : N((int)v.size() + 1), M(1), B(N, 0) { for (int i = 0; i < N - 1; i++) add(i, v.at(i)); while (M * 2 <= N) M *= 2; } void add(int i, T x) { i++; while (i < N) B.at(i) += x, i += i & -i; } void add(int l, int r, T x) { add(l, x); add(r + 1, -x); } T sum(int i) { i++; T ret = 0; while (i > 0) ret += B.at(i), i -= i & -i; return ret; } T sum(int l, int r) { return sum(r) - sum(l - 1); } T at(int i) { return sum(i) - sum(i - 1); } int lower_bound(T w) { if (w <= 0) return 0; int x = 0; for (int 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; } int upper_bound(T w) { if (w < 0) return 0; int x = 0; for (int 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; int N, Q; cin >> N >> Q; vector A(N); for (int i = 0; i < N; i++) cin >> A.at(i); vector> V(Q); for (int i = 0; i < Q; i++) { int 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 (int i = 0; i < N; i++) T.insert(i); for (const auto& [a, l, r] : V) { if (a == 1) { for (int 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'; } } }