-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqrt_Decomposition.cpp
More file actions
114 lines (103 loc) · 2.7 KB
/
Sqrt_Decomposition.cpp
File metadata and controls
114 lines (103 loc) · 2.7 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
#include <bits/stdc++.h>
using namespace std;
// Sqrt Decomposition, build -> O(n), update & query -> O(sqrt(n))
template <typename T>
struct SqrtDecomposition {
int n, sq;
vector<T> a, b, lazy;
SqrtDecomposition(int _n) : n(_n) {
sq = sqrt(n) + 1;
a.assign(n, 0);
b.assign(sq, numeric_limits<T>::max());
lazy.assign(sq, 0);
}
SqrtDecomposition(const vector<T> &_a) : n(_a.size()) {
sq = sqrt(n) + 1;
a.assign(_a.begin(), _a.end());
b.assign(sq, numeric_limits<T>::max());
lazy.assign(sq, 0);
for (int i = 0; i < n; i++) {
b[i / sq] = min(b[i / sq], a[i]);
}
}
void modify(int l, int r, const T &v) {
assert(l >= 0 && l <= r && r < n);
int lc = l / sq, rc = r / sq;
if (lc == rc) {
for (int i = l; i <= r; i++) {
a[i] += v;
b[lc] = min(b[lc], a[i]);
}
}
else {
for (int i = l, end = (lc + 1) * sq; i < end; i++) {
a[i] += v;
b[lc] = min(b[lc], a[i]);
}
for (int i = lc + 1; i < rc; i++) {
lazy[i] += v;
}
for (int i = rc * sq; i <= r; i++) {
a[i] += v;
b[rc] = min(b[rc], a[i]);
}
}
}
T get(int l, int r) {
assert(l >= 0 && l <= r && r < n);
T res = numeric_limits<T>::max();
int lc = l / sq, rc = r / sq;
if (lc == rc) {
for (int i = l; i <= r; i++) {
res = min(res, a[i] + lazy[lc]);
}
}
else {
for (int i = l, end = (lc + 1) * sq; i < end; i++) {
res = min(res, a[i] + lazy[lc]);
}
for (int i = lc + 1; i < rc; i++) {
res = min(res, b[i] + lazy[i]);
}
for (int i = rc * sq; i <= r; i++) {
res = min(res, a[i] + lazy[rc]);
}
}
return res;
}
};
void solve() {
int n, q;
cin >> n >> q;
vector<int64_t> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
SqrtDecomposition<int64_t> sq(a);
while (q--) {
int op;
cin >> op;
if (op == 1) {
int l, r, v;
cin >> l >> r >> v;
l--, r--;
sq.modify(l, r, v);
}
else if (op == 2) {
int l, r;
cin >> l >> r;
l--, r--;
cout << sq.get(l, r) << '\n';
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
}