結果

問題 No.789 範囲の合計
ユーザー kappybarkappybar
提出日時 2020-05-08 15:39:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 332 ms / 1,000 ms
コード長 2,397 bytes
コンパイル時間 2,000 ms
コンパイル使用メモリ 183,864 KB
実行使用メモリ 22,800 KB
最終ジャッジ日時 2023-09-16 08:28:33
合計ジャッジ時間 5,603 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 290 ms
21,064 KB
testcase_03 AC 88 ms
11,792 KB
testcase_04 AC 260 ms
20,960 KB
testcase_05 AC 203 ms
21,404 KB
testcase_06 AC 220 ms
21,056 KB
testcase_07 AC 77 ms
11,580 KB
testcase_08 AC 163 ms
15,848 KB
testcase_09 AC 148 ms
15,424 KB
testcase_10 AC 332 ms
22,800 KB
testcase_11 AC 234 ms
20,608 KB
testcase_12 AC 239 ms
20,600 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;


struct SegmentTree {
        private:
        int n;
        vector<ll> node;
        
        public:
        SegmentTree(vector<ll> v){
                int sz = (int)v.size();
                n = 1;
                while(n < sz) n *= 2;
                node.resize(2*n-1,0);
                for(int i=0;i<sz;i++) node[i+n-1] = v[i];
                for(int i=n-2;i>=0;i--) node[i] = node[2*i+1]+node[2*i+2];
        }
        
        //x番目の要素にvalを足す。
        void add(int x,ll val){
                x += n-1;
                node[x] += val;
                while(x > 0){
                        x = (x-1)/2;
                        node[x] = node[2*x+1]+node[2*x+2];
                }
        }
        
        //区間[a,b)のwaを求める
        ll getsum(int a,int b,int k=0,int l=0,int r=-1){
                if(r < 0) r = n;
                if(r <= a || b <= l) return 0;
                if(a <= l && r <= b) return node[k];
                ll vl = getsum(a,b,2*k+1,l,(l+r)/2);
                ll vr = getsum(a,b,2*k+2,(l+r)/2,r);
                return vl+vr;
        }
};

struct query{
    int id;
    ll a,b;
};

int main(){
    int n;
    cin >> n;
    vector<query> querys;
    vector<ll> point;
    rep(i,n){
        int c;
        cin >> c;
        if(c==0){
            ll x,y;
            cin >> x>> y;
            querys.push_back(query{c,x,y});
            point.push_back(x);
        }else{
            ll l,r;
            cin >> l >> r;
            querys.push_back(query{c,l,r});
            point.push_back(l);
            point.push_back(r);
        }
    }
    sort(point.begin(),point.end());
    map<ll,int> trans;
    int j =  0;
    rep(i,point.size()){
        if(i>0){
            if(point[i-1] == point[i]) continue;
        }
        trans[point[i]] = j++;
    }
    SegmentTree seg(vector<ll>(point.size(),0));

    ll ans = 0;
    rep(i,n){
        if(querys[i].id==0){
            seg.add(trans[querys[i].a],querys[i].b);
        }else{
            ans += seg.getsum(trans[querys[i].a],trans[querys[i].b]+1);
        }
    }
    cout << ans << endl;
    return 0;
}
0