Submission #51989
ソースコード
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; struct SegmentTree { private : vector<i64> node; i64 size; public : SegmentTree(i64 n, i64 m) { size = 1; while (size < n) size *= 2; node.resize(2 * size - 1, m); } void add(i64 i, i64 x) { i += size - 1; node[i] += x; while (i > 0) { i = (i - 1) / 2; node[i] = node[i*2+1] + node[i*2+2]; } } i64 getsum(i64 begin, i64 end, i64 pos = 0, i64 left = 0, i64 right = -1) { if (right < 0) right = size; if (right <= begin || end <= left) return 0; if (begin <= left && right <= end) return node[pos]; i64 vl = getsum(begin, end, 2 * pos + 1, left, (left + right) / 2); i64 vr = getsum(begin, end, 2 * pos + 2, (left + right) / 2, right); return vl + vr; } }; signed main() { ios::sync_with_stdio( false ); cin.tie(nullptr); i64 n, q; cin >> n >> q; SegmentTree seg(n, 0); rep(i, q) { i64 com, x, y; cin >> com >> x >> y; if (!com) seg.add(x - 1, y); else cout << seg.getsum(x - 1, y) << "\n" ; } return 0; } |
ステータス
項目 | データ |
---|---|
問題 | 0649 - 区間和(セグ木、BIT練習) |
ユーザー名 | もけ |
投稿日時 | 2019-07-27 11:45:05 |
言語 | C++14 |
状態 | Accepted |
得点 | 5 |
ソースコード長 | 1611 Byte |
最大実行時間 | 342 ms |
最大メモリ使用量 | 118884 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 5 / 5 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
input100-1 | AC | 54 ms | 1628 KB |
1
|
input100-2 | AC | 77 ms | 4136 KB |
1
|
input100-3 | AC | 106 ms | 9964 KB |
1
|
input100-4 | AC | 204 ms | 21944 KB |
1
|
input100-5 | AC | 263 ms | 29948 KB |
1
|
input1000-1 | AC | 103 ms | 32072 KB |
1
|
input1000-2 | AC | 208 ms | 37120 KB |
1
|
input1000-3 | AC | 97 ms | 39352 KB |
1
|
input1000-4 | AC | 243 ms | 45676 KB |
1
|
input1000-5 | AC | 87 ms | 47532 KB |
1
|
input10000-1 | AC | 249 ms | 53608 KB |
1
|
input10000-2 | AC | 266 ms | 59948 KB |
1
|
input10000-3 | AC | 222 ms | 64756 KB |
1
|
input10000-4 | AC | 255 ms | 70336 KB |
1
|
input10000-5 | AC | 138 ms | 73100 KB |
1
|
input100000-1 | AC | 326 ms | 81364 KB |
1
|
input100000-2 | AC | 57 ms | 82332 KB |
1
|
input100000-3 | AC | 65 ms | 83296 KB |
1
|
input100000-4 | AC | 179 ms | 86568 KB |
1
|
input100000-5 | AC | 287 ms | 92280 KB |
1
|
input1000000-1 | AC | 65 ms | 107200 KB |
1
|
input1000000-2 | AC | 179 ms | 109448 KB |
1
|
input1000000-3 | AC | 170 ms | 111824 KB |
1
|
input1000000-4 | AC | 342 ms | 116760 KB |
1
|
input1000000-5 | AC | 162 ms | 118884 KB |
1
|
input1001000-1 | AC | 23 ms | 102568 KB |
1
|
input1001000-2 | AC | 21 ms | 102512 KB |
1
|
input1001000-3 | AC | 25 ms | 102712 KB |
1
|
input1001000-4 | AC | 21 ms | 102652 KB |
1
|
input1001000-5 | AC | 24 ms | 102600 KB |
1
|