#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 2e5;
const ll G = 2*N;
ll n,m;
vector <ll> e[G];
ll v[N];
ll leftid[N],rightid[N];
ll leftside[N + 1],rightside[N + 1];
///
ll stk[N];
vector <ll> p;
struct star{
ll x,y,c;
}v2[N];
vector <ll> cand[G];
map<ll,pair<ll,ll>> f;
vector <ll> polls;
ll dp[G];
ll cnt = 0;
void addedge(ll u,ll w){
e[u].push_back(w);
}
void dfs(ll node){
ll mx = 0;
for(auto i:e[node]){
dfs(i);
mx+=dp[i];
}
for(auto i:cand[node]){
///leftadd
star s = v2[i];
ll candcost = s.c;
for(ll j = s.x;j != n && v[j] < s.y;j = rightside[j]){
if(rightid[j] != -1){candcost+=dp[rightid[j]];};
}
for(ll j = s.x;j != n && v[j] < s.y;j = leftside[j]){
if(leftid[j] != -1){candcost+=dp[leftid[j]];};
}
mx = max(mx,candcost);
}
dp[node] = mx;
}
int main(){
cin>>n;
p.resize(n);
for(ll i = 0;i < n;i++){
cin>>v[i];
p[i] = i;
leftid[i] = rightid[i] = -1;
}
sort(p.begin(),p.end(),[&](ll a,ll b){
if(v[a] == v[b])return a < b;
return v[a] > v[b];
});
cin>>m;
ll sum = 0;
for(ll i = 0;i < m;i++){
cin>>v2[i].x>>v2[i].y>>v2[i].c;
sum+=v2[i].c;
v2[i].x--;
}
sort(v2,v2 + m,[&](star a,star b){
return a.y > b.y;
});
f[0] = {n - 1,cnt++};
ll pt = 0;
ll pt2 = 0;
for(ll i = n;i >= 1;i--){
polls.clear();
while(pt2 < n && v[p[pt2]] >= i){
polls.push_back(p[pt2]);
pt2++;
}
ll j = 0;
while(j < polls.size()){
auto it = prev(f.upper_bound(polls[j]));
ll k = j;
while(k < polls.size() && polls[k] <= it->second.first){
k++;
}
ll l = it->first;
ll r = it->second.first;
ll id = it->second.second;
f.erase(it);
for(ll p = j;p <= k;p++){
ll lside = (p == j?l:polls[p - 1] + 1);
ll rside = (p == k?r:polls[p] - 1);
if(lside <= rside){
addedge(id,cnt);
f[lside] = {rside,cnt};
if(p != k){
leftid[rside + 1] = cnt;
}
if(p != j){
rightid[lside - 1] = cnt;
}
cnt++;
}
}
j = k;
}
while(pt < m && v2[pt].y >= i){
ll id = prev(f.upper_bound(v2[pt].x))->second.second;
cand[id].push_back(pt);
pt++;
}
}
ll cnt2 = 0;
for(ll i = 0;i < n;i++){
while(cnt2 > 0 && v[i] > v[stk[cnt2 - 1]]){
cnt2--;
}
leftside[i] = (cnt2 == 0?n:stk[cnt2 - 1]);
stk[cnt2++] = i;
}
cnt2 = 0;
for(ll i = n - 1;i >= 0;i--){
while(cnt2 > 0 && v[i] > v[stk[cnt2 - 1]]){
cnt2--;
}
rightside[i] = (cnt2 == 0?n:stk[cnt2 - 1]);
stk[cnt2++] = i;
}
dfs(0);
cout<<sum - dp[0];
return 0;
}
Compilation message
constellation3.cpp: In function 'int main()':
constellation3.cpp:77:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | while(j < polls.size()){
| ~~^~~~~~~~~~~~~~
constellation3.cpp:80:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | while(k < polls.size() && polls[k] <= it->second.first){
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
32092 KB |
Output is correct |
2 |
Correct |
5 ms |
32092 KB |
Output is correct |
3 |
Correct |
5 ms |
32092 KB |
Output is correct |
4 |
Correct |
5 ms |
32148 KB |
Output is correct |
5 |
Correct |
5 ms |
32092 KB |
Output is correct |
6 |
Correct |
5 ms |
32144 KB |
Output is correct |
7 |
Correct |
5 ms |
32092 KB |
Output is correct |
8 |
Correct |
4 ms |
32092 KB |
Output is correct |
9 |
Correct |
5 ms |
32092 KB |
Output is correct |
10 |
Correct |
5 ms |
32088 KB |
Output is correct |
11 |
Correct |
4 ms |
32092 KB |
Output is correct |
12 |
Correct |
4 ms |
32164 KB |
Output is correct |
13 |
Correct |
4 ms |
32092 KB |
Output is correct |
14 |
Correct |
5 ms |
32088 KB |
Output is correct |
15 |
Correct |
5 ms |
32092 KB |
Output is correct |
16 |
Correct |
6 ms |
32212 KB |
Output is correct |
17 |
Correct |
5 ms |
32092 KB |
Output is correct |
18 |
Correct |
5 ms |
32088 KB |
Output is correct |
19 |
Correct |
6 ms |
32092 KB |
Output is correct |
20 |
Correct |
5 ms |
32092 KB |
Output is correct |
21 |
Correct |
5 ms |
32092 KB |
Output is correct |
22 |
Correct |
5 ms |
32088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
32092 KB |
Output is correct |
2 |
Correct |
5 ms |
32092 KB |
Output is correct |
3 |
Correct |
5 ms |
32092 KB |
Output is correct |
4 |
Correct |
5 ms |
32148 KB |
Output is correct |
5 |
Correct |
5 ms |
32092 KB |
Output is correct |
6 |
Correct |
5 ms |
32144 KB |
Output is correct |
7 |
Correct |
5 ms |
32092 KB |
Output is correct |
8 |
Correct |
4 ms |
32092 KB |
Output is correct |
9 |
Correct |
5 ms |
32092 KB |
Output is correct |
10 |
Correct |
5 ms |
32088 KB |
Output is correct |
11 |
Correct |
4 ms |
32092 KB |
Output is correct |
12 |
Correct |
4 ms |
32164 KB |
Output is correct |
13 |
Correct |
4 ms |
32092 KB |
Output is correct |
14 |
Correct |
5 ms |
32088 KB |
Output is correct |
15 |
Correct |
5 ms |
32092 KB |
Output is correct |
16 |
Correct |
6 ms |
32212 KB |
Output is correct |
17 |
Correct |
5 ms |
32092 KB |
Output is correct |
18 |
Correct |
5 ms |
32088 KB |
Output is correct |
19 |
Correct |
6 ms |
32092 KB |
Output is correct |
20 |
Correct |
5 ms |
32092 KB |
Output is correct |
21 |
Correct |
5 ms |
32092 KB |
Output is correct |
22 |
Correct |
5 ms |
32088 KB |
Output is correct |
23 |
Correct |
7 ms |
32348 KB |
Output is correct |
24 |
Correct |
7 ms |
32340 KB |
Output is correct |
25 |
Correct |
7 ms |
32132 KB |
Output is correct |
26 |
Correct |
7 ms |
32348 KB |
Output is correct |
27 |
Correct |
10 ms |
32344 KB |
Output is correct |
28 |
Correct |
7 ms |
32344 KB |
Output is correct |
29 |
Correct |
7 ms |
32176 KB |
Output is correct |
30 |
Correct |
8 ms |
32348 KB |
Output is correct |
31 |
Correct |
7 ms |
32348 KB |
Output is correct |
32 |
Correct |
7 ms |
32348 KB |
Output is correct |
33 |
Correct |
7 ms |
32348 KB |
Output is correct |
34 |
Correct |
7 ms |
32372 KB |
Output is correct |
35 |
Correct |
8 ms |
32348 KB |
Output is correct |
36 |
Correct |
8 ms |
32092 KB |
Output is correct |
37 |
Correct |
9 ms |
32140 KB |
Output is correct |
38 |
Correct |
6 ms |
32348 KB |
Output is correct |
39 |
Correct |
7 ms |
32228 KB |
Output is correct |
40 |
Correct |
9 ms |
32220 KB |
Output is correct |
41 |
Correct |
6 ms |
32092 KB |
Output is correct |
42 |
Correct |
7 ms |
32220 KB |
Output is correct |
43 |
Correct |
8 ms |
32224 KB |
Output is correct |
44 |
Correct |
6 ms |
32092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
32092 KB |
Output is correct |
2 |
Correct |
5 ms |
32092 KB |
Output is correct |
3 |
Correct |
5 ms |
32092 KB |
Output is correct |
4 |
Correct |
5 ms |
32148 KB |
Output is correct |
5 |
Correct |
5 ms |
32092 KB |
Output is correct |
6 |
Correct |
5 ms |
32144 KB |
Output is correct |
7 |
Correct |
5 ms |
32092 KB |
Output is correct |
8 |
Correct |
4 ms |
32092 KB |
Output is correct |
9 |
Correct |
5 ms |
32092 KB |
Output is correct |
10 |
Correct |
5 ms |
32088 KB |
Output is correct |
11 |
Correct |
4 ms |
32092 KB |
Output is correct |
12 |
Correct |
4 ms |
32164 KB |
Output is correct |
13 |
Correct |
4 ms |
32092 KB |
Output is correct |
14 |
Correct |
5 ms |
32088 KB |
Output is correct |
15 |
Correct |
5 ms |
32092 KB |
Output is correct |
16 |
Correct |
6 ms |
32212 KB |
Output is correct |
17 |
Correct |
5 ms |
32092 KB |
Output is correct |
18 |
Correct |
5 ms |
32088 KB |
Output is correct |
19 |
Correct |
6 ms |
32092 KB |
Output is correct |
20 |
Correct |
5 ms |
32092 KB |
Output is correct |
21 |
Correct |
5 ms |
32092 KB |
Output is correct |
22 |
Correct |
5 ms |
32088 KB |
Output is correct |
23 |
Correct |
7 ms |
32348 KB |
Output is correct |
24 |
Correct |
7 ms |
32340 KB |
Output is correct |
25 |
Correct |
7 ms |
32132 KB |
Output is correct |
26 |
Correct |
7 ms |
32348 KB |
Output is correct |
27 |
Correct |
10 ms |
32344 KB |
Output is correct |
28 |
Correct |
7 ms |
32344 KB |
Output is correct |
29 |
Correct |
7 ms |
32176 KB |
Output is correct |
30 |
Correct |
8 ms |
32348 KB |
Output is correct |
31 |
Correct |
7 ms |
32348 KB |
Output is correct |
32 |
Correct |
7 ms |
32348 KB |
Output is correct |
33 |
Correct |
7 ms |
32348 KB |
Output is correct |
34 |
Correct |
7 ms |
32372 KB |
Output is correct |
35 |
Correct |
8 ms |
32348 KB |
Output is correct |
36 |
Correct |
8 ms |
32092 KB |
Output is correct |
37 |
Correct |
9 ms |
32140 KB |
Output is correct |
38 |
Correct |
6 ms |
32348 KB |
Output is correct |
39 |
Correct |
7 ms |
32228 KB |
Output is correct |
40 |
Correct |
9 ms |
32220 KB |
Output is correct |
41 |
Correct |
6 ms |
32092 KB |
Output is correct |
42 |
Correct |
7 ms |
32220 KB |
Output is correct |
43 |
Correct |
8 ms |
32224 KB |
Output is correct |
44 |
Correct |
6 ms |
32092 KB |
Output is correct |
45 |
Correct |
395 ms |
53588 KB |
Output is correct |
46 |
Correct |
363 ms |
53328 KB |
Output is correct |
47 |
Correct |
374 ms |
53376 KB |
Output is correct |
48 |
Correct |
368 ms |
53072 KB |
Output is correct |
49 |
Correct |
356 ms |
53072 KB |
Output is correct |
50 |
Correct |
363 ms |
52820 KB |
Output is correct |
51 |
Correct |
358 ms |
53072 KB |
Output is correct |
52 |
Correct |
370 ms |
53332 KB |
Output is correct |
53 |
Correct |
387 ms |
53328 KB |
Output is correct |
54 |
Execution timed out |
1565 ms |
61268 KB |
Time limit exceeded |
55 |
Halted |
0 ms |
0 KB |
- |