/*
* In the name of God
*
* Author: Farbod Doost
* Last Modified: Sun, 19 Feb 2023 (16:27:05)
*
*/
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
//#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
//#pragma GCC optimize("fast-math")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
const int N = 2e5 + 5;
const long long inf = 1e18;
int pw[N];
int n, m, a[N], dp[N][20];
long long s[N];
vector <pair <int, int>> adj[N];
vector <int> v;
int mx(int l, int r)
{
if (a[dp[l][pw[r - l + 1]]] > a[dp[r - (1 << pw[r - l + 1]) + 1][pw[r - l + 1]]])
return dp[l][pw[r - l + 1]];
return dp[r - (1 << pw[r - l + 1]) + 1][pw[r - l + 1]];
}
int mn(int i, int x)
{
int j = upper_bound(adj[i].begin(), adj[i].end(), make_pair(x + 1, 0)) - adj[i].begin();
j--;
if (j < 0)
return 0;
return adj[i][j].second;
}
void f(int l, int r, vector <long long> &ans)
{
if (l > r) {
for (int i = 0; i < v.size(); i++)
ans.push_back(0);
v.pop_back();
return;
}
if (l == r) {
for (int i = 0; i < v.size(); i++)
ans.push_back(s[l] - mn(l, v[i]));
v.pop_back();
return;
}
int mid = mx(l, r);
vector <long long> ansL, ansR;
v.push_back(a[mid]), f(l, mid - 1, ansL);
v.push_back(a[mid]), f(mid + 1, r, ansR);
for (int i = 0; i < v.size(); i++) {
long long Ans = inf;
Ans = min(Ans, ansL[i] + ansR.back() + s[mid]),
Ans = min(Ans, ansL.back() + ansR[i] + s[mid]),
Ans = min(Ans, ansL.back() + ansR.back() + s[mid] - mn(mid, v[i]));
ans.push_back(Ans);
}
ansL.clear(), ansR.clear();
v.pop_back();
return;
}
signed main()
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
pw[1] = 0;
for (int i = 2; i < N; i++)
pw[i] = pw[i / 2] + 1;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i], dp[i][0] = i;
for (int j = 1; j < 20; j++)
for (int i = 0; i < n; i++) {
if (a[dp[i][j - 1]] > a[dp[min(n - 1, i + (1 << (j - 1)))][j - 1]])
dp[i][j] = dp[i][j - 1];
else
dp[i][j] = dp[min(n - 1, i + (1 << (j - 1)))][j - 1];
}
cin >> m;
for (int i = 0; i < m; i++) {
int x, y, c;
cin >> x >> y >> c, x--;
s[x] += c;
adj[x].push_back({y, c});
}
for (int i = 0; i < n; i++) {
sort(adj[i].begin(), adj[i].end());
for (int j = 1; j < adj[i].size(); j++)
adj[i][j].second = max(adj[i][j].second, adj[i][j - 1].second);
}
vector <long long> ans;
v.push_back(n), f(0, n - 1, ans);
cout << ans.back();
return 0;
}
Compilation message
constellation3.cpp: In function 'void f(int, int, std::vector<long long int>&)':
constellation3.cpp:49:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for (int i = 0; i < v.size(); i++)
| ~~^~~~~~~~~~
constellation3.cpp:56:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for (int i = 0; i < v.size(); i++)
| ~~^~~~~~~~~~
constellation3.cpp:69:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
constellation3.cpp: In function 'int main()':
constellation3.cpp:118:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
118 | for (int j = 1; j < adj[i].size(); j++)
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5844 KB |
Output is correct |
2 |
Correct |
3 ms |
5844 KB |
Output is correct |
3 |
Correct |
3 ms |
5820 KB |
Output is correct |
4 |
Correct |
3 ms |
5844 KB |
Output is correct |
5 |
Correct |
3 ms |
5820 KB |
Output is correct |
6 |
Correct |
3 ms |
5844 KB |
Output is correct |
7 |
Correct |
3 ms |
5844 KB |
Output is correct |
8 |
Correct |
4 ms |
5844 KB |
Output is correct |
9 |
Correct |
3 ms |
5808 KB |
Output is correct |
10 |
Correct |
3 ms |
5972 KB |
Output is correct |
11 |
Correct |
4 ms |
5844 KB |
Output is correct |
12 |
Correct |
3 ms |
5844 KB |
Output is correct |
13 |
Correct |
4 ms |
5844 KB |
Output is correct |
14 |
Correct |
3 ms |
5844 KB |
Output is correct |
15 |
Correct |
4 ms |
5844 KB |
Output is correct |
16 |
Correct |
5 ms |
5844 KB |
Output is correct |
17 |
Correct |
4 ms |
5812 KB |
Output is correct |
18 |
Correct |
5 ms |
5972 KB |
Output is correct |
19 |
Correct |
3 ms |
5820 KB |
Output is correct |
20 |
Correct |
4 ms |
5844 KB |
Output is correct |
21 |
Correct |
4 ms |
5948 KB |
Output is correct |
22 |
Correct |
4 ms |
5844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5844 KB |
Output is correct |
2 |
Correct |
3 ms |
5844 KB |
Output is correct |
3 |
Correct |
3 ms |
5820 KB |
Output is correct |
4 |
Correct |
3 ms |
5844 KB |
Output is correct |
5 |
Correct |
3 ms |
5820 KB |
Output is correct |
6 |
Correct |
3 ms |
5844 KB |
Output is correct |
7 |
Correct |
3 ms |
5844 KB |
Output is correct |
8 |
Correct |
4 ms |
5844 KB |
Output is correct |
9 |
Correct |
3 ms |
5808 KB |
Output is correct |
10 |
Correct |
3 ms |
5972 KB |
Output is correct |
11 |
Correct |
4 ms |
5844 KB |
Output is correct |
12 |
Correct |
3 ms |
5844 KB |
Output is correct |
13 |
Correct |
4 ms |
5844 KB |
Output is correct |
14 |
Correct |
3 ms |
5844 KB |
Output is correct |
15 |
Correct |
4 ms |
5844 KB |
Output is correct |
16 |
Correct |
5 ms |
5844 KB |
Output is correct |
17 |
Correct |
4 ms |
5812 KB |
Output is correct |
18 |
Correct |
5 ms |
5972 KB |
Output is correct |
19 |
Correct |
3 ms |
5820 KB |
Output is correct |
20 |
Correct |
4 ms |
5844 KB |
Output is correct |
21 |
Correct |
4 ms |
5948 KB |
Output is correct |
22 |
Correct |
4 ms |
5844 KB |
Output is correct |
23 |
Correct |
5 ms |
6040 KB |
Output is correct |
24 |
Correct |
6 ms |
6100 KB |
Output is correct |
25 |
Correct |
5 ms |
5972 KB |
Output is correct |
26 |
Correct |
5 ms |
6084 KB |
Output is correct |
27 |
Correct |
6 ms |
6036 KB |
Output is correct |
28 |
Correct |
5 ms |
6080 KB |
Output is correct |
29 |
Correct |
5 ms |
5972 KB |
Output is correct |
30 |
Correct |
5 ms |
6084 KB |
Output is correct |
31 |
Correct |
6 ms |
6092 KB |
Output is correct |
32 |
Correct |
18 ms |
10964 KB |
Output is correct |
33 |
Correct |
14 ms |
8768 KB |
Output is correct |
34 |
Correct |
15 ms |
8032 KB |
Output is correct |
35 |
Correct |
12 ms |
7380 KB |
Output is correct |
36 |
Correct |
17 ms |
6364 KB |
Output is correct |
37 |
Correct |
18 ms |
6440 KB |
Output is correct |
38 |
Correct |
32 ms |
6524 KB |
Output is correct |
39 |
Correct |
5 ms |
6100 KB |
Output is correct |
40 |
Correct |
22 ms |
12372 KB |
Output is correct |
41 |
Correct |
6 ms |
6100 KB |
Output is correct |
42 |
Correct |
5 ms |
6036 KB |
Output is correct |
43 |
Correct |
23 ms |
12484 KB |
Output is correct |
44 |
Correct |
7 ms |
6084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5844 KB |
Output is correct |
2 |
Correct |
3 ms |
5844 KB |
Output is correct |
3 |
Correct |
3 ms |
5820 KB |
Output is correct |
4 |
Correct |
3 ms |
5844 KB |
Output is correct |
5 |
Correct |
3 ms |
5820 KB |
Output is correct |
6 |
Correct |
3 ms |
5844 KB |
Output is correct |
7 |
Correct |
3 ms |
5844 KB |
Output is correct |
8 |
Correct |
4 ms |
5844 KB |
Output is correct |
9 |
Correct |
3 ms |
5808 KB |
Output is correct |
10 |
Correct |
3 ms |
5972 KB |
Output is correct |
11 |
Correct |
4 ms |
5844 KB |
Output is correct |
12 |
Correct |
3 ms |
5844 KB |
Output is correct |
13 |
Correct |
4 ms |
5844 KB |
Output is correct |
14 |
Correct |
3 ms |
5844 KB |
Output is correct |
15 |
Correct |
4 ms |
5844 KB |
Output is correct |
16 |
Correct |
5 ms |
5844 KB |
Output is correct |
17 |
Correct |
4 ms |
5812 KB |
Output is correct |
18 |
Correct |
5 ms |
5972 KB |
Output is correct |
19 |
Correct |
3 ms |
5820 KB |
Output is correct |
20 |
Correct |
4 ms |
5844 KB |
Output is correct |
21 |
Correct |
4 ms |
5948 KB |
Output is correct |
22 |
Correct |
4 ms |
5844 KB |
Output is correct |
23 |
Correct |
5 ms |
6040 KB |
Output is correct |
24 |
Correct |
6 ms |
6100 KB |
Output is correct |
25 |
Correct |
5 ms |
5972 KB |
Output is correct |
26 |
Correct |
5 ms |
6084 KB |
Output is correct |
27 |
Correct |
6 ms |
6036 KB |
Output is correct |
28 |
Correct |
5 ms |
6080 KB |
Output is correct |
29 |
Correct |
5 ms |
5972 KB |
Output is correct |
30 |
Correct |
5 ms |
6084 KB |
Output is correct |
31 |
Correct |
6 ms |
6092 KB |
Output is correct |
32 |
Correct |
18 ms |
10964 KB |
Output is correct |
33 |
Correct |
14 ms |
8768 KB |
Output is correct |
34 |
Correct |
15 ms |
8032 KB |
Output is correct |
35 |
Correct |
12 ms |
7380 KB |
Output is correct |
36 |
Correct |
17 ms |
6364 KB |
Output is correct |
37 |
Correct |
18 ms |
6440 KB |
Output is correct |
38 |
Correct |
32 ms |
6524 KB |
Output is correct |
39 |
Correct |
5 ms |
6100 KB |
Output is correct |
40 |
Correct |
22 ms |
12372 KB |
Output is correct |
41 |
Correct |
6 ms |
6100 KB |
Output is correct |
42 |
Correct |
5 ms |
6036 KB |
Output is correct |
43 |
Correct |
23 ms |
12484 KB |
Output is correct |
44 |
Correct |
7 ms |
6084 KB |
Output is correct |
45 |
Correct |
256 ms |
33400 KB |
Output is correct |
46 |
Correct |
249 ms |
33060 KB |
Output is correct |
47 |
Correct |
267 ms |
32960 KB |
Output is correct |
48 |
Correct |
239 ms |
33256 KB |
Output is correct |
49 |
Correct |
253 ms |
32752 KB |
Output is correct |
50 |
Correct |
242 ms |
32672 KB |
Output is correct |
51 |
Correct |
228 ms |
32664 KB |
Output is correct |
52 |
Correct |
226 ms |
33416 KB |
Output is correct |
53 |
Correct |
257 ms |
33208 KB |
Output is correct |
54 |
Runtime error |
564 ms |
524288 KB |
Execution killed with signal 9 |
55 |
Halted |
0 ms |
0 KB |
- |