forked from Dev-XYS/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Segment-Tree(Minimum).cpp
120 lines (112 loc) · 1.76 KB
/
Segment-Tree(Minimum).cpp
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
#include <cstdio>
using namespace std;
struct node
{
int l, r, min, lazy;
}T[4000010];
int d[1000010];
inline int min(int x, int y)
{
return x < y ? x : y;
}
void build_tree(int l, int r, int k)
{
T[k].l = l;
T[k].r = r;
if (l == r)
{
T[k].min = d[l];
}
else
{
int mid = (l + r) >> 1;
build_tree(l, mid, k << 1);
build_tree(mid + 1, r, (k << 1) + 1);
T[k].min = min(T[k << 1].min, T[(k << 1) + 1].min);
}
}
int delta;
void update(int l, int r, int k)
{
if (T[k].l == l && T[k].r == r)
{
T[k].lazy += delta;
T[k].min += delta;
}
else
{
int mid = (T[k].l + T[k].r) >> 1;
if (r <= mid)
{
update(l, r, k << 1);
}
else if (l > mid)
{
update(l, r, (k << 1) + 1);
}
else
{
update(l, mid, k << 1);
update(mid + 1, r, (k << 1) + 1);
}
T[k].min = min(T[k << 1].min, T[(k << 1) + 1].min);
}
}
int query(int l, int r, int k)
{
if (T[k].l == l && T[k].r == r)
{
return T[k].min;
}
else
{
int mid = (T[k].l + T[k].r) >> 1, res;
T[k << 1].lazy += T[k].lazy;
T[k << 1].min += T[k].lazy;
T[(k << 1) + 1].lazy += T[k].lazy;
T[(k << 1) + 1].min += T[k].lazy;
T[k].lazy = 0;
if (r <= mid)
{
res = query(l, r, k << 1);
}
else if (l > mid)
{
res = query(l, r, (k << 1) + 1);
}
else
{
res = min(query(l, mid, k << 1), query(mid + 1, r, (k << 1) + 1));
}
T[k].min = min(T[k << 1].min, T[(k << 1) + 1].min);
return res;
}
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &d[i]);
}
build_tree(1, n, 1);
while (true)
{
int o, a, b, d;
scanf("%d", &o);
switch (o)
{
case 1:
scanf("%d%d", &a, &b);
printf("%d\n", query(a, b, 1));
break;
case 2:
scanf("%d%d%d", &a, &b, &d);
delta = d;
update(a, b, 1);
break;
}
}
return 0;
}