-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegment_tree_beats.cpp
More file actions
137 lines (103 loc) · 3.04 KB
/
segment_tree_beats.cpp
File metadata and controls
137 lines (103 loc) · 3.04 KB
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
128
129
130
131
132
133
134
135
136
137
// BOJ 17474 수열과 쿼리 26
#include <bits/stdc++.h>
#define sz size()
#define bk back()
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
struct Node {
ll mx1, mx2, cnt, sum;
};
Node merge(Node a, Node b) {
if (a.mx1 == b.mx1)
return {a.mx1, max(a.mx2, b.mx2), a.cnt + b.cnt, a.sum + b.sum};
if (a.mx1 > b.mx1)
swap(a, b);
return {b.mx1, max(a.mx1, b.mx2), b.cnt, a.sum + b.sum};
}
struct SegmentTreeBeats {
vector<Node> tree;
SegmentTreeBeats(int n) { tree.resize(4 * n); }
Node init(int idx, int s, int e, vector<int> &v) {
if (s == e)
return tree[idx] = {v[s], -1, 1, v[s]};
int m = (s + e) >> 1;
return tree[idx] = merge(init(2 * idx, s, m, v), init(2 * idx + 1, m + 1, e, v));
}
void propagate(int idx, int s, int e) {
if (s == e)
return;
for (int i = 2 * idx; i <= 2 * idx + 1; i++) {
if (tree[idx].mx1 < tree[i].mx1) {
tree[i].sum += tree[i].cnt * (tree[idx].mx1 - tree[i].mx1);
tree[i].mx1 = tree[idx].mx1;
}
}
}
void update(int idx, int s, int e, int l, int r, int k) {
propagate(idx, s, e);
if (r < s || e < l || tree[idx].mx1 <= k)
return;
if (l <= s && e <= r && tree[idx].mx2 < k) {
tree[idx].sum += tree[idx].cnt * (k - tree[idx].mx1);
tree[idx].mx1 = k;
propagate(idx, s, e);
return;
}
int m = (s + e) >> 1;
update(2 * idx, s, m, l, r, k);
update(2 * idx + 1, m + 1, e, l, r, k);
tree[idx] = merge(tree[2 * idx], tree[2 * idx + 1]);
}
int maximum(int idx, int s, int e, int l, int r) {
propagate(idx, s, e);
if (r < s || e < l)
return 0;
if (l <= s && e <= r)
return tree[idx].mx1;
int m = (s + e) >> 1;
return max(maximum(2 * idx, s, m, l, r), maximum(2 * idx + 1, m + 1, e, l, r));
}
ll sum(int idx, int s, int e, int l, int r) {
propagate(idx, s, e);
if (r < s || e < l)
return 0;
if (l <= s && e <= r)
return tree[idx].sum;
int m = (s + e) >> 1;
return sum(2 * idx, s, m, l, r) + sum(2 * idx + 1, m + 1, e, l, r);
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<int> v(n + 1);
for (int i = 1; i <= n; i++)
cin >> v[i];
SegmentTreeBeats stb(n);
stb.init(1, 1, n, v);
int q;
cin >> q;
while (q--) {
int op;
cin >> op;
if (op == 1) {
int l, r, k;
cin >> l >> r >> k;
stb.update(1, 1, n, l, r, k);
} else if (op == 2) {
int l, r;
cin >> l >> r;
cout << stb.maximum(1, 1, n, l, r) << '\n';
} else if (op == 3) {
int l, r;
cin >> l >> r;
cout << stb.sum(1, 1, n, l, r) << '\n';
}
}
}