#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define dbg(x) cerr << #x << " " << x << "\n"
struct segment_tree {
vector <pair <int, int>> seg;
int n;
void init (int n) {
seg.resize (1 + 4 * n);
}
void build (int node, int lb, int rb, vector <int> &v) {
if (lb == rb) {
seg[node] = {v[lb], lb};
return;
}
int mid = (lb + rb) / 2, left_node = node * 2, right_node = node * 2 + 1;
build (left_node, lb, mid, v);
build (right_node, mid + 1, rb, v);
seg[node] = max (seg[left_node], seg[right_node]);
}
pair <int, int> query_max (int node, int lb, int rb, int ql, int qr) {
if (ql <= lb && rb <= qr)
return seg[node];
int mid = (lb + rb) / 2, left_node = node * 2, right_node = node * 2 + 1;
pair <int, int> ans = {0, 0};
if (ql <= mid)
ans = max (ans, query_max (left_node, lb, mid, ql, qr));
if (qr > mid)
ans = max (ans, query_max (right_node, mid + 1, rb, ql, qr));
return ans;
}
};
const ll INF = 1e18;
struct special_set {
multiset <pair <int, ll>> stars;
ll more = 0;
void update (ll x) {
more += x;
}
void add (pair <int, ll> x) {
x.second -= more;
auto it = stars.upper_bound (x);
if (it == stars.begin ())
stars.insert (x);
else {
it = prev (it);
if (x.second < it->second)
stars.insert (x);
}
it = stars.upper_bound (x);
while (it != stars.end () && x.second <= it->second) {
stars.erase (it);
it = stars.upper_bound (x);
}
}
ll query (int x) {
auto it = prev (stars.lower_bound ({x + 1, -INF}));
return it->second + more;
}
int size () {
return (int) stars.size ();
}
void join (special_set other) {
for (auto it : other.stars)
add ({it.first, it.second + other.more});
}
};
segment_tree buildings;
const int MAX_N = 2e5;
int n;
vector <pair <int, int>> events[1 + MAX_N];
void solve (int lb, int rb, special_set &sol) {
if (lb > rb) return;
if (lb == rb) {
ll total = 0;
for (auto event : events[lb])
total += event.second;
sol.add ({0, total});
for (auto event : events[lb])
sol.add ({event.first, total - event.second});
return;
}
pair <int, int> ans = buildings.query_max (1, 1, n, lb, rb);
int mid = ans.second;
int pivot = ans.first;
special_set L, R;
if (mid == rb)
mid--;
solve (lb, mid, L);
solve (mid + 1, rb, R);
ll topL = R.query (pivot), topR = L.query (pivot);
L.update (topL);
R.update (topR);
if (L.size () < R.size ())
swap (L, R);
L.join (R);
swap (sol, L);
}
int main () {
ios::sync_with_stdio (false);
cin.tie (0); cout.tie (0);
cin >> n;
vector <int> v (n + 1);
for (int i = 1; i <= n; i++)
cin >> v[i];
buildings.init (n);
buildings.build (1, 1, n, v);
int m;
cin >> m;
while (m--) {
int x, y, c;
cin >> x >> y >> c;
events[x].push_back ({y, c});
}
special_set sol;
solve (1, n, sol);
cout << sol.query (n) << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5100 KB |
Output is correct |
2 |
Correct |
5 ms |
5100 KB |
Output is correct |
3 |
Correct |
5 ms |
5100 KB |
Output is correct |
4 |
Correct |
5 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
5 ms |
5100 KB |
Output is correct |
9 |
Correct |
5 ms |
5100 KB |
Output is correct |
10 |
Correct |
6 ms |
5100 KB |
Output is correct |
11 |
Correct |
5 ms |
5228 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
5 ms |
5228 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
5 ms |
5100 KB |
Output is correct |
17 |
Correct |
5 ms |
5100 KB |
Output is correct |
18 |
Correct |
5 ms |
5120 KB |
Output is correct |
19 |
Correct |
5 ms |
5100 KB |
Output is correct |
20 |
Correct |
5 ms |
5100 KB |
Output is correct |
21 |
Correct |
5 ms |
5100 KB |
Output is correct |
22 |
Correct |
5 ms |
5096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5100 KB |
Output is correct |
2 |
Correct |
5 ms |
5100 KB |
Output is correct |
3 |
Correct |
5 ms |
5100 KB |
Output is correct |
4 |
Correct |
5 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
5 ms |
5100 KB |
Output is correct |
9 |
Correct |
5 ms |
5100 KB |
Output is correct |
10 |
Correct |
6 ms |
5100 KB |
Output is correct |
11 |
Correct |
5 ms |
5228 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
5 ms |
5228 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
5 ms |
5100 KB |
Output is correct |
17 |
Correct |
5 ms |
5100 KB |
Output is correct |
18 |
Correct |
5 ms |
5120 KB |
Output is correct |
19 |
Correct |
5 ms |
5100 KB |
Output is correct |
20 |
Correct |
5 ms |
5100 KB |
Output is correct |
21 |
Correct |
5 ms |
5100 KB |
Output is correct |
22 |
Correct |
5 ms |
5096 KB |
Output is correct |
23 |
Correct |
6 ms |
5228 KB |
Output is correct |
24 |
Correct |
8 ms |
5228 KB |
Output is correct |
25 |
Correct |
6 ms |
5228 KB |
Output is correct |
26 |
Correct |
6 ms |
5228 KB |
Output is correct |
27 |
Correct |
6 ms |
5356 KB |
Output is correct |
28 |
Correct |
6 ms |
5228 KB |
Output is correct |
29 |
Correct |
7 ms |
5228 KB |
Output is correct |
30 |
Correct |
6 ms |
5228 KB |
Output is correct |
31 |
Correct |
7 ms |
5228 KB |
Output is correct |
32 |
Correct |
7 ms |
5740 KB |
Output is correct |
33 |
Correct |
7 ms |
5740 KB |
Output is correct |
34 |
Correct |
7 ms |
5612 KB |
Output is correct |
35 |
Correct |
7 ms |
5612 KB |
Output is correct |
36 |
Correct |
6 ms |
5612 KB |
Output is correct |
37 |
Correct |
6 ms |
5484 KB |
Output is correct |
38 |
Correct |
7 ms |
5868 KB |
Output is correct |
39 |
Correct |
7 ms |
5228 KB |
Output is correct |
40 |
Correct |
8 ms |
5740 KB |
Output is correct |
41 |
Correct |
6 ms |
5228 KB |
Output is correct |
42 |
Correct |
7 ms |
5228 KB |
Output is correct |
43 |
Correct |
9 ms |
5868 KB |
Output is correct |
44 |
Correct |
8 ms |
5228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5100 KB |
Output is correct |
2 |
Correct |
5 ms |
5100 KB |
Output is correct |
3 |
Correct |
5 ms |
5100 KB |
Output is correct |
4 |
Correct |
5 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
5 ms |
5100 KB |
Output is correct |
9 |
Correct |
5 ms |
5100 KB |
Output is correct |
10 |
Correct |
6 ms |
5100 KB |
Output is correct |
11 |
Correct |
5 ms |
5228 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
5 ms |
5228 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
5 ms |
5100 KB |
Output is correct |
17 |
Correct |
5 ms |
5100 KB |
Output is correct |
18 |
Correct |
5 ms |
5120 KB |
Output is correct |
19 |
Correct |
5 ms |
5100 KB |
Output is correct |
20 |
Correct |
5 ms |
5100 KB |
Output is correct |
21 |
Correct |
5 ms |
5100 KB |
Output is correct |
22 |
Correct |
5 ms |
5096 KB |
Output is correct |
23 |
Correct |
6 ms |
5228 KB |
Output is correct |
24 |
Correct |
8 ms |
5228 KB |
Output is correct |
25 |
Correct |
6 ms |
5228 KB |
Output is correct |
26 |
Correct |
6 ms |
5228 KB |
Output is correct |
27 |
Correct |
6 ms |
5356 KB |
Output is correct |
28 |
Correct |
6 ms |
5228 KB |
Output is correct |
29 |
Correct |
7 ms |
5228 KB |
Output is correct |
30 |
Correct |
6 ms |
5228 KB |
Output is correct |
31 |
Correct |
7 ms |
5228 KB |
Output is correct |
32 |
Correct |
7 ms |
5740 KB |
Output is correct |
33 |
Correct |
7 ms |
5740 KB |
Output is correct |
34 |
Correct |
7 ms |
5612 KB |
Output is correct |
35 |
Correct |
7 ms |
5612 KB |
Output is correct |
36 |
Correct |
6 ms |
5612 KB |
Output is correct |
37 |
Correct |
6 ms |
5484 KB |
Output is correct |
38 |
Correct |
7 ms |
5868 KB |
Output is correct |
39 |
Correct |
7 ms |
5228 KB |
Output is correct |
40 |
Correct |
8 ms |
5740 KB |
Output is correct |
41 |
Correct |
6 ms |
5228 KB |
Output is correct |
42 |
Correct |
7 ms |
5228 KB |
Output is correct |
43 |
Correct |
9 ms |
5868 KB |
Output is correct |
44 |
Correct |
8 ms |
5228 KB |
Output is correct |
45 |
Correct |
269 ms |
21740 KB |
Output is correct |
46 |
Correct |
281 ms |
21740 KB |
Output is correct |
47 |
Correct |
280 ms |
21740 KB |
Output is correct |
48 |
Correct |
276 ms |
21740 KB |
Output is correct |
49 |
Correct |
265 ms |
21612 KB |
Output is correct |
50 |
Correct |
287 ms |
21356 KB |
Output is correct |
51 |
Correct |
276 ms |
21484 KB |
Output is correct |
52 |
Correct |
282 ms |
21868 KB |
Output is correct |
53 |
Correct |
280 ms |
21740 KB |
Output is correct |
54 |
Correct |
356 ms |
84844 KB |
Output is correct |
55 |
Correct |
339 ms |
71148 KB |
Output is correct |
56 |
Correct |
324 ms |
62168 KB |
Output is correct |
57 |
Correct |
331 ms |
55788 KB |
Output is correct |
58 |
Correct |
310 ms |
52716 KB |
Output is correct |
59 |
Correct |
302 ms |
53228 KB |
Output is correct |
60 |
Correct |
262 ms |
85516 KB |
Output is correct |
61 |
Correct |
323 ms |
21484 KB |
Output is correct |
62 |
Correct |
496 ms |
85100 KB |
Output is correct |
63 |
Correct |
307 ms |
21356 KB |
Output is correct |
64 |
Correct |
318 ms |
21100 KB |
Output is correct |
65 |
Correct |
476 ms |
85856 KB |
Output is correct |
66 |
Correct |
297 ms |
21356 KB |
Output is correct |