結果
問題 | No.789 範囲の合計 |
ユーザー |
![]() |
提出日時 | 2025-04-28 04:32:29 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 142 ms / 1,000 ms |
コード長 | 4,453 bytes |
コンパイル時間 | 4,198 ms |
コンパイル使用メモリ | 287,172 KB |
実行使用メモリ | 36,304 KB |
最終ジャッジ日時 | 2025-04-28 04:32:36 |
合計ジャッジ時間 | 6,311 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 15 |
ソースコード
#include <bits/stdc++.h> using namespace std; #define ALL(x) (x).begin(),(x).end() #define IO ios::sync_with_stdio(false),cin.tie(nullptr); #define LB(v, x) (ll)(lower_bound(ALL(v),x)-(v).begin()) #define UQ(v) sort(ALL(v)),(v).erase(unique(ALL(v)),v.end()) #define REP(i, n) for(ll i=0; i<(ll)(n); i++) #define FOR(i, a, b) for(ll i=(ll)(a); (a)<(b) ? i<(b) : i>(b); i+=((a)<(b) ? 1 : -1)) #define chmax(a, b) ((a)<(b) ? ((a)=(b), 1) : 0) #define chmin(a, b) ((a)>(b) ? ((a)=(b), 1) : 0) template<typename T> using rpriority_queue=priority_queue<T,vector<T>,greater<T>>; using ll=long long; const int INF=1e9+10; const ll INFL=4e18; using ld=long double; using ull=uint64_t; using LL=__int128_t; using VI=vector<int>; using VVI=vector<VI>; using VL=vector<ll>; using VVL=vector<VL>; using PL=pair<ll,ll>; using VP=vector<PL>; using WG=vector<vector<pair<int,ll>>>; template<typename Monoid> struct SegTreeDyanamic { using Type=typename Monoid::Type; struct Node { Type value; array<int,2> to; ll left,right; Node(ll l, ll r) { to.fill(-1); left=l; right=r; } }; vector<Node> node; ll mx=1e9; vector<int> route; SegTreeDyanamic(ll mx=1e9, int q=5e5) { this->mx=mx; node.reserve(q); node.push_back(Node(0,mx)); route.reserve(100); } void set(ll i, Type v) { ll left=0,right=mx,cur=0; route.clear(); while(left<right-1) { ll mid=(left+right)/2; int nxt,toi; if(i<mid) nxt=node[cur].to[0],toi=0; //左 else nxt=node[cur].to[1],toi=1; //右 if(nxt==-1) { nxt=node.size(); node[cur].to[toi]=nxt; if(toi==0) node.push_back(Node(left,mid)); else node.push_back(Node(mid,right)); } if(i<mid) right=mid; else left=mid; route.push_back(cur); cur=nxt; } reverse(ALL(route)); node[cur].value=v; for(int r:route) { int leftc=node[r].to[0],rightc=node[r].to[1]; Type leftv= leftc==-1 ? Monoid::id() : node[leftc].value; Type rightv= rightc==-1 ? Monoid::id() : node[rightc].value; node[r].value=Monoid::op(leftv,rightv); } } Type fold(ll l, ll r) { auto dfs=[&](auto&& dfs, int idx, ll left, ll right)-> Type { if(right<l) return Monoid::id(); if(left>r) return Monoid::id(); if(l<=left&&right<=r) return node[idx].value; ll mid=(left+right)/2; int leftc=node[idx].to[0],rightc=node[idx].to[1]; Type leftv,rightv; if(leftc==-1) leftv=Monoid::id(); else leftv=dfs(dfs,leftc,left,mid); if(rightc==-1) rightv=Monoid::id(); else rightv=dfs(dfs,rightc,mid,right); return Monoid::op(leftv,rightv); }; return dfs(dfs,0,0,mx); } }; /// @brief モノイド namespace Monoid { /// @brief Minモノイド /// @tparam max_value 単位元 template<typename T, T max_value=INF> struct Min { using Type=T; static Type id() { return max_value; } static Type op(const Type& a, const Type& b) { return min(a,b); } }; /// @brief Maxモノイド /// @tparam min_value 単位元 template<typename T, T min_value=-INF> struct Max { using Type=T; static Type id() { return min_value; } static Type op(const Type& a, const Type& b) { return max(a,b); } }; /// @brief 和 template<typename T> struct Sum { using Type=T; static Type id() { return 0; } static Type op(const Type& a, const Type& b) { return a+b; } }; /// @brief (和,区間の長さ) template<typename T> struct SumPair { using Type=pair<T,int>; static Type id() { return make_pair(T(0),0); } static Type op(const Type& a, const Type& b) { return {a.first+b.first,a.second+b.second}; } }; } //---------------------------------------------------------- int main() { IO; int Q; cin>>Q; const int N=1e9+10; SegTreeDyanamic<Monoid::Sum<ll>> seg(N); ll ans=0; while(Q--) { int t; cin>>t; if(t==0) { int x,y; cin>>x>>y; seg.set(x,seg.fold(x,x+1)+y); } else { int l,r; cin>>l>>r; r++; ans+=seg.fold(l,r); } } cout<<ans<<endl; }