結果

問題 No.789 範囲の合計
ユーザー tempura_pptempura_pp
提出日時 2019-02-08 22:07:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 189 ms / 1,000 ms
コード長 2,096 bytes
コンパイル時間 993 ms
コンパイル使用メモリ 101,636 KB
実行使用メモリ 34,624 KB
最終ジャッジ日時 2023-09-14 03:41:55
合計ジャッジ時間 3,607 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 183 ms
28,628 KB
testcase_03 AC 79 ms
4,376 KB
testcase_04 AC 189 ms
31,772 KB
testcase_05 AC 161 ms
27,456 KB
testcase_06 AC 168 ms
28,660 KB
testcase_07 AC 74 ms
4,376 KB
testcase_08 AC 172 ms
34,624 KB
testcase_09 AC 159 ms
31,824 KB
testcase_10 AC 162 ms
20,020 KB
testcase_11 AC 144 ms
28,172 KB
testcase_12 AC 145 ms
28,200 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

template<typename T>
struct SegmentTree{
    struct Node{
        T val;
        Node *lch,*rch;
        Node(T val):val(val),lch(nullptr),rch(nullptr){}
    };

    Node *root;
    ll sz;
    T id;

    T f(T x,T y){
        //return max(x,y);
        //return min(x,y);
        return x+y;
        //return {x.first*y.first, y.first*x.second + y.second};
    }

    T g(T x,T y){
        //return x;
        return x+y;
    }

    T val(Node* t){return t ? t->val : id;}

    SegmentTree(ll sz_,T id_=0):root(nullptr),id(id_){
        sz=1;
        while(sz<sz_)sz*=2;
    }

    Node* update(Node* t,ll k, ll l, ll r, T x){
        //assert(l<=k&&k<r);
        if(!t)t=new Node(id);
        if(r-l==1){
            t->val=g(x,t->val);
            return t;
        }
        if(k<(l+r)/2)t->lch=update(t->lch,k, l, (l+r)>>1, x);
        else t->rch=update(t->rch,k, (l+r)>>1, r, x);
        t->val=f(val(t->lch), val(t->rch));
        return t;
    }

    T get(Node* t, ll a,ll b,ll l, ll r){
        if(!t)return id;
        if(b<=l||r<=a)return id;
        if(a<=l&&r<=b)return t->val;
        T lx=get(t->lch, a, b, l, (l+r)>>1);
        T rx=get(t->rch, a, b, (l+r)>>1, r);
        return f(lx,rx);
    }

    void update(ll k,T x){
        root = update(root, k, 0, sz, x);
    }

    T get(ll a, ll b){
        return get(root, a, b, 0, sz);
    }
};

typedef pair<double,double> P;
int main(){
	int n;
	cin>>n;
	SegmentTree<ll> sg(inf);
	ll ans=0;
	rep(i,n){
		int a,b,c;
		cin>>a>>b>>c;
		if(a){
			ans+=sg.get(b,c+1);
		}
		else {
			sg.update(b,c);
		}
	}
	cout<<ans<<endl;
    return 0;
}
0