Submission #52164
ソースコード
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 108 | #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]); } void add( 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, M; cin >> N >> M; SegmentTree< int > minseg(N, LINF, []( int a, int b) { return min(a, b); }); SegmentTree< int > maxseg(N, -LINF, []( int a, int b) { return max(a, b); }); SegmentTree< int > sumseg(N, 0, []( int a, int b) { return a + b; }); rep(i, N) minseg[i] = maxseg[i] = 0; minseg.build(); maxseg.build(); string q; int a, b; rep(i, M) { cin >> q >> a >> b; if (q == "update" ) { minseg.update(a, b); maxseg.update(a, b); sumseg.update(a, b); } else if (q == "add" ) { minseg.add(a, b); maxseg.add(a, b); sumseg.add(a, b); } else if (q == "getMin" ) { cout << minseg.query(a, b) << "\n" ; } else if (q == "getMax" ) { cout << maxseg.query(a, b) << "\n" ; } else if (q == "getSum" ) { cout << sumseg.query(a, b) << "\n" ; } } return 0; } |
ステータス
項目 | データ |
---|---|
問題 | 1072 - セグメントツリー技術基礎 |
ユーザー名 | もけ |
投稿日時 | 2019-07-31 18:29:10 |
言語 | C++14 |
状態 | Accepted |
得点 | 1 |
ソースコード長 | 2624 Byte |
最大実行時間 | 87 ms |
最大メモリ使用量 | 12220 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 1 / 1 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
in01 | AC | 23 ms | 604 KB |
1
|
in02 | AC | 38 ms | 812 KB |
1
|
in03 | AC | 36 ms | 6960 KB |
1
|
in04 | AC | 48 ms | 7284 KB |
1
|
in05 | AC | 35 ms | 7348 KB |
1
|
in06 | AC | 28 ms | 4340 KB |
1
|
in07 | AC | 36 ms | 4532 KB |
1
|
in08 | AC | 46 ms | 3316 KB |
1
|
in09 | AC | 50 ms | 8120 KB |
1
|
in10 | AC | 47 ms | 5368 KB |
1
|
in11 | AC | 22 ms | 3768 KB |
1
|
in12 | AC | 30 ms | 2428 KB |
1
|
in13 | AC | 87 ms | 8708 KB |
1
|
in14 | AC | 58 ms | 4420 KB |
1
|
in15 | AC | 42 ms | 9224 KB |
1
|
in16 | AC | 46 ms | 4040 KB |
1
|
in17 | AC | 47 ms | 9732 KB |
1
|
in18 | AC | 46 ms | 9796 KB |
1
|
in19 | AC | 46 ms | 9984 KB |
1
|
in20 | AC | 35 ms | 10044 KB |
1
|
in21 | AC | 36 ms | 7032 KB |
1
|
in22 | AC | 59 ms | 7352 KB |
1
|
in23 | AC | 46 ms | 10748 KB |
1
|
in24 | AC | 46 ms | 7872 KB |
1
|
in25 | AC | 55 ms | 6532 KB |
1
|
in26 | AC | 53 ms | 11332 KB |
1
|
in27 | AC | 47 ms | 11520 KB |
1
|
in28 | AC | 61 ms | 8764 KB |
1
|
in29 | AC | 36 ms | 12028 KB |
1
|
in30 | AC | 55 ms | 12220 KB |
1
|
in31 | AC | 35 ms | 9344 KB |
1
|
in32 | AC | 54 ms | 9536 KB |
1
|