Submission

Status:
PPPPPPPPPPPPPPPPPPPP

Score: 100

User: Dormon

Problemset: ผลบวก (ยาก)

Language: cpp

Time: 0.060 second

Submitted On: 2024-11-15 22:12:02

#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <functional>

#define debug(...) Debug(#__VA_ARGS__, __VA_ARGS__)
using namespace std;

template<typename T>
typename std::enable_if<std::is_integral<T>::value>::type
Debug(const char* name, T value) {
    std::cout << name << " : " << value << '\n';
}

template<typename T, typename... Args>
typename std::enable_if<std::is_integral<T>::value>::type
Debug(const char* names, T value, Args... args) {
    const char* comma = strchr(names, ',');
    std::cout.write(names, comma - names) << " : " << value << " | ";
    Debug(comma + 1, args...);
}

void solve(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    int n; cin >> n;
    vector<int> fw(n+2, 0), a(n+2, 0);
    
    auto upd = [&](int i, int val) -> void {
        for (;i <= n;i+=i&-i) fw[i] += val;
    };

    for (int i = 1;i <= n;i++){
        cin >> a[i];
        upd(i, a[i]);
    }
    int q; cin >> q;
    while (q--){
        int t; cin >> t;
        while (t--){
            int i, b; cin >> i >> b; i++;
            upd(i, b - a[i]);
            a[i] = b;
        }
        int l, r; cin >> l >> r; l++, r++;
        auto qry = [&](int i, int res = 0) -> int{
            for (;i;i-=i&-i) res += fw[i]; return res;
        };
        cout << qry(r) - qry(l-1) << '\n';
    }

}

int main()
{
    int q = 1; 
    //scanf("%d", &q);
    while (q--){
        solve();
    }
}