compro-library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ningenMe/compro-library

:heavy_check_mark: test/binary-indexed-tree/DynamicBinaryIndexedTree-rsqraq.test.cpp

Depends on

Code

#define PROBLEM "https://yukicoder.me/problems/no/789"

#include <vector>
#include <iostream>
#include <cassert>
#include <unordered_map>
using namespace std;
#include "../../lib/99-operator/abel/AbelPrefixSumPointAdd.cpp"
#include "../../lib/11-binary-indexed-tree/DynamicBinaryIndexedTree.cpp"

int main(void){
    cin.tie(0);ios::sync_with_stdio(false);
    DynamicBinaryIndexedTree<AbelPrefixSumPointAdd<long long>> seg(1000000010);
    int N; cin >> N;
    long long ans = 0;
    while(N--) {
        int q,l,r; cin >> q >> l >> r;
        if(q==0) seg.operate(l,r);
        else ans += seg.fold(l,r+1);
    }
    cout << ans << endl;
    return 0; 
}
#line 1 "test/binary-indexed-tree/DynamicBinaryIndexedTree-rsqraq.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/789"

#include <vector>
#include <iostream>
#include <cassert>
#include <unordered_map>
using namespace std;
#line 1 "lib/99-operator/abel/AbelPrefixSumPointAdd.cpp"
/*
 * @title AbelPrefixSumPointAdd
 * @docs md/operator/abel/AbelPrefixSumPointAdd.md
 */
template<class T> struct AbelPrefixSumPointAdd {
    using TypeNode = T;
    inline static constexpr TypeNode unit_node = 0;
    inline static constexpr TypeNode func_fold(const TypeNode& l,const TypeNode& r){return l+r;}
    inline static constexpr TypeNode func_fold_inv(const TypeNode& l,const TypeNode& r){return l-r;}
};
#line 1 "lib/11-binary-indexed-tree/DynamicBinaryIndexedTree.cpp"
/*
 * @title DynamicBinaryIndexedTree - 動的BIT
 * @docs md/binary-indexed-tree/DynamicBinaryIndexedTree.md
 */
template<class Abel> class DynamicBinaryIndexedTree {
    using TypeNode = typename Abel::TypeNode;
    using i64 = long long;
    i64 length;

    unordered_map<i64,TypeNode> node;
public:

    //[0,N) constructed, inplace [0,1) + [1,N+1)
    //you can ignore inplace offset
    DynamicBinaryIndexedTree(const i64 num) {
        for (length = 1; length < num; length *= 2);
    }

    //[idx,idx+1) operate
    void operate(i64 idx, TypeNode var) {
        assert(0 <= idx && idx < length);
        for (++idx; idx <= length; idx += idx & -idx) node[idx] = Abel::func_fold(node[idx],var);
    }

    //[0,idx) fold
    TypeNode fold(i64 idx) {
        TypeNode ret = Abel::unit_node;
        for (idx = min(length,idx); idx > 0; idx -= idx & -idx) ret = Abel::func_fold(ret,node[idx]);
        return ret;
    }

    //[l,r) fold
    TypeNode fold(i64 l, i64 r) {
        return Abel::func_fold_inv(fold(r),fold(l));
    }

    void print() {
        cout << "{ " << fold(1);
        for(int i = 1; i < length; ++i) cout << ", " << fold(i+1);
        cout << " }" << endl;
    }
};
#line 10 "test/binary-indexed-tree/DynamicBinaryIndexedTree-rsqraq.test.cpp"

int main(void){
    cin.tie(0);ios::sync_with_stdio(false);
    DynamicBinaryIndexedTree<AbelPrefixSumPointAdd<long long>> seg(1000000010);
    int N; cin >> N;
    long long ans = 0;
    while(N--) {
        int q,l,r; cin >> q >> l >> r;
        if(q==0) seg.operate(l,r);
        else ans += seg.fold(l,r+1);
    }
    cout << ans << endl;
    return 0; 
}
Back to top page