Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define endl "\n"
- using namespace std;
- using ll = long long;
- using pii = pair<int, int>;
- constexpr int N = 5e6+1;
- constexpr int MOD = 1e9+7;
- constexpr int INF = 1e9+9;
- int tr[4*N], n, arr[N], q, ql, qr;
- char type;
- void build(int id = 0, int l = 0, int r = n)
- {
- if (l==r)
- return;
- if (l+1==r)
- {
- tr[id]=arr[l];
- return;
- }
- int mid = (r+l)/2;
- build(2*id+1, l, mid);
- build(2*id+2, mid, r);
- tr[id]+=tr[2*id+1]+tr[2*id+2];
- }
- void upd(int val, int i, int id = 0, int l = 0, int r = n)
- {
- if (l==r)
- return;
- if (l+1==r)
- {
- tr[id]+=val;
- return;
- }
- tr[id]+=val;
- int mid = (l+r)/2;
- if (i<mid)
- upd(val, i, 2*id+1, l, mid);
- else upd(val, i, 2*id+2, mid, r);
- }
- void update(int i, int val)
- {
- i--;
- int temp = val-arr[i];
- arr[i]=val;
- val = temp;
- upd(val, i);
- }
- int get(int l, int r, int id = 0, int cl = 0, int cr = n)
- {
- if (l>cr||r<cl||l==r)
- return 0;
- if (l==cl&&r==cr)
- {
- return tr[id];
- }
- int mid = (cl+cr)/2;
- return get(max(l, cl), min(r, mid), 2*id+1, cl, mid)+
- get(max(l, mid), min(r, cr), 2*id+2, mid, cr);
- }
- int get_sum(int l, int r)
- {
- l--;
- return get(l, r);
- }
- int close(int n)
- {
- int res = 1;
- while(res<n)
- {
- res<<=1;
- }
- return res;
- }
- void Solve()
- {
- cin >> n >> q;
- for (int i = 0; i < n; i++)
- cin >> arr[i];
- n=close(n);
- build();
- while(q--)
- {
- cin >> type >> ql >> qr;
- if (type=='=')
- update(ql, qr);
- else cout << get_sum(ql, qr) << endl;
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- Solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement