結果
| 問題 |
No.789 範囲の合計
|
| コンテスト | |
| ユーザー |
polyomino_24
|
| 提出日時 | 2019-02-09 00:20:39 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 187 ms / 1,000 ms |
| コード長 | 3,468 bytes |
| コンパイル時間 | 1,176 ms |
| コンパイル使用メモリ | 128,008 KB |
| 最終ジャッジ日時 | 2025-01-06 21:04:08 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 15 |
ソースコード
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define show(x) cout << #x << " = " << (x) << endl;
using namespace std;
using ll = long long;
using pii = pair<int,int>;
template<typename T> class dynamic_segtree{
private:
struct node {
T val;
node *par, *left, *right;
node() : val(identity), par(nullptr), left(nullptr), right(nullptr){}
};
void opr1(T& arg1,T arg2){
arg1 += arg2;
}
T opr2(T arg1,T arg2){
return arg1+arg2;
}
int nw_size;
static const int POOL_SIZE = 2000000;
static const int MAX_SIZE = 1000000001;
static node *root;
static node pool[POOL_SIZE];
static const T identity = (T)0;
node *alloc(){
assert(nw_size < POOL_SIZE);
return (&pool[nw_size++]);
}
public:
dynamic_segtree() : nw_size(0){ root = alloc(); }
void insert(int a, T x, node *k = root, int l = 0, int r = MAX_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 update(int a, T x, node *k = root, int l = 0, int r = MAX_SIZE){
if(r - l == 1){
opr1(k->val,x);
while(k->par){
k = k->par;
k->val = identity;
if(k->left){
k->val = opr2(k->val,k->left->val);
}
if(k->right){
k->val = opr2(k->val,k->right->val);
}
}
return;
}
if(a < (l + r) / 2){
update(a, x, k->left, l, (l+r)/2);
}else{
update(a, x, k->right, (l+r)/2, r);
}
}
T query(int a, int b, node *k = root, int l=0, int r = MAX_SIZE){
if(b <= l || r <= a){
return identity;
}
if(a <= l && r <= b){
return k->val;
}
T vl = identity,vr = identity;
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(){
dynamic_segtree<ll> seg;
int q;
cin >> q;
set<int>st;
ll ans = 0;
while(q--){
int a,b,c;
cin >> a >> b >> c;
if(a){
ans += seg.query(b,c+1);
}else{
if(st.count(b)){
seg.update(b,c);
}else{
st.insert(b);
seg.insert(b,c);
}
}
}
cout << ans << endl;
}
polyomino_24