#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
typedef long long ll;
using namespace std;
const int maxn = 1 << 18;
ll tree[maxn * 2];
void range_add(int l, int r, ll d)
{
for (l += maxn, r += maxn+1; l < r; l >>= 1, r >>= 1)
{
if (l & 1) tree[l++] += d;
if (r & 1) tree[--r] += d;
}
}
ll query(int i)
{
ll ans = 0;
for (i += maxn; i > 0; i >>= 1) ans += tree[i];
return ans;
}
struct star { int x, y, c; };
bool cmp(const star& a, const star& b) { return (a.y < b.y || (a.y == b.y && a.x < b.x)); }
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n + 2, n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
int m;
cin >> m;
vector<int> l(m, -1), r(m, -1);
ll ans = 0;
vector<star> s(m);
for (int i = 0; i < m; i++) cin >> s[i].x >> s[i].y >> s[i].c, ans += s[i].c;
sort(s.begin(), s.end(), cmp);
vector<vector<int> > v(n + 2);
for (int i = 0; i < m; i++) v[s[i].x].push_back(i);
vector<int> stk = {0};
set<pair<int, int> > tall = { { a[0], 0 } };
for (int i = 1; i <= n; i++)
{
while (!stk.empty() && a[stk.back()] <= a[i]) tall.erase({ a[stk.back()], stk.back() }), stk.pop_back();
for (int j : v[i])
l[j] = tall.upper_bound({ s[j].y, -1 })->second;
tall.insert({ a[i], i });
stk.push_back(i);
}
stk = { n + 1 }, tall = { { a[n + 1], n + 1 } };
for (int i = n; i >= 1; i--)
{
while (!stk.empty() && a[stk.back()] <= a[i]) tall.erase({ a[stk.back()], stk.back() }), stk.pop_back();
for (int j : v[i])
r[j] = tall.upper_bound({ s[j].y, -1 })->second;
tall.insert({ a[i], i });
stk.push_back(i);
}
for (int i = 0; i < m; i++)
{
ll val = (ll)s[i].c - query(s[i].x);
if (val > 0)
{
range_add(l[i] + 1, r[i] - 1, val);
ans -= val;
}
/*for (int j = 0; j < n; j++) cout << query(j) << " ";
cout << "\n";*/
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
2 ms |
460 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
3 ms |
460 KB |
Output is correct |
27 |
Correct |
2 ms |
460 KB |
Output is correct |
28 |
Correct |
2 ms |
460 KB |
Output is correct |
29 |
Correct |
2 ms |
460 KB |
Output is correct |
30 |
Correct |
3 ms |
460 KB |
Output is correct |
31 |
Correct |
3 ms |
460 KB |
Output is correct |
32 |
Correct |
3 ms |
460 KB |
Output is correct |
33 |
Correct |
3 ms |
460 KB |
Output is correct |
34 |
Correct |
3 ms |
460 KB |
Output is correct |
35 |
Correct |
3 ms |
460 KB |
Output is correct |
36 |
Correct |
2 ms |
460 KB |
Output is correct |
37 |
Correct |
2 ms |
460 KB |
Output is correct |
38 |
Correct |
2 ms |
588 KB |
Output is correct |
39 |
Correct |
2 ms |
460 KB |
Output is correct |
40 |
Correct |
3 ms |
460 KB |
Output is correct |
41 |
Correct |
2 ms |
460 KB |
Output is correct |
42 |
Correct |
2 ms |
460 KB |
Output is correct |
43 |
Correct |
3 ms |
460 KB |
Output is correct |
44 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
2 ms |
460 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
3 ms |
460 KB |
Output is correct |
27 |
Correct |
2 ms |
460 KB |
Output is correct |
28 |
Correct |
2 ms |
460 KB |
Output is correct |
29 |
Correct |
2 ms |
460 KB |
Output is correct |
30 |
Correct |
3 ms |
460 KB |
Output is correct |
31 |
Correct |
3 ms |
460 KB |
Output is correct |
32 |
Correct |
3 ms |
460 KB |
Output is correct |
33 |
Correct |
3 ms |
460 KB |
Output is correct |
34 |
Correct |
3 ms |
460 KB |
Output is correct |
35 |
Correct |
3 ms |
460 KB |
Output is correct |
36 |
Correct |
2 ms |
460 KB |
Output is correct |
37 |
Correct |
2 ms |
460 KB |
Output is correct |
38 |
Correct |
2 ms |
588 KB |
Output is correct |
39 |
Correct |
2 ms |
460 KB |
Output is correct |
40 |
Correct |
3 ms |
460 KB |
Output is correct |
41 |
Correct |
2 ms |
460 KB |
Output is correct |
42 |
Correct |
2 ms |
460 KB |
Output is correct |
43 |
Correct |
3 ms |
460 KB |
Output is correct |
44 |
Correct |
2 ms |
460 KB |
Output is correct |
45 |
Correct |
229 ms |
16592 KB |
Output is correct |
46 |
Correct |
233 ms |
21968 KB |
Output is correct |
47 |
Correct |
242 ms |
22188 KB |
Output is correct |
48 |
Correct |
234 ms |
21860 KB |
Output is correct |
49 |
Correct |
236 ms |
21736 KB |
Output is correct |
50 |
Correct |
236 ms |
21552 KB |
Output is correct |
51 |
Correct |
231 ms |
21792 KB |
Output is correct |
52 |
Correct |
234 ms |
22212 KB |
Output is correct |
53 |
Correct |
259 ms |
22080 KB |
Output is correct |
54 |
Correct |
379 ms |
25432 KB |
Output is correct |
55 |
Correct |
369 ms |
24384 KB |
Output is correct |
56 |
Correct |
338 ms |
23616 KB |
Output is correct |
57 |
Correct |
337 ms |
23264 KB |
Output is correct |
58 |
Correct |
248 ms |
21452 KB |
Output is correct |
59 |
Correct |
244 ms |
21608 KB |
Output is correct |
60 |
Correct |
200 ms |
29112 KB |
Output is correct |
61 |
Correct |
213 ms |
21656 KB |
Output is correct |
62 |
Correct |
367 ms |
25624 KB |
Output is correct |
63 |
Correct |
199 ms |
21708 KB |
Output is correct |
64 |
Correct |
209 ms |
21172 KB |
Output is correct |
65 |
Correct |
380 ms |
25692 KB |
Output is correct |
66 |
Correct |
203 ms |
21752 KB |
Output is correct |