Submission #61178
ソースコード
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | #include <bits/stdc++.h> //{ START using namespace std; #define int int64_t #define rep(i, a, n) for (int i = (a); i < (n); ++i) #define reps(i, a, n) for (int i = (a); i > (n); --i) #define arep(i, x) for (auto &&i : (x)) #define irep(i, x) for (auto i = (x).begin(); i != (x).end(); ++i) #define rirep(i, x) for (auto i = (x).rbegin(); i != (x).rend(); ++i) //降順はgreater<T>() #define all(x) (x).begin(), (x).end() #define rv(s) reverse((s).begin(), (s).end()) // gcd lcmはそのままok #define gcd(a, b) __gcd(a, b) #define bits(n) (1LL << (n)) #define pcnt(x) __builtin_popcountll(x) //配列内等要素削除 #define unique(x) (x).erase(unique((x).begin(), (x).end()), (x).end()) #define Fixed(n) fixed << setprecision(n) //総和 #define sowa(n) (((n) * ((n) + 1)) / 2) #define cauto const auto & using P = pair< int , int >; using Graph = vector<vector<P>>; template < class T> //昇順 using min_heap = priority_queue<T, vector<T>, greater<T>>; template < class T> //降順 using max_heap = priority_queue<T>; template < class A, class B> using umap = unordered_map<A, B>; template < class A> using uset = unordered_set<A>; template < typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { //多次元初期化 std::fill((T *)array, (T *)(array + N), val); } template < class A, class B> bool chmax(A &a, const B &b) { //最大値更新 返り値はbool if (a < b) { a = b; return 1; } return 0; } template < class A, class B> bool chmin(A &a, const B &b) { //最小値更新 返り値はbool if (b < a) { a = b; return 1; } return 0; } int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; constexpr int INF = 0x3f3f3f3f; constexpr int LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr int mod1 = 1e9 + 7; constexpr int mod2 = 998244353; constexpr int MOD = 1e9 + 7; //} END /* BIT: 区間和の更新や計算を行う構造体 初期値は a_1 = a_2 = ... = a_n = 0 add(i,x): a_i += x とする sum(i): a_1 + a_2 + ... + a_i を計算する 計算量は全て O(logn) */ template < typename T> struct BIT { int n; // 配列の要素数(数列の要素数+1) vector<T> bit; // データの格納先 BIT( int n_) : n(n_ + 1), bit(n, 0) {} void add( int i, T x) { for ( int idx = i; idx < n; idx += (idx & -idx)) bit[idx] += x; } T sum( int i) { T s(0); for ( int idx = i; idx > 0; idx -= (idx & -idx)) s += bit[idx]; return s; } }; signed main() { cin.tie(nullptr); ios_base::sync_with_stdio( false ); int n; cin >> n; BIT< int > seg(n); int q; cin >> q; rep(i, 0, q) { int que, a, b; cin >> que >> a >> b; if (que == 0) { seg.add(a, b); } else { cout << seg.sum(b) - seg.sum(a - 1) << '\n' ; } } return 0; } |
ステータス
項目 | データ |
---|---|
問題 | 0649 - 区間和(セグ木、BIT練習) |
ユーザー名 | immunity |
投稿日時 | 2020-07-20 18:49:53 |
言語 | C++17 |
状態 | Accepted |
得点 | 5 |
ソースコード長 | 2854 Byte |
最大実行時間 | 208 ms |
最大メモリ使用量 | 101864 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 5 / 5 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
input100-1 | AC | 76 ms | 1628 KB |
1
|
input100-2 | AC | 98 ms | 4136 KB |
1
|
input100-3 | AC | 105 ms | 9460 KB |
1
|
input100-4 | AC | 136 ms | 14276 KB |
1
|
input100-5 | AC | 208 ms | 21392 KB |
1
|
input1000-1 | AC | 86 ms | 23516 KB |
1
|
input1000-2 | AC | 158 ms | 28576 KB |
1
|
input1000-3 | AC | 78 ms | 30692 KB |
1
|
input1000-4 | AC | 194 ms | 37292 KB |
1
|
input1000-5 | AC | 69 ms | 38896 KB |
1
|
input10000-1 | AC | 180 ms | 44728 KB |
1
|
input10000-2 | AC | 200 ms | 51124 KB |
1
|
input10000-3 | AC | 160 ms | 55984 KB |
1
|
input10000-4 | AC | 177 ms | 61476 KB |
1
|
input10000-5 | AC | 107 ms | 64288 KB |
1
|
input100000-1 | AC | 207 ms | 71452 KB |
1
|
input100000-2 | AC | 57 ms | 72408 KB |
1
|
input100000-3 | AC | 45 ms | 73368 KB |
1
|
input100000-4 | AC | 141 ms | 76760 KB |
1
|
input100000-5 | AC | 188 ms | 82456 KB |
1
|
input1000000-1 | AC | 52 ms | 90072 KB |
1
|
input1000000-2 | AC | 93 ms | 92192 KB |
1
|
input1000000-3 | AC | 121 ms | 94816 KB |
1
|
input1000000-4 | AC | 192 ms | 99748 KB |
1
|
input1000000-5 | AC | 105 ms | 101864 KB |
1
|
input1001000-1 | AC | 22 ms | 93992 KB |
1
|
input1001000-2 | AC | 33 ms | 94068 KB |
1
|
input1001000-3 | AC | 34 ms | 94140 KB |
1
|
input1001000-4 | AC | 21 ms | 94088 KB |
1
|
input1001000-5 | AC | 21 ms | 93912 KB |
1
|