-
Notifications
You must be signed in to change notification settings - Fork 0
/
Demo.cpp
221 lines (179 loc) · 6.4 KB
/
Demo.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <bits/stdc++.h>
using namespace std;
// Định nghĩa cấu trúc một node
struct Node {
int Sum, Max, Min;
long long Product;
// Khởi tạo node
Node() {
Sum = 0;
Max = INT_MIN;
Min = INT_MAX;
Product = 1;
}
};
// Hàm gộp 2 node
Node merge(Node& left, Node& right);
// Hàm tạo cây phân đoạn
void build(Node* SegmentTree, int* arr, int id, int low, int high);
// Hàm cập nhật phần tử thứ i
void update(Node* SegmentTree, int id, int low, int high, int i, int val);
// Hàm cập nhật đoạn [i,j]
void rangeUpdate(Node* SegmentTree, int id, int low, int high, int i, int j, int val);
// Hàm truy vấn đoạn [i,j]
Node query(Node* SegmentTree, int id, int low, int high, int i, int j);
int main() {
int N;
cout << "Nhap so phan tu cua mang: ";
cin >> N;
int arr[N];
cout << "Nhap cac phan tu cua mang: ";
for (int i = 0; i < N; i++) {
cin >> arr[i];
}
Node SegmentTree[4 * N];
build(SegmentTree, arr, 0, 0, N - 1);
while (true) {
cout << "Mang hien tai: ";
for (int i = 0; i < N; i++) {
cout << arr[i] << " ";
}
cout << endl;
cout << "1. Cap nhat phan tu" << endl;
cout << "2. Cap nhat doan" << endl;
cout << "3. Tim tong" << endl;
cout << "4. Tim tich" << endl;
cout << "5. Tim max" << endl;
cout << "6. Tim min" << endl;
cout << "7. Thoat" << endl;
cout << "Nhap lua chon: " << endl;
int choice;
cin >> choice;
if (choice == 1) {
int i, val;
cout << "Nhap vi tri can cap nhat: ";
cin >> i;
cout << "Nhap gia tri can cap nhat: ";
cin >> val;
arr[i] = val;
update(SegmentTree, 0, 0, N - 1, i, val);
}
else if (choice == 2) {
int i, j, val;
cout << "Nhap doan can cap nhat:";
cin >> i >> j;
cout << "Nhap gia tri can cap nhat: ";
cin >> val;
rangeUpdate(SegmentTree, 0, 0, N - 1, i, j, val);
for (i; i <= j; i++) {
arr[i] = val;
}
}
else if (choice == 3) {
int i, j;
cout << "Nhap doan can tinh tong: ";
cin >> i >> j;
cout << "Tong cua doan [" << i << ", " << j << "] la: ";
cout << query(SegmentTree, 0, 0, N - 1, i, j).Sum << endl;
cout << "==============================" << endl;
}
else if (choice == 4) {
int i, j;
cout << "Nhap doan can tinh tich: ";
cin >> i >> j;
cout << "Tich cua doan [" << i << ", " << j << "] la: ";
cout << query(SegmentTree, 0, 0, N - 1, i, j).Product << endl;
cout << "==============================" << endl;
}
else if (choice == 5) {
int i, j;
cout << "Nhap doan can tim max: ";
cin >> i >> j;
cout << "Max cua doan [" << i << ", " << j << "] la: ";
cout << query(SegmentTree, 0, 0, N - 1, i, j).Max << endl;
cout << "==============================" << endl;
}
else if (choice == 6) {
int i, j;
cout << "Nhap doan can tim min: ";
cin >> i >> j;
cout << "Min cua doan [" << i << ", " << j << "] la: ";
cout << query(SegmentTree, 0, 0, N - 1, i, j).Min << endl;
cout << "==============================" << endl;
}
else if (choice == 7) {
break;
}
}
}
Node merge(Node& left, Node& right) {
Node parent;
parent.Sum = left.Sum + right.Sum;
parent.Max = max(left.Max, right.Max);
parent.Min = min(left.Min, right.Min);
parent.Product = left.Product * right.Product;
return parent;
}
void build(Node* SegmentTree, int* arr, int id, int low, int high) {
// Trường hợp đoạn có 1 phần tử, node đang xét là node lá
if (low == high) {
SegmentTree[id].Sum = arr[low];
SegmentTree[id].Max = arr[low];
SegmentTree[id].Min = arr[low];
SegmentTree[id].Product = arr[low];
return;
}
// Trường hợp đoạn có nhiều hơn 1 phần tử
int mid = (low + high) / 2;
build(SegmentTree, arr, 2 * id + 1, low, mid);
build(SegmentTree, arr, 2 * id + 2, mid + 1, high);
SegmentTree[id] = merge(SegmentTree[2 * id + 1], SegmentTree[2 * id + 2]);
}
void update(Node* SegmentTree, int id, int low, int high, int i, int val) {
// i nằm ngoài đoạn [low, high]
if (low > i || high < i)
return;
// Trường hợp node lá (đoạn có 1 phần tử)
if (low == high) {
SegmentTree[id].Sum = SegmentTree[id].Product = val;
SegmentTree[id].Max = SegmentTree[id].Min = val;
return;
}
// Trường hợp không phải node lá
int mid = (low + high) / 2;
update(SegmentTree, 2 * id + 1, low, mid, i, val);
update(SegmentTree, 2 * id + 2, mid + 1, high, i, val);
SegmentTree[id] = merge(SegmentTree[2 * id + 1], SegmentTree[2 * id + 2]);
}
void rangeUpdate(Node* SegmentTree, int id, int low, int high, int i, int j, int val) {
// Trường hợp không giao nhau
if (low > j || high < i)
return;
// Các trường hợp giao nhau
// Trường hợp là node lá
if (low == high) {
SegmentTree[id].Sum = SegmentTree[id].Product = val;
SegmentTree[id].Max = SegmentTree[id].Min = val;
return;
}
// Trường hợp không là node lá
int mid = (low + high) / 2;
rangeUpdate(SegmentTree, 2 * id + 1, low, mid, i, j, val);
rangeUpdate(SegmentTree, 2 * id + 2, mid + 1, high, i, j, val);
SegmentTree[id] = merge(SegmentTree[2 * id + 1], SegmentTree[2 * id + 2]);
}
Node query(Node* SegmentTree, int id, int low, int high, int i, int j) {
// Trường hợp không giao nhau
if (low > j || high < i) {
Node nullNode;
return nullNode;
}
// Trường hợp đoạn [low, high] nằm trong đoạn [i, j]
if (low >= i && high <= j)
return SegmentTree[id];
// Trường hợp đoạn [low, high] giao với đoạn [i, j]
int mid = (low + high) / 2;
Node left = query(SegmentTree, 2 * id + 1, low, mid, i, j);
Node right = query(SegmentTree, 2 * id + 2, mid + 1, high, i, j);
return merge(left, right);
}