Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define pii pair<int,int>
- #define pb push_back
- #define IOS ios_base::sync_with_stdio(NULL);cin.tie(0);cout.tie(0);
- #define f first
- #define s second
- #define mp make_pair
- #define ll long long
- using namespace std;
- const int MAXN=1e5+10;
- const ll INF=1e18 + 7;
- const int MOD=1e9+7;
- int t[MAXN * 4], a[MAXN];
- int n;
- void build(int v, int tl, int tr){
- if (tl > tr){
- return;
- }
- if (tl == tr){
- t[v] = a[tl];
- return;
- }
- int tm = (tl + tr) >> 1;
- build((v << 1), tl, tm);
- build((v << 1) | 1, tm + 1, tr);
- t[v] = t[(v << 1)] + t[((v << 1) | 1)];
- }
- int sum(int v, int tl, int tr, int l, int r){
- if (l > r){
- return 0;
- }
- if (tl == l && tr == r){
- return t[v];
- }
- int tm = (tl + tr) >> 1;
- return sum((v << 1), tl, tm, l, min(r, tm))
- + sum(((v << 1) | 1), tm + 1, tr, max(l, tm + 1), r);
- }
- void update(int v, int tl, int tr, int pos, int val){
- if (tl > tr){
- return;
- }
- if (tl == tr){
- t[v] = val;
- return;
- }
- int tm = (tl + tr) >> 1;
- if (pos <= tm){
- update((v << 1), tl, tm, pos, val);
- }
- else{
- update(((v << 1) | 1), tm + 1, tr, pos, val);
- }
- t[v] = t[(v << 1)] + t[((v << 1) | 1)];
- }
- void solve(){
- cin >> n;
- for (int i = 1; i <= n; i++){
- cin >> a[i];
- }
- build(1, 1, n);
- int q;
- cin >> q;
- for (int i = 1; i <= q; i++){
- int type;
- cin >> type;
- if (type == 1){
- int l, r;
- cin >> l >> r;
- cout << sum(1, 1, n, l, r) << '\n';
- }
- else{
- int pos, val;
- cin >> pos >> val;
- update(1, 1, n, pos, val);
- }
- }
- }
- int main(){
- //IOS;
- int ttt = 1;
- //cin >> ttt;
- while (ttt--){
- solve();
- }
- }
- //Vertex
- //Academy203!
Advertisement
Add Comment
Please, Sign In to add comment