Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <queue>
- #include <cmath>
- #include <set>
- #include <stack>
- #include <bitset>
- #include <map>
- #include <ctime>
- #include <numeric>
- #ifndef M_PI
- #define M_PI 3.141592653589
- #endif
- #define int long long
- #define double long double
- #ifdef TIME
- #define start cin.tie(NULL); cout.tie(NULL); cout.setf(ios::fixed); cout.precision(10); ios_base::sync_with_stdio(false);int32_t START = clock()
- #define finish cout << "\ntime: " << (clock() - START) / (CLOCKS_PER_SEC * 1.0); return 0
- #endif
- #ifndef TIME
- #define start cin.tie(NULL); cout.tie(NULL); cout.setf(ios::fixed); cout.precision(10); ios_base::sync_with_stdio(false)
- #define finish return 0
- #endif
- using namespace std;
- //vector input
- template<typename T>
- istream &operator>>(istream &is, vector<T> &vec) {
- for (auto &i : vec) {
- cin >> i;
- }
- return is;
- }
- //pair output
- template<typename E>
- ostream &operator<<(ostream &os, pair<E, E> &t) {
- os << t.first << ' ' << t.second;
- return os;
- }
- //"map" pair output
- template<typename E>
- ostream &operator<<(ostream &os, pair<const E, E> &t) {
- os << t.first << ' ' << t.second;
- return os;
- }
- //vector output
- template<typename T>
- ostream &operator<<(ostream &os, vector<T> &vec) {
- for (T i : vec) {
- os << i << ' ';
- }
- return os;
- }
- //2 dimensional vector output
- template<typename T>
- ostream &operator<<(ostream &os, vector<vector<T> > &vec) {
- for (vector<T> i : vec) {
- os << i << '\n';
- }
- return os;
- }
- struct segtree {
- int n;
- vector<int> tree;
- segtree(vector<int> &a) {
- n = a.size();
- tree.resize(2 * n);
- for (int i = 0; i < n; ++i) {
- tree[n + i] = a[i];
- }
- for (int i = n - 1; i >= 1; --i) {
- tree[i] = tree[2 * i] + tree[2 * i + 1];
- }
- }
- void set(int i, int v) {
- i += n;
- tree[i] = v;
- i /= 2;
- while (i != 0) {
- tree[i] = tree[2 * i] + tree[2 * i + 1];
- i /= 2;
- }
- }
- int get(int l, int r) {
- l += n;
- r += n;
- int ans = 0;
- while (l <= r) {
- if (l % 2 == 1) {
- ans += tree[l];
- l += 1;
- }
- if (r % 2 == 0) {
- ans += tree[r];
- r -= 1;
- }
- l /= 2;
- r /= 2;
- }
- return ans;
- }
- };
- int32_t main() {
- start;
- int n, m;
- cin >> n >> m;
- vector<int> a(n);
- cin >> a;
- segtree sgt(a);
- while (m--) {
- int op;
- cin >> op;
- if (op == 1) {
- int i, v;
- cin >> i >> v;
- sgt.set(i, v);
- } else {
- int l, r;
- cin >> l >> r;
- cout << sgt.get(l, r - 1) << '\n';
- }
- }
- finish;
- }
Advertisement
Add Comment
Please, Sign In to add comment