Submission #52170


ソースコード

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
#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 cout << sumseg.query(a, b) << "\n";
}
return 0;
}

ステータス

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

セット

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

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
in01 AC 31 ms 604 KB
1
in02 AC 43 ms 816 KB
1
in03 AC 46 ms 6968 KB
1
in04 AC 85 ms 7156 KB
1
in05 AC 38 ms 7216 KB
1
in06 AC 31 ms 4208 KB
1
in07 AC 41 ms 4532 KB
1
in08 AC 52 ms 3188 KB
1
in09 AC 48 ms 7980 KB
1
in10 AC 57 ms 5356 KB
1
in11 AC 30 ms 3880 KB
1
in12 AC 26 ms 2540 KB
1
in13 AC 48 ms 8696 KB
1
in14 AC 51 ms 4408 KB
1
in15 AC 38 ms 9208 KB
1
in16 AC 45 ms 4156 KB
1
in17 AC 46 ms 9720 KB
1
in18 AC 29 ms 9780 KB
1
in19 AC 49 ms 9972 KB
1
in20 AC 35 ms 10032 KB
1
in21 AC 35 ms 7028 KB
1
in22 AC 50 ms 7348 KB
1
in23 AC 44 ms 10612 KB
1
in24 AC 41 ms 7856 KB
1
in25 AC 48 ms 6632 KB
1
in26 AC 58 ms 11300 KB
1
in27 AC 49 ms 11624 KB
1
in28 AC 53 ms 8876 KB
1
in29 AC 28 ms 12012 KB
1
in30 AC 49 ms 12200 KB
1
in31 AC 38 ms 9320 KB
1
in32 AC 48 ms 9516 KB
1