-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGTLN.cpp
99 lines (77 loc) · 1.77 KB
/
GTLN.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
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
class Query {
private:
int u, v;
public:
Query(int uu, int vv) : u(uu), v(vv) {}
int getU() const {
return u;
}
int getV() const {
return v;
}
};
class Update {
private:
int u, v, k;
public:
Update(int uu, int vv, int kk) : u(uu), v(vv), k(kk) {}
int getU() const {
return u;
}
int getV() const {
return v;
}
int getK() const {
return k;
}
};
class ArrayUpdater {
private:
vector<int> arr;
public:
ArrayUpdater(int n) : arr(n, 0) {}
void applyUpdate(const Update& update) {
arr[update.getU() - 1] += update.getK();
if (update.getV() < arr.size()) {
arr[update.getV()] -= update.getK();
}
}
void calculatePrefixSum() {
for (int i = 1; i < arr.size(); ++i) {
arr[i] += arr[i - 1];
}
}
int getMaxValueInRange(int u, int v) const {
int maxVal = INT_MIN;
for (int i = u - 1; i < v; ++i) {
maxVal = max(maxVal, arr[i]);
}
return maxVal;
}
};
int main() {
int n, m;
cin >> n >> m;
ArrayUpdater arrayUpdater(n);
for (int i = 0; i < m; ++i) {
int u, v, k;
cin >> u >> v >> k;
Update update(u, v, k);
arrayUpdater.applyUpdate(update);
}
arrayUpdater.calculatePrefixSum();
int p;
cin >> p;
for (int i = 0; i < p; ++i) {
int u, v;
cin >> u >> v;
Query query(u, v);
cout << arrayUpdater.getMaxValueInRange(query.getU(), query.getV()) << endl;
}
return 0;
}