結果

問題 No.789 範囲の合計
ユーザー champignon
提出日時 2025-10-12 18:46:35
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 84 ms / 1,000 ms
コード長 3,253 bytes
コンパイル時間 3,244 ms
コンパイル使用メモリ 283,864 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-10-12 18:46:40
合計ジャッジ時間 4,990 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

#define rep(i, l, n) for(int i = int(l); i < int(n); i++)
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

template<class T> bool chmin(T &a, T b) {if(a > b) {a = b; return true;} return false;}
template<class T> bool chmax(T &a, T b) {if(a < b) {a = b; return true;} return false;}
template<class T> using spq = priority_queue<T, vector<T>, greater<T>>;

// bool -> Yes/No
string answer(bool b) {return b ? "Yes" : "No";}

void fix(int k) {cout << fixed << setprecision(k);}

const int inf = 2e9;
const long long INF = 2e18;
const long double eps = 1e-12;
const long double pi = acos(-1);
int dx[] = {0, -1, 0, 1, -1, -1, 1, 1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

template<class S, S(*op)(S, S), S(*e)()>
struct Dynamic_Segment_Tree {
    
public:
    Dynamic_Segment_Tree(int N) : n(N) {}
    void set(int pos, S v) {set(root, pos, v, 0, n);}
    S get(int pos) {return get(root, pos, 0, n);}
    S prod(int l, int r) {return prod(root, l, r, 0, n);}

private:
    int n;
    S unit = e();

    struct node {

        int idx;
        S val, res;
        node *lch, *rch;

        node(int pos, S v) : idx(pos), val(v), res(v), lch(nullptr), rch(nullptr) {}

        void update() {
            res = op(op(lch == nullptr ? e() : lch->res, val), rch == nullptr ? e() : rch->res);
        }

    };

    node *root = nullptr;

    void set(node *&x, int pos, S v, int l, int r) {
        if(x == nullptr) {
            x = new node(pos, v);
            return;
        }
        if(x->idx == pos) {
            x->val = v;
            x->update();
            return;
        }
        int m = (l + r) >> 1;
        if(pos < m) {
            if(pos > x->idx) std::swap(pos, x->idx), std::swap(v, x->val);
            set(x->lch, pos, v, l, m);
        }
        else {
            if(pos < x->idx) std::swap(pos, x->idx), std::swap(v, x->val);
            set(x->rch, pos, v, m, r);
        }
        x->update();
    }

    S get(node *&x, int pos, int l, int r) {
        if(x == nullptr) return unit;
        if(x->idx == pos) return x->val;
        int m = (l + r) >> 1;
        if(pos < m) return get(x->lch, pos, l, m);
        else return get(x->rch, pos, m, r);
    }

    S prod(node *&x, int ql, int qr, int l, int r) {
        if(x == nullptr || qr <= l || r <= ql) return unit;
        if(ql <= l && r <= qr) return x->res;
        int m = (l + r) >> 1;
        S ret = prod(x->lch, ql, qr, l, m);
        if(ql <= x->idx && x->idx < qr) ret = op(ret, x->val);
        return op(ret, prod(x->rch, ql, qr, m, r));
    }

};

using S = long;
S op(S a, S b) {return a + b;}
S e() {return 0l;}

void main_program() {
    int q; cin >> q;
    Dynamic_Segment_Tree<S, op, e> seg(1e9 + 3);
    long ans = 0;
    while(q--) {
        int t; cin >> t;
        if(t) {
            int l, r; cin >> l >> r;
            ans += seg.prod(l, ++r);
        }
        else {
            int pos; long v; cin >> pos >> v;
            seg.set(pos, seg.get(pos) + v);
        }
    }
    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    //cin >> T;
    while(T--) {
        main_program();
    }
}
0