-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSegmentTree.cpp
More file actions
166 lines (151 loc) · 3.45 KB
/
Copy pathSegmentTree.cpp
File metadata and controls
166 lines (151 loc) · 3.45 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define X first
#define Y second
#define f(c) c - 'a'
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int N = 1e5 + 5;
const ll INF = 1e18;
ll a[N];
struct node
{
ll min_A;
ll min_B;
ll min_D;
ll lazy;
ll res;
node() : min_A(0), min_B(INF), min_D(0), lazy(0), res(0) {}
};
node merge(const node &a, const node &b)
{
node res;
res.min_A = min(a.min_A, b.min_A);
res.min_B = min(a.min_B, b.min_B);
res.min_D = min(a.min_D, b.min_D);
res.res = min(res.min_A, res.min_B);
return res;
}
node tree[4 * N];
void build(int id, int l, int r)
{
if (l == r)
{
tree[id].min_A = a[l];
tree[id].min_B = INF;
tree[id].min_D = a[l];
tree[id].lazy = 0;
tree[id].res = a[l];
return;
}
int mid = (l + r) / 2;
build(2 * id, l, mid);
build(2 * id + 1, mid + 1, r);
tree[id] = merge(tree[2 * id], tree[2 * id + 1]);
}
void push(int id, int l, int r)
{
if (tree[id].lazy)
{
tree[id].min_B += tree[id].lazy;
tree[id].min_D -= tree[id].lazy;
tree[id].res = min(tree[id].min_A, tree[id].min_B);
if (l != r) // not a leaf node
{
tree[2 * id].lazy += tree[id].lazy;
tree[2 * id + 1].lazy += tree[id].lazy;
}
tree[id].lazy = 0;
}
}
void update(int id, int l, int r, int ql, int qr, ll val)
{
push(id, l, r);
if (l > qr || r < ql)
return; // no overlap
if (l >= ql && r <= qr) // total overlap
{
tree[id].lazy += val;
push(id, l, r);
return;
}
int mid = (l + r) / 2;
update(2 * id, l, mid, ql, qr, val);
update(2 * id + 1, mid + 1, r, ql, qr, val);
tree[id] = merge(tree[2 * id], tree[2 * id + 1]);
}
void fix(int id, int l, int r, int ql, int qr)
{
if (l > qr || r < ql)
return; // no overlap
push(id, l, r);
if (tree[id].min_D > 0)
return;
if (l == r)
{
tree[id].min_B -= INF;
tree[id].min_A = INF;
tree[id].lazy = 0;
tree[id].res = tree[id].min_B;
tree[id].min_D = INF;
return;
}
int mid = (l + r) / 2;
fix(2 * id, l, mid, ql, qr);
fix(2 * id + 1, mid + 1, r, ql, qr);
tree[id] = merge(tree[2 * id], tree[2 * id + 1]);
}
ll query(int id, int l, int r, int ql, int qr)
{
push(id, l, r);
if (l > qr || r < ql)
return 1e18;
if (l >= ql && r <= qr)
{
return tree[id].res;
}
int mid = (l + r) / 2;
return min(query(2 * id, l, mid, ql, qr), query(2 * id + 1, mid + 1, r, ql, qr));
}
void solve()
{
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
}
build(1, 1, n);
while (q--)
{
ll type, l, r;
cin >> type >> l >> r;
if (type == 1)
{
ll val;
cin >> val;
update(1, 1, n, l, r, val);
fix(1, 1, n, 1, n);
}
else if (type == 2)
{
ll ans = query(1, 1, n, l, r);
cout << ans << '\n';
}
}
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#ifdef LOCAL_IO
freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif
int tc = 1;
// cin>>tc;
for (int i = 1; i <= tc; ++i)
{
solve();
}
}