Submission #52073
ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | #include <bits/stdc++.h> using namespace std; #define all(x) x.begin(), x.end() #define rep(i, n) for (int i = 0; i < n; ++i) #define reps(i, m, n) for (int i = m; i <= n; ++i) using i64 = long long ; using pii = pair<i64, i64>; template < class A, class B> inline bool chmax(A &a, const B &b){ return b > a ? a = b,1 : 0;} template < class A, class B> inline bool chmin(A &a, const B &b){ return b < a ? a = b,1 : 0;} constexpr int INF = 0x3f3f3f3f; constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr int mod = int (1e9) + 7; #define int i64 template < typename Monoid> struct SegmentTree { using Func = function<Monoid(Monoid, Monoid)>; const int sz; const Func fn; const Monoid unity; vector<Monoid> seg; SegmentTree( int n, const Monoid &u, Func f) : sz(1 << (__lg(n + 5) + 1)), fn(f), unity(u), seg(sz * 2, unity) {} void set( int k, const Monoid &v) { seg[k+sz] = v; } Monoid& operator[]( int k) { return seg[k+sz]; } const Monoid& operator[]( int k) const { return seg[k+sz]; } void build() { for ( int k = sz - 1; k > 0; --k) seg[k] = fn(seg[2*k], seg[2*k+1]); } void update( int k, const Monoid &x) { k += sz; seg[k] += x; while (k >>= 1) seg[k] = fn(seg[2*k], seg[2*k+1]); } Monoid query( int a, int b) const { Monoid L = unity, R = unity; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = fn(L, seg[a++]); if (b & 1) R = fn(seg[--b], R); } return fn(L, R); } }; signed main() { ios::sync_with_stdio( false ); cin.tie(nullptr); int N, Q; cin >> N >> Q; SegmentTree< int > seg(N, 0, []( int a, int b) { return a + b; }); rep(i, Q) { int s, a, b; cin >> s >> a >> b; if (!s) seg.update(a - 1, b); else cout << seg.query(a - 1, b) << "\n" ; } return 0; } |
ステータス
項目 | データ |
---|---|
問題 | 0649 - 区間和(セグ木、BIT練習) |
ユーザー名 | もけ |
投稿日時 | 2019-07-29 23:37:19 |
言語 | C++14 |
状態 | Accepted |
得点 | 5 |
ソースコード長 | 1833 Byte |
最大実行時間 | 257 ms |
最大メモリ使用量 | 104912 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 5 / 5 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
input100-1 | AC | 36 ms | 988 KB |
1
|
input100-2 | AC | 51 ms | 1828 KB |
1
|
input100-3 | AC | 80 ms | 4076 KB |
1
|
input100-4 | AC | 151 ms | 8884 KB |
1
|
input100-5 | AC | 257 ms | 15996 KB |
1
|
input1000-1 | AC | 82 ms | 18112 KB |
1
|
input1000-2 | AC | 176 ms | 23160 KB |
1
|
input1000-3 | AC | 89 ms | 25400 KB |
1
|
input1000-4 | AC | 218 ms | 31732 KB |
1
|
input1000-5 | AC | 84 ms | 33708 KB |
1
|
input10000-1 | AC | 219 ms | 39776 KB |
1
|
input10000-2 | AC | 239 ms | 45992 KB |
1
|
input10000-3 | AC | 192 ms | 50800 KB |
1
|
input10000-4 | AC | 217 ms | 56376 KB |
1
|
input10000-5 | AC | 116 ms | 59264 KB |
1
|
input100000-1 | AC | 254 ms | 67268 KB |
1
|
input100000-2 | AC | 61 ms | 68240 KB |
1
|
input100000-3 | AC | 62 ms | 69208 KB |
1
|
input100000-4 | AC | 156 ms | 72740 KB |
1
|
input100000-5 | AC | 223 ms | 78316 KB |
1
|
input1000000-1 | AC | 54 ms | 93232 KB |
1
|
input1000000-2 | AC | 126 ms | 95352 KB |
1
|
input1000000-3 | AC | 155 ms | 97984 KB |
1
|
input1000000-4 | AC | 253 ms | 102924 KB |
1
|
input1000000-5 | AC | 138 ms | 104912 KB |
1
|
input1001000-1 | AC | 28 ms | 88600 KB |
1
|
input1001000-2 | AC | 20 ms | 88548 KB |
1
|
input1001000-3 | AC | 17 ms | 88488 KB |
1
|
input1001000-4 | AC | 18 ms | 88560 KB |
1
|
input1001000-5 | AC | 21 ms | 88636 KB |
1
|