Submission #52156


ソースコード

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <utility>
#include <functional>
#include <climits>
#define REP(i, n) for(int i = 0; i < n; ++i)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define F first
#define S second
#define OUT(x) cout << x << "\n"
#define int long long
using namespace std;
using point = pair< int,int >;
template< class T >
class Segment {
// 0-indexed
// parent : (n - 1) / 2
// left : 2 * n + 1
// right : 2 * n + 2
int size;
T inf;
function< T(T, T) > f;
vector< T > data;
public:
Segment(int size, T val, T inf, function< T(T, T) > f) : inf(inf), f(f)
{
int _n = 1;
while (_n < size) {
_n *= 2;
}
this -> size = _n;
data.resize(_n * 2 - 1, inf);
}
// 値の更新
void update(int p, T v)
{
p += size - 1;
data[p] = v;
while (p > 0) {
p = (p - 1) / 2;
data[p] = f(data[2 * p + 1], data[2 * p + 2]);
}
return;
}
void add(int p, T v)
{
p += size - 1;
data[p] += v;
while (p > 0) {
p = (p - 1) / 2;
data[p] = f(data[2 * p + 1], data[2 * p + 2]);
}
return;
}
// [a, b)に対するクエリ
// usage : query(a, b, 0, 0, n);
T query(int a, int b, int p, int l, int r)
{
if (b <= l || r <= a) return inf;
else if (a <= l && r <= b) return data[p];
else {
T lv = query(a, b, 2 * p + 1, l, (l + r) / 2);
T rv = query(a, b, 2 * p + 2, (l + r) / 2, r);
return f(lv, rv);
}
}
T query(int a, int b)
{
return query(a, b, 0, 0, size);
}
};
signed main() {
int n, m;
cin >> n >> m;
Segment< int > tree_min(n, 0, LLONG_MAX, [](int a, int b) { return min(a, b); });
Segment< int > tree_max(n, 0, LLONG_MIN, [](int a, int b) { return max(a, b); });
Segment< int > tree_sum(n, 0, 0, [](int a, int b) { return a + b; });
for (int i = 0; i < n; i++) {
tree_min.update(i, 0);
tree_max.update(i, 0);
tree_sum.update(i, 0);
}
string str;
int a, b;
REP (i, m) {
cin >> str;
scanf("%lld%lld", &a, &b);
if (str == "update") {
tree_min.update(a, b);
tree_max.update(a, b);
tree_sum.update(a, b);
} else if (str == "add") {
tree_min.add(a, b);
tree_max.add(a, b);
tree_sum.add(a, b);
} else if (str == "getMin") {
OUT(tree_min.query(a, b));
} else if (str == "getMax") {
OUT(tree_max.query(a, b));
} else if (str == "getSum") {
OUT(tree_sum.query(a, b));
}
}
return 0;
}

ステータス

項目 データ
問題 1072 - セグメントツリー技術基礎
ユーザー名 Wonder /*ei1741*/
投稿日時 2019-07-31 13:34:26
言語 C++14
状態 Accepted
得点 1
ソースコード長 3060 Byte
最大実行時間 189 ms
最大メモリ使用量 12288 KB

セット

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

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
in01 AC 22 ms 604 KB
1
in02 AC 125 ms 1092 KB
1
in03 AC 105 ms 7144 KB
1
in04 AC 136 ms 7228 KB
1
in05 AC 58 ms 7316 KB
1
in06 AC 85 ms 4336 KB
1
in07 AC 94 ms 4552 KB
1
in08 AC 161 ms 3236 KB
1
in09 AC 155 ms 7936 KB
1
in10 AC 161 ms 5212 KB
1
in11 AC 45 ms 3636 KB
1
in12 AC 73 ms 2568 KB
1
in13 AC 146 ms 8752 KB
1
in14 AC 189 ms 4484 KB
1
in15 AC 117 ms 9312 KB
1
in16 AC 147 ms 4024 KB
1
in17 AC 129 ms 9616 KB
1
in18 AC 75 ms 9704 KB
1
in19 AC 135 ms 10048 KB
1
in20 AC 83 ms 10136 KB
1
in21 AC 108 ms 7152 KB
1
in22 AC 166 ms 7496 KB
1
in23 AC 116 ms 10660 KB
1
in24 AC 138 ms 7800 KB
1
in25 AC 140 ms 6476 KB
1
in26 AC 181 ms 11300 KB
1
in27 AC 158 ms 11516 KB
1
in28 AC 188 ms 8788 KB
1
in29 AC 69 ms 11944 KB
1
in30 AC 148 ms 12288 KB
1
in31 AC 94 ms 9304 KB
1
in32 AC 140 ms 9512 KB
1