結果

問題 No.789 範囲の合計
ユーザー koprickykopricky
提出日時 2019-02-08 22:15:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 118 ms / 1,000 ms
コード長 4,810 bytes
コンパイル時間 1,449 ms
コンパイル使用メモリ 157,028 KB
実行使用メモリ 97,288 KB
最終ジャッジ日時 2023-09-14 03:44:04
合計ジャッジ時間 3,834 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
97,072 KB
testcase_01 AC 30 ms
97,048 KB
testcase_02 AC 110 ms
97,288 KB
testcase_03 AC 71 ms
97,140 KB
testcase_04 AC 118 ms
97,092 KB
testcase_05 AC 91 ms
97,052 KB
testcase_06 AC 94 ms
97,172 KB
testcase_07 AC 65 ms
97,184 KB
testcase_08 AC 86 ms
97,052 KB
testcase_09 AC 83 ms
97,072 KB
testcase_10 AC 108 ms
97,092 KB
testcase_11 AC 77 ms
97,144 KB
testcase_12 AC 77 ms
97,032 KB
testcase_13 AC 31 ms
97,064 KB
testcase_14 AC 30 ms
97,068 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

template<typename T> class dynamic_segtree{
private:
    struct node {
    	T val, lazy;
        int st_size;
    	node *par, *left, *right;
    	node() : val(id2), lazy(id1), st_size(0),
            par(nullptr), left(nullptr), right(nullptr){}
    };
    void opr1(T& arg1,T arg2){
        arg1 += arg2;
    }
    T opr2(T arg1,T arg2){
        return arg1 + arg2;
    }
    static const int POOL_SIZE = 2000000;
    static const int MAX_SIZE = 1000000001;
    static node *root;
    static node pool[POOL_SIZE];
    static const T id1 = (T)0;
    static const T id2 = (T)0;
    int nw_size;
    node *alloc(){
        assert(nw_size < POOL_SIZE);
        return (&pool[nw_size++]);
    }
    void eval(node* k, int l, int r){
        if(k->lazy != id1){
            opr1(k->val,k->lazy);
            if(r-l>1){
                if(k->left){
                    opr1(k->left->lazy, k->lazy / k->st_size * k->left->st_size);
                }
                if(k->right){
                    opr1(k->right->lazy, k->lazy / k->st_size * k->right->st_size);
                }
            }
            k->lazy = id1;
        }
    }
public:
    dynamic_segtree(){ nw_size = 0; root = alloc(); }
    //a番目にxを追加
    void insert(int a, T x, node *k = root, int l = 0, int r = MAX_SIZE){
        eval(k, l, r);
        k->st_size++;
        k->val = opr2(k->val,x);
        if(r - l == 1){
            return;
        }
        if(a < (l + r) / 2){
            if(!(k->left)){
                k->left = alloc();
                k->left->par = k;
            }
            insert(a, x, k->left, l, (l+r)/2);
        }else{
            if(!(k->right)){
                k->right = alloc();
                k->right->par = k;
            }
            insert(a, x, k->right, (l+r)/2, r);
        }
    }
    void range(int a, int b, const T x, node *k = root, int l = 0, int r = MAX_SIZE){
        eval(k, l, r);
    	if(b <= l || r <= a) return;
    	if(a <= l && r <= b){
            opr1(k->lazy, k->st_size * x);
    		eval(k, l, r);
    		return;
    	}
        k->val = id2;
    	if(k->left){
            range(a, b, x, k->left, l, (l+r)/2);
            k->val = opr2(k->val,k->left->val);
    	}
        if(k->right){
            range(a, b, x, k->right, (l+r)/2, r);
            k->val = opr2(k->val,k->right->val);
        }
    }
    T query(int a, int b, node *k = root, int l=0, int r = MAX_SIZE){
        eval(k, l, r);
        if(b <= l || r <= a){
            return id2;
        }
        if(a <= l && r <= b){
            return k->val;
        }
        T vl = id2,vr = id2;
        if(k->left){
            vl = query(a, b, k->left, l, (l+r)/2);
        }
        if(k->right){
            vr = query(a, b, k->right, (l+r)/2, r);
        }
        return opr2(vl, vr);
    }
};
template<typename T> class dynamic_segtree<T>::node* dynamic_segtree<T>::root;
template<typename T> class dynamic_segtree<T>::node dynamic_segtree<T>::pool[POOL_SIZE];

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    dynamic_segtree<ll> ds;
    set<int> st;
    ll ans = 0;
    rep(i,n){
        int a,b,c;
        cin >> a >> b >> c;
        if(a == 0){
            if(st.find(b) == st.end()){
                ds.insert(b, c);
            }else{
                ds.range(b, b+1, c);
            }
        }else{
            ans += ds.query(b, c+1);
        }
    }
    cout << ans << "\n";
    return 0;
}
0