-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegment_tree.cpp
More file actions
83 lines (81 loc) · 2.24 KB
/
segment_tree.cpp
File metadata and controls
83 lines (81 loc) · 2.24 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
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back;
const int N = 5e5 + 9;
int a[N];
struct ST {
#define lc (n << 1)
#define rc ((n << 1) | 1)
long long t[4 * N], lazy[4 * N];
ST() {
memset(t, 0, sizeof t);
memset(lazy, 0, sizeof lazy);
}
inline void push(int n, int b, int e) { //change this
if (lazy[n] == 0) return;
t[n] = t[n] + lazy[n];
if (b != e) {
lazy[lc] = (lazy[lc] + lazy[n]);
lazy[rc] = (lazy[rc] + lazy[n]);
// lazy[rc] = lazy[n];
}
lazy[n] = 0;
}
inline long long combine(long long a, long long b) { //change this
return min(a , b);
}
inline void pull(int n) { //change this
t[n] = min(t[lc] , t[rc]);
}
void build(int n, int b, int e) {
lazy[n] = 0;//change this
if (b == e) {
t[n] = a[b]; //change this
return;
}
int mid = (b + e) >> 1;
build(lc, b, mid);
build(rc, mid + 1, e);
pull(n);
}
void upd(int n, int b, int e, int i, int j, long long v) {
push(n, b, e);
if (j < b || e < i) return ;
if (i <= b && e <= j) {
lazy[n] = (v); //set lazy
push(n, b, e);
return;
}
int mid = (b + e) >> 1;
upd(lc, b, mid, i, j, v);
upd(rc, mid + 1, e, i, j, v);
pull(n);
}
long long query(int n, int b, int e, int i, int j) {
push(n, b, e);
if (i > e || b > j) return 1e18; //change this
if (i <= b && e <= j) return t[n];
int mid = (b + e) >> 1;
return combine(query(lc, b, mid, i, j), query(rc, mid + 1, e, i, j));
}
} tt;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
for (int i = 1; i <= n; i++) a[i] = 0;
tt.build(1, 1, n);
while (m--) {
int type; cin >> type;
if (type == 1) {
int l , r, v; cin >> l >> r >> v;
tt.upd(1, 1, n, l + 1, r, v);
// cout << " >> " << tt.query(1 , 1 , n, 4 , 5) << endl;
}
else {
int l , r; cin >> l >> r;
cout << tt.query(1, 1, n, l + 1, r) << endl;
}
}
}