#include <bits/stdc++.h>
#ifdef LOCAL
#include <bits/debug.hpp>
#endif // LOCAL
#define ll long long
#define all(x) x.begin(), x.end()
#define Neco "Constellation 3"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 2e5 + 7
using namespace std;
const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;
int n, m, rt;
int a[maxn];
vector<pair<int, int>> store[maxn];
int cha[maxn][18], ma[maxn][18];
int In[maxn], Out[maxn];
ll dp[maxn][2];
struct dl { int p, c[2]; } T[maxn];
struct BIT {
ll bit[maxn];
void update(int l, int r, ll val) {
for( l; l < maxn; l += (l & -l)) bit[l] += val;
for(++ r; r < maxn; r += (r & -r)) bit[r] -= val;
}
ll get(int x, ll ans = 0) {
for(; x > 0; x -= (x & -x)) ans += bit[x];
return ans;
}
} Bit;
void BuildTree() {
a[0] = 1e9;
fi(i, 1, n) {
T[i].p = i - 1;
while(a[i] > a[T[i].p]) T[i].p = T[T[i].p].p;
T[i].c[0] = T[T[i].p].c[1];
T[T[i].c[0]].p = i;
T[T[i].p].c[1] = i;
}
fi(i, 1, n) if(T[i].p == 0) return rt = i, void();
}
void pre_dfs(int u) {
static int Time = 0;
In[u] = ++Time;
for(int v : T[u].c) if(v != 0) {
cha[v][0] = u, ma[v][0] = a[u];
fi(i, 1, 17) {
cha[v][i] = cha[cha[v][i - 1]][i - 1];
ma[v][i] = max(ma[v][i - 1], ma[cha[v][i - 1]][i - 1]);
}
pre_dfs(v);
}
Out[u] = Time;
}
void Add(int x, int h, int val) {
int u = x;
fid(i, 17, 0) if(ma[u][i] < h) u = cha[u][i];
store[u].push_back({x, val});
}
void dfs(int u) {
for(int v : T[u].c) if(v != 0) {
dfs(v);
dp[u][0] += dp[v][1];
}
Bit.update(In[u], Out[u], dp[u][0]);
dp[u][1] = dp[u][0];
for(auto [v, w] : store[u]) dp[u][1] = max(dp[u][1], Bit.get(In[v]) + w);
Bit.update(In[u], Out[u], -dp[u][1]);
}
void solve()
{
cin >> n;
fi(i, 1, n) cin >> a[i];
BuildTree();
memset(ma, 60, sizeof ma);
pre_dfs(rt);
cin >> m;
ll S = 0;
fi(i, 1, m) {
int x, y, val; cin >> x >> y >> val;
Add(x, y, val);
S += val;
}
dfs(rt);
cout << S - max(dp[rt][0], dp[rt][1]);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(Neco".inp", "r")) {
freopen(Neco".inp", "r", stdin);
freopen(Neco".out", "w", stdout);
}
int nTest = 1;
// cin >> nTest;
while(nTest--)
{
solve();
}
return 0;
}
Compilation message
constellation3.cpp: In member function 'void BIT::update(int, int, long long int)':
constellation3.cpp:35:16: warning: statement has no effect [-Wunused-value]
35 | for( l; l < maxn; l += (l & -l)) bit[l] += val;
| ^
constellation3.cpp: In function 'int main()':
constellation3.cpp:127:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | freopen(Neco".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
constellation3.cpp:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
128 | freopen(Neco".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
25180 KB |
Output is correct |
2 |
Correct |
3 ms |
25180 KB |
Output is correct |
3 |
Correct |
3 ms |
25180 KB |
Output is correct |
4 |
Correct |
3 ms |
25180 KB |
Output is correct |
5 |
Correct |
3 ms |
25180 KB |
Output is correct |
6 |
Correct |
3 ms |
25340 KB |
Output is correct |
7 |
Correct |
3 ms |
25344 KB |
Output is correct |
8 |
Correct |
3 ms |
25180 KB |
Output is correct |
9 |
Correct |
3 ms |
25348 KB |
Output is correct |
10 |
Correct |
3 ms |
25180 KB |
Output is correct |
11 |
Correct |
3 ms |
25276 KB |
Output is correct |
12 |
Correct |
3 ms |
25180 KB |
Output is correct |
13 |
Correct |
3 ms |
25180 KB |
Output is correct |
14 |
Correct |
3 ms |
25348 KB |
Output is correct |
15 |
Correct |
3 ms |
25180 KB |
Output is correct |
16 |
Correct |
3 ms |
25180 KB |
Output is correct |
17 |
Correct |
4 ms |
25180 KB |
Output is correct |
18 |
Correct |
4 ms |
25180 KB |
Output is correct |
19 |
Correct |
4 ms |
25324 KB |
Output is correct |
20 |
Correct |
3 ms |
25180 KB |
Output is correct |
21 |
Correct |
3 ms |
25180 KB |
Output is correct |
22 |
Correct |
3 ms |
25324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
25180 KB |
Output is correct |
2 |
Correct |
3 ms |
25180 KB |
Output is correct |
3 |
Correct |
3 ms |
25180 KB |
Output is correct |
4 |
Correct |
3 ms |
25180 KB |
Output is correct |
5 |
Correct |
3 ms |
25180 KB |
Output is correct |
6 |
Correct |
3 ms |
25340 KB |
Output is correct |
7 |
Correct |
3 ms |
25344 KB |
Output is correct |
8 |
Correct |
3 ms |
25180 KB |
Output is correct |
9 |
Correct |
3 ms |
25348 KB |
Output is correct |
10 |
Correct |
3 ms |
25180 KB |
Output is correct |
11 |
Correct |
3 ms |
25276 KB |
Output is correct |
12 |
Correct |
3 ms |
25180 KB |
Output is correct |
13 |
Correct |
3 ms |
25180 KB |
Output is correct |
14 |
Correct |
3 ms |
25348 KB |
Output is correct |
15 |
Correct |
3 ms |
25180 KB |
Output is correct |
16 |
Correct |
3 ms |
25180 KB |
Output is correct |
17 |
Correct |
4 ms |
25180 KB |
Output is correct |
18 |
Correct |
4 ms |
25180 KB |
Output is correct |
19 |
Correct |
4 ms |
25324 KB |
Output is correct |
20 |
Correct |
3 ms |
25180 KB |
Output is correct |
21 |
Correct |
3 ms |
25180 KB |
Output is correct |
22 |
Correct |
3 ms |
25324 KB |
Output is correct |
23 |
Correct |
5 ms |
27484 KB |
Output is correct |
24 |
Correct |
5 ms |
27484 KB |
Output is correct |
25 |
Correct |
4 ms |
27484 KB |
Output is correct |
26 |
Correct |
4 ms |
25436 KB |
Output is correct |
27 |
Correct |
4 ms |
27736 KB |
Output is correct |
28 |
Correct |
5 ms |
27564 KB |
Output is correct |
29 |
Correct |
4 ms |
27484 KB |
Output is correct |
30 |
Correct |
4 ms |
27388 KB |
Output is correct |
31 |
Correct |
4 ms |
27484 KB |
Output is correct |
32 |
Correct |
4 ms |
25608 KB |
Output is correct |
33 |
Correct |
4 ms |
27484 KB |
Output is correct |
34 |
Correct |
4 ms |
25436 KB |
Output is correct |
35 |
Correct |
4 ms |
27484 KB |
Output is correct |
36 |
Correct |
4 ms |
27484 KB |
Output is correct |
37 |
Correct |
4 ms |
25436 KB |
Output is correct |
38 |
Correct |
4 ms |
27484 KB |
Output is correct |
39 |
Correct |
5 ms |
27484 KB |
Output is correct |
40 |
Correct |
5 ms |
27484 KB |
Output is correct |
41 |
Correct |
4 ms |
25388 KB |
Output is correct |
42 |
Correct |
4 ms |
27484 KB |
Output is correct |
43 |
Correct |
5 ms |
25692 KB |
Output is correct |
44 |
Correct |
4 ms |
25436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
25180 KB |
Output is correct |
2 |
Correct |
3 ms |
25180 KB |
Output is correct |
3 |
Correct |
3 ms |
25180 KB |
Output is correct |
4 |
Correct |
3 ms |
25180 KB |
Output is correct |
5 |
Correct |
3 ms |
25180 KB |
Output is correct |
6 |
Correct |
3 ms |
25340 KB |
Output is correct |
7 |
Correct |
3 ms |
25344 KB |
Output is correct |
8 |
Correct |
3 ms |
25180 KB |
Output is correct |
9 |
Correct |
3 ms |
25348 KB |
Output is correct |
10 |
Correct |
3 ms |
25180 KB |
Output is correct |
11 |
Correct |
3 ms |
25276 KB |
Output is correct |
12 |
Correct |
3 ms |
25180 KB |
Output is correct |
13 |
Correct |
3 ms |
25180 KB |
Output is correct |
14 |
Correct |
3 ms |
25348 KB |
Output is correct |
15 |
Correct |
3 ms |
25180 KB |
Output is correct |
16 |
Correct |
3 ms |
25180 KB |
Output is correct |
17 |
Correct |
4 ms |
25180 KB |
Output is correct |
18 |
Correct |
4 ms |
25180 KB |
Output is correct |
19 |
Correct |
4 ms |
25324 KB |
Output is correct |
20 |
Correct |
3 ms |
25180 KB |
Output is correct |
21 |
Correct |
3 ms |
25180 KB |
Output is correct |
22 |
Correct |
3 ms |
25324 KB |
Output is correct |
23 |
Correct |
5 ms |
27484 KB |
Output is correct |
24 |
Correct |
5 ms |
27484 KB |
Output is correct |
25 |
Correct |
4 ms |
27484 KB |
Output is correct |
26 |
Correct |
4 ms |
25436 KB |
Output is correct |
27 |
Correct |
4 ms |
27736 KB |
Output is correct |
28 |
Correct |
5 ms |
27564 KB |
Output is correct |
29 |
Correct |
4 ms |
27484 KB |
Output is correct |
30 |
Correct |
4 ms |
27388 KB |
Output is correct |
31 |
Correct |
4 ms |
27484 KB |
Output is correct |
32 |
Correct |
4 ms |
25608 KB |
Output is correct |
33 |
Correct |
4 ms |
27484 KB |
Output is correct |
34 |
Correct |
4 ms |
25436 KB |
Output is correct |
35 |
Correct |
4 ms |
27484 KB |
Output is correct |
36 |
Correct |
4 ms |
27484 KB |
Output is correct |
37 |
Correct |
4 ms |
25436 KB |
Output is correct |
38 |
Correct |
4 ms |
27484 KB |
Output is correct |
39 |
Correct |
5 ms |
27484 KB |
Output is correct |
40 |
Correct |
5 ms |
27484 KB |
Output is correct |
41 |
Correct |
4 ms |
25388 KB |
Output is correct |
42 |
Correct |
4 ms |
27484 KB |
Output is correct |
43 |
Correct |
5 ms |
25692 KB |
Output is correct |
44 |
Correct |
4 ms |
25436 KB |
Output is correct |
45 |
Correct |
118 ms |
51796 KB |
Output is correct |
46 |
Correct |
134 ms |
51796 KB |
Output is correct |
47 |
Correct |
116 ms |
51836 KB |
Output is correct |
48 |
Correct |
127 ms |
51428 KB |
Output is correct |
49 |
Correct |
111 ms |
51796 KB |
Output is correct |
50 |
Correct |
122 ms |
51540 KB |
Output is correct |
51 |
Correct |
116 ms |
51540 KB |
Output is correct |
52 |
Correct |
147 ms |
51796 KB |
Output is correct |
53 |
Correct |
116 ms |
51568 KB |
Output is correct |
54 |
Correct |
208 ms |
59684 KB |
Output is correct |
55 |
Correct |
188 ms |
57428 KB |
Output is correct |
56 |
Correct |
201 ms |
55892 KB |
Output is correct |
57 |
Correct |
162 ms |
55124 KB |
Output is correct |
58 |
Correct |
126 ms |
56404 KB |
Output is correct |
59 |
Correct |
139 ms |
56404 KB |
Output is correct |
60 |
Correct |
85 ms |
62812 KB |
Output is correct |
61 |
Correct |
109 ms |
50260 KB |
Output is correct |
62 |
Correct |
171 ms |
61256 KB |
Output is correct |
63 |
Correct |
115 ms |
50312 KB |
Output is correct |
64 |
Correct |
117 ms |
50128 KB |
Output is correct |
65 |
Correct |
200 ms |
61140 KB |
Output is correct |
66 |
Correct |
126 ms |
50216 KB |
Output is correct |