Submission

Status:
xP------------------

Score: 5

User: Dormon

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

Language: cpp

Time: 0.081 second

Submitted On: 2024-11-15 21:51:41

#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(){
    int n; scanf("%d", &n);
    vector<int> a(n, 0), fw(n, 0);
    
    auto upd = [&](int i, int val) -> void {
        for (;i < n;i+=i&-i) fw[i] += val;
    };

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

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