Submission #52163


ソースコード

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
#include "bits/stdc++.h"
// Custom Header {{{
#define ALL(x) x.begin(), x.end()
#define rep(i,n) for (i64 i = 0; i < (n); ++i)
#define reps(i,s,t) for (i64 i = (s); i <= (t); ++i)
using namespace std;
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;
// }}}
template<class Monoid> struct SegTree { // {{{
using Func = function<Monoid(Monoid, Monoid)>;
const int sz;
const Func fn;
const Monoid unity;
vector<Monoid> dat;
SegTree(int n, const Monoid &u, Func f)
: sz(1 << (__lg(n+5) + 1)), fn(f), unity(u), dat(sz*2, unity) {}
void set(int k, const Monoid &v) { dat[k + sz] = v; }
Monoid& operator[] (int k) { return dat[k + sz]; }
const Monoid& operator[] (int k) const { return dat[k + sz]; }
void build() { for (int k = sz-1; k > 0; --k) dat[k] = fn(dat[2*k], dat[2*k+1]); }
void update(int k, const Monoid &v) {
dat[ k += sz ] = v;
while(k >>= 1) dat[k] = fn(dat[2*k], dat[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, dat[a++]);
if (b & 1) R = fn(dat[--b], R);
}
return fn(L, R);
}
}; // }}}
signed main()
{
ios::sync_with_stdio(false); cin.tie(nullptr);
int N, M;
cin >> N >> M;
string com;
int a, b;
SegTree<i64> rangeMin(N, LINF, [](i64 a, i64 b) { return min(a, b); });
SegTree<i64> rangeMax(N, -LINF, [](i64 a, i64 b) { return max(a, b); });
SegTree<i64> rangeSum(N, 0LL, [](i64 a, i64 b) { return (a + b); });
rep(i, N) {
rangeMin[i] = rangeMax[i] = rangeSum[i] = 0;
}
rangeMin.build();
rangeMax.build();
rangeSum.build();
while(M--) {
cin >> com >> a >> b;
if (com == "update") {
rangeMin.update(a, b);
rangeMax.update(a, b);
rangeSum.update(a, b);
}
else if (com == "add") {
rangeMin.update(a, rangeMin[a] + b);
rangeMax.update(a, rangeMax[a] + b);
rangeSum.update(a, rangeSum[a] + b);
}
else if (com == "getMin") {
cout << rangeMin.query(a, b) << "\n";
}
else if (com == "getMax") {
cout << rangeMax.query(a, b) << "\n";
}
else if (com == "getSum") {
cout << rangeSum.query(a, b) << "\n";
}
}
return 0;
}

ステータス

項目 データ
問題 1072 - セグメントツリー技術基礎
ユーザー名 Arumakan_ei1727
投稿日時 2019-07-31 18:25:51
言語 C++14
状態 Accepted
得点 1
ソースコード長 2807 Byte
最大実行時間 58 ms
最大メモリ使用量 12240 KB

セット

セット 得点 Cases
1 ALL 1 / 1 *

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
in01 AC 31 ms 600 KB
1
in02 AC 48 ms 816 KB
1
in03 AC 40 ms 6968 KB
1
in04 AC 53 ms 7288 KB
1
in05 AC 30 ms 7352 KB
1
in06 AC 35 ms 4220 KB
1
in07 AC 34 ms 4412 KB
1
in08 AC 54 ms 3196 KB
1
in09 AC 47 ms 8132 KB
1
in10 AC 52 ms 5380 KB
1
in11 AC 25 ms 3908 KB
1
in12 AC 25 ms 2564 KB
1
in13 AC 58 ms 8724 KB
1
in14 AC 50 ms 4428 KB
1
in15 AC 36 ms 9100 KB
1
in16 AC 46 ms 4176 KB
1
in17 AC 53 ms 9748 KB
1
in18 AC 35 ms 9684 KB
1
in19 AC 50 ms 9876 KB
1
in20 AC 34 ms 9940 KB
1
in21 AC 34 ms 7064 KB
1
in22 AC 57 ms 7384 KB
1
in23 AC 40 ms 10772 KB
1
in24 AC 42 ms 7892 KB
1
in25 AC 49 ms 6548 KB
1
in26 AC 56 ms 11344 KB
1
in27 AC 49 ms 11792 KB
1
in28 AC 57 ms 9040 KB
1
in29 AC 34 ms 12048 KB
1
in30 AC 54 ms 12240 KB
1
in31 AC 39 ms 9360 KB
1
in32 AC 43 ms 9684 KB
1