#line 2 ".lib/data_structure/bit_sequence/bit_operation.hpp" #include #include constexpr static uint64_t mask_32_64 = 0xFFFFFFFF00000000; constexpr static uint64_t mask_0_32 = 0x00000000FFFFFFFF; constexpr static uint64_t mask_48_64 = 0xFFFF000000000000; constexpr static uint64_t mask_32_48 = 0x0000FFFF00000000; constexpr static uint64_t mask_16_32 = 0x00000000FFFF0000; constexpr static uint64_t mask_0_16 = 0x000000000000FFFF; constexpr static int TABLE_SIZE_LOG = 16, TABLE_SIZE = 1 << TABLE_SIZE_LOG; static std::vector> select_build(){ std::vector> ret(TABLE_SIZE, std::vector(TABLE_SIZE_LOG, -1)); for(int i = 0; i < TABLE_SIZE; i++){ int pcnt = 0; for(int j = 0; j < TABLE_SIZE_LOG; j++) if((i >> j) & 1) ret[i][pcnt++] = j; } return ret; } // k-bit目以降(kも含む)に初めて現れる1の位置, 無い場合は-1 static int find_next_32bit(uint32_t x, int k){ uint32_t b = x >> k; if(!b) return -1; return k + __builtin_ctz(b); } // k-bit目以降(kも含む)に初めて現れる1の位置, 無い場合は-1 static int find_next_64bit(uint64_t x, int k){ uint64_t b = x >> k; if(!b) return -1; return k + __builtin_ctzll(b); } // 0 <= k <= 63 // k-bit目以前(kも含む)に初めて現れる1の位置, 無い場合は-1 static int find_prev_64bit(uint64_t x, int k){ uint64_t b = x << (63 - k); if(!b) return -1; return k - (63 - __builtin_clzll(b)); } // k番目(0-indexed)の1の場所(0-indexed)を返す. 無い場合壊れる static int select_32bit(uint32_t x, int k){ static std::vector> table = select_build(); int r = __builtin_popcount(x & mask_0_16); if(r > k) return table[x & mask_0_16][k]; return 16 + table[(x & mask_16_32) >> 16][k - r]; } // k番目(0-indexed)の1の場所(0-indexed)を返す. 無い場合壊れる static int select_64bit(uint64_t x, int k){ static std::vector> table = select_build(); int r = __builtin_popcount(x & mask_0_32); if(r > k){ int rr = __builtin_popcount(x & mask_0_16); if(rr > k) return table[x & mask_0_16][k]; else return 16 + table[(x & mask_16_32) >> 16][k - rr]; }else{ k -= r; int lr = __builtin_popcountll(x & mask_32_48); if(lr > k) return 32 + table[(x & mask_32_48) >> 32][k]; else return 48 + table[(x & mask_48_64) >> 48][k - lr]; } } // 先頭k_bit(0 <= k <= 32)の1の数 static int rank_32bit(uint32_t x, int k){ return k == 32 ? __builtin_popcount(x) : __builtin_popcount(x & ((1ULL << k) - 1)); } // 先頭k_bit(0 <= k <= 64)の1の数 static int rank_64bit(uint64_t x, int k){ return k == 64 ? __builtin_popcountll(x) : __builtin_popcountll(x & ((1ULL << k) - 1)); } #line 4 "sparse_bit.hpp" #include #include template struct sparse_binary_indexed_tree{ private: using I = int; static constexpr int bitlen = sizeof(I) * 8; // idxの2進数においてk-bitから下位bitに向けて1がいくつ続くか static int count_consecutive_one(I idx, int k){ static_assert(bitlen == 32 || bitlen == 64); if(bitlen == 32) return __builtin_clz(~((uint32_t)idx << (31 - k))); return __builtin_clzll(~((uint64_t)idx << (63 - k))); } static int count_trailing_one(I idx){ static_assert(bitlen == 32 || bitlen == 64); if(bitlen == 32) return __builtin_ctz(~(uint32_t)idx); return __builtin_ctzll(~(uint64_t)idx); } I maxn; int h; struct node{ I idx; std::vector ch; Val val, sum; I ch_flag; node(I idx, Val val): idx(idx), val(val), sum(val){} int find_next_ch(int a){ static_assert(bitlen == 32 || bitlen == 64); if(bitlen == 32) return find_next_32bit(ch_flag, a); return find_next_64bit(ch_flag, a); } }; node *root; void update(node* &v, I idx, Val x, int h_cur){ if(!v){ v = new node(idx, x); return; }else if(v->idx == idx){ v->val += x; v->sum += x; return; }else v->sum += x; if(h_cur == -1 || count_trailing_one(idx) == h_cur + 1) return; // 残りの下位bitが全て1なら終了 int k = count_consecutive_one(idx, h_cur); int t = count_consecutive_one(v->idx, h_cur); bool f = false; if(v->idx > idx){ if(k || t) f = true; }else if(!k && !t) f = true; if(f) std::swap(v->idx, idx), std::swap(v->val, x), std::swap(k, t); if(v->ch.size() <= k) v->ch.resize(k + 1, nullptr); update(v->ch[k], idx, x, h_cur - 1 - k); v->ch_flag |= 1ULL << k; } Val query(node *v, I idx, int h_cur){ if(!v || h_cur == -1) return Val(0); Val res = v->idx < idx ? v->val : Val(0); int k = count_consecutive_one(idx, h_cur); int maxk = std::min(k, (int)v->ch.size()); for(int i = v->find_next_ch(0); i < maxk && i != -1; i = v->find_next_ch(i + 1)) res += v->ch[i]->sum; if((v->ch_flag >> k) & 1) res += query(v->ch[k], idx, h_cur - 1 - k); return res; } public: sparse_binary_indexed_tree(): root(nullptr){} // [0, n) sparse_binary_indexed_tree(I n): root(nullptr){ n = std::max(n + 1, 2); maxn = 1, h = 0; while(maxn < n) maxn <<= 1, h++; } void update(I idx, Val x){ assert(0 <= idx && idx < maxn); update(root, idx, x, h - 1); } Val query(I r){ assert(0 <= r && r <= maxn); return query(root, r, h - 1); } Val query(I l, I r){ assert(0 <= l && l <= r && r <= maxn); if(l == r) return Val(0); return query(root, r, h - 1) - query(root, l, h - 1); } }; #line 2 ".lib/template.hpp" #include #include #line 5 ".lib/template.hpp" #include #include #include #include #include #line 11 ".lib/template.hpp" #include #include #include #include #include #include #include #line 19 ".lib/template.hpp" #include #include #include #include #include #define allof(obj) (obj).begin(), (obj).end() #define range(i, l, r) for(int i=l;i>1)|y_bit)) #define bit_kth(i, k) ((i >> k)&1) #define bit_highest(i) (i?63-__builtin_clzll(i):-1) #define bit_lowest(i) (i?__builtin_ctzll(i):-1) using ll = long long; using ld = long double; using ul = uint64_t; using pi = std::pair; using pl = std::pair; template using vec = std::vector; using namespace std; template std::ostream &operator<<(std::ostream &dest, const std::pair &p){ dest << p.first << ' ' << p.second; return dest; } template std::ostream &operator<<(std::ostream &dest, const std::vector> &v){ int sz = v.size(); if(sz==0) return dest; for(int i=0;i std::ostream &operator<<(std::ostream &dest, const std::vector &v){ int sz = v.size(); if(sz==0) return dest; for(int i=0;i std::ostream &operator<<(std::ostream &dest, const std::array &v){ if(sz==0) return dest; for(int i=0;i std::ostream &operator<<(std::ostream &dest, const std::set &v){ for(auto itr=v.begin();itr!=v.end();){ dest << *itr; itr++; if(itr!=v.end()) dest << ' '; } return dest; } template std::ostream &operator<<(std::ostream &dest, const std::map &v){ for(auto itr=v.begin();itr!=v.end();){ dest << '(' << itr->first << ", " << itr->second << ')'; itr++; if(itr!=v.end()) dest << '\n'; } return dest; } template vector make_vec(size_t sz, T val){return std::vector(sz, val);} template auto make_vec(size_t sz, Tail ...tail){ return std::vector(tail...))>(sz, make_vec(tail...)); } template vector read_vec(size_t sz){ std::vector v(sz); for(int i=0;i> v[i]; return v; } template auto read_vec(size_t sz, Tail ...tail){ auto v = std::vector(tail...))>(sz); for(int i=0;i(tail...); return v; } void io_init(){ std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } #line 3 "a.cpp" int main(){ std::cin.tie(nullptr); std::ios::sync_with_stdio(false); int n; std::cin >> n; sparse_binary_indexed_tree t(1000000001); long long ans = 0; for(int i = 0; i < n; i++){ int a, b, c; std::cin >> a >> b >> c; if(!a){ t.update(b, c); }else{ ans += t.query(b, c + 1); } } std::cout << ans << '\n'; }