#include <cstdio>
#include <vector>
#include <map>
#include <stack>
#include <algorithm>
#define PB push_back
#define X first
#define Y second
using namespace std;
typedef pair < int, int > pii;
typedef vector < int > vi;
typedef long long ll;
const int N = 2e5 + 500;
const int LOG = 18;
map < int, ll > dp[N];
map < int, vi > sjec[N];
int n, m, kol[N];
int A[N], zvj[N], C[N];
int L[N], R[N], pos[N];
int rmq[N][LOG];
int par[N][2];
vector < pii > bitni;
vi T[N][2];
int Q_L[N][2], Q_R[N][2], cur = 0;
ll loga[N][2];
bool cmp(const pii &A,const pii &B){
return A.Y - A.X < B.Y - B.X;
}
void dfs(int x,int fl){
Q_L[x][fl] = cur++;
for(int y : T[x][fl])
dfs(y, fl);
Q_R[x][fl] = cur;
}
void add(int x,int fl, ll y){
for(x += 10; x < N ; x += x & -x)
loga[x][fl] += y;
}
ll get(int x, int fl){
ll ret = 0;
for(x += 10; x ; x -= x & -x)
ret += loga[x][fl];
return ret;
}
inline int maax(int i,int j){
if(A[i] > A[j])
return i;
return j;
}
inline int get_maax(int l,int r){
return maax(rmq[l][kol[r - l + 1]], rmq[r - (1 << kol[r - l + 1]) + 1][kol[r - l + 1]]);
}
ll svi_C = 0;
ll f(int l,int r){
if(r < l) return 0;
if(dp[l][r] != 0) return dp[l][r] - 1;
int mks = get_maax(l, r);
ll ret = f(l, mks - 1) + f(mks + 1, r);
for(int x : sjec[l][r])
ret = max(ret, f(l, pos[x] - 1) + f(pos[x] + 1, r) + C[x]);
dp[l][r] = ret + 1;
return ret;
}
int main(){
scanf("%d", &n);
vector < pii > saz;
for(int i = 1;i <= n;i++){
scanf("%d", A + i);
rmq[i][0] = i;
}
scanf("%d", &m);
for(int i = 1;i <= n;i++)
saz.PB({A[i], i + m});
for(int i = 0;i < m;i++){
scanf("%d%d%d", pos + i, zvj + i, C + i);
saz.PB({zvj[i], i});
}
sort(saz.begin(), saz.end());
for(int i = 0;i < n + m;i++){
if(saz[i].Y < m)
zvj[saz[i].Y] = i + 1;
else
A[saz[i].Y - m] = i + 1;
}
A[0] = n + m + 1, A[n + 1] = n + m + 1;
stack < int > s; s.push(0);
for(int i = 1;i <= n;i++){
for(;A[s.top()] < A[i];s.pop());
par[i][0] = s.top(); s.push(i);
if(par[i][0] < i - 1)
bitni.PB({par[i][0] , i});
T[par[i][0]][0].PB(i);
}
bitni.PB({0, n + 1});
for(;!s.empty();s.pop());
s.push(n + 1);
for(int i = n;i >= 1;i--){
for(;A[s.top()] < A[i];s.pop());
par[i][1] = s.top(); s.push(i);
if(par[i][1] > i + 1)
bitni.PB({i , par[i][1]});
T[par[i][1]][1].PB(i);
}
dfs(0, 0); cur = 0; dfs(n + 1, 1);
for(int i = 1;i <= n + 2;i++){
{for(;(1 << kol[i]) <= i;kol[i]++); kol[i]--;}
rmq[i][0] = i;
}
for(int j = 1;j < LOG;j++){
for(int i = 0;i <= n + 1;i++){
rmq[i][j] = rmq[i][j - 1];
if(i + (1 << (j - 1)) <= n + 1)
rmq[i][j] = maax(rmq[i][j], rmq[i + (1 << (j - 1))][j - 1]);
}
}
for(int i = 0;i < m;i++){
L[i] = pos[i], R[i] = pos[i];
for(int j = LOG - 1;j >= 0;j--){
if(L[i] - (1 << j) >= 1 && A[get_maax(L[i] - (1 << j), pos[i])] < zvj[i])
L[i] -= (1 << j);
if(R[i] + (1 << j) <= n && A[get_maax(pos[i], R[i] + (1 << j))] < zvj[i])
R[i] += (1 << j);
}
L[i]--, R[i]++;
sjec[L[i]][R[i]].PB(i); svi_C += C[i];
}
sort(bitni.begin(), bitni.end(), cmp);
for(pii ja : bitni){
int l = ja.X, r = ja.Y;
int mks = get_maax(l + 1, r - 1);
ll cur = dp[l][mks] + dp[mks][r];
for(int x : sjec[l][r]){
cur = max(cur, (ll)C[x] + get(Q_L[pos[x]][0], 0) + get(Q_L[pos[x]][1], 1) - get(Q_L[l][0], 0) - get(Q_L[r][1], 1));
}
dp[l][r] = cur;
if(A[l] > A[r]){
add(Q_L[r][0], 0, cur);
add(Q_R[r][0], 0, -cur);
}
if(A[l] < A[r]){
add(Q_L[l][1], 1, cur);
add(Q_R[l][1], 1, -cur);
}
}
printf("%lld\n", svi_C - dp[0][n + 1]);
}
Compilation message
constellation3.cpp: In function 'int main()':
constellation3.cpp:82:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
constellation3.cpp:85:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", A + i);
~~~~~^~~~~~~~~~~~~
constellation3.cpp:89:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &m);
~~~~~^~~~~~~~~~
constellation3.cpp:93:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", pos + i, zvj + i, C + i);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
28800 KB |
Output is correct |
2 |
Correct |
23 ms |
28744 KB |
Output is correct |
3 |
Correct |
24 ms |
28800 KB |
Output is correct |
4 |
Correct |
23 ms |
28756 KB |
Output is correct |
5 |
Correct |
28 ms |
28800 KB |
Output is correct |
6 |
Correct |
30 ms |
28800 KB |
Output is correct |
7 |
Correct |
23 ms |
28800 KB |
Output is correct |
8 |
Correct |
23 ms |
28800 KB |
Output is correct |
9 |
Correct |
22 ms |
28800 KB |
Output is correct |
10 |
Correct |
22 ms |
28800 KB |
Output is correct |
11 |
Correct |
21 ms |
28800 KB |
Output is correct |
12 |
Correct |
32 ms |
28800 KB |
Output is correct |
13 |
Correct |
21 ms |
28800 KB |
Output is correct |
14 |
Correct |
21 ms |
28800 KB |
Output is correct |
15 |
Correct |
21 ms |
28800 KB |
Output is correct |
16 |
Correct |
27 ms |
28792 KB |
Output is correct |
17 |
Correct |
24 ms |
28800 KB |
Output is correct |
18 |
Correct |
35 ms |
28800 KB |
Output is correct |
19 |
Correct |
28 ms |
28796 KB |
Output is correct |
20 |
Correct |
27 ms |
28792 KB |
Output is correct |
21 |
Correct |
26 ms |
28776 KB |
Output is correct |
22 |
Correct |
22 ms |
28800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
28800 KB |
Output is correct |
2 |
Correct |
23 ms |
28744 KB |
Output is correct |
3 |
Correct |
24 ms |
28800 KB |
Output is correct |
4 |
Correct |
23 ms |
28756 KB |
Output is correct |
5 |
Correct |
28 ms |
28800 KB |
Output is correct |
6 |
Correct |
30 ms |
28800 KB |
Output is correct |
7 |
Correct |
23 ms |
28800 KB |
Output is correct |
8 |
Correct |
23 ms |
28800 KB |
Output is correct |
9 |
Correct |
22 ms |
28800 KB |
Output is correct |
10 |
Correct |
22 ms |
28800 KB |
Output is correct |
11 |
Correct |
21 ms |
28800 KB |
Output is correct |
12 |
Correct |
32 ms |
28800 KB |
Output is correct |
13 |
Correct |
21 ms |
28800 KB |
Output is correct |
14 |
Correct |
21 ms |
28800 KB |
Output is correct |
15 |
Correct |
21 ms |
28800 KB |
Output is correct |
16 |
Correct |
27 ms |
28792 KB |
Output is correct |
17 |
Correct |
24 ms |
28800 KB |
Output is correct |
18 |
Correct |
35 ms |
28800 KB |
Output is correct |
19 |
Correct |
28 ms |
28796 KB |
Output is correct |
20 |
Correct |
27 ms |
28792 KB |
Output is correct |
21 |
Correct |
26 ms |
28776 KB |
Output is correct |
22 |
Correct |
22 ms |
28800 KB |
Output is correct |
23 |
Correct |
28 ms |
29568 KB |
Output is correct |
24 |
Correct |
28 ms |
29440 KB |
Output is correct |
25 |
Correct |
32 ms |
29568 KB |
Output is correct |
26 |
Correct |
30 ms |
29560 KB |
Output is correct |
27 |
Correct |
26 ms |
29568 KB |
Output is correct |
28 |
Correct |
30 ms |
29440 KB |
Output is correct |
29 |
Correct |
25 ms |
29568 KB |
Output is correct |
30 |
Correct |
33 ms |
29596 KB |
Output is correct |
31 |
Correct |
32 ms |
29536 KB |
Output is correct |
32 |
Correct |
29 ms |
29568 KB |
Output is correct |
33 |
Correct |
32 ms |
29560 KB |
Output is correct |
34 |
Correct |
27 ms |
29440 KB |
Output is correct |
35 |
Correct |
24 ms |
29568 KB |
Output is correct |
36 |
Correct |
26 ms |
29568 KB |
Output is correct |
37 |
Correct |
29 ms |
29568 KB |
Output is correct |
38 |
Correct |
27 ms |
29668 KB |
Output is correct |
39 |
Correct |
29 ms |
29544 KB |
Output is correct |
40 |
Correct |
42 ms |
29560 KB |
Output is correct |
41 |
Correct |
28 ms |
29440 KB |
Output is correct |
42 |
Correct |
32 ms |
29568 KB |
Output is correct |
43 |
Correct |
34 ms |
29564 KB |
Output is correct |
44 |
Correct |
31 ms |
29568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
28800 KB |
Output is correct |
2 |
Correct |
23 ms |
28744 KB |
Output is correct |
3 |
Correct |
24 ms |
28800 KB |
Output is correct |
4 |
Correct |
23 ms |
28756 KB |
Output is correct |
5 |
Correct |
28 ms |
28800 KB |
Output is correct |
6 |
Correct |
30 ms |
28800 KB |
Output is correct |
7 |
Correct |
23 ms |
28800 KB |
Output is correct |
8 |
Correct |
23 ms |
28800 KB |
Output is correct |
9 |
Correct |
22 ms |
28800 KB |
Output is correct |
10 |
Correct |
22 ms |
28800 KB |
Output is correct |
11 |
Correct |
21 ms |
28800 KB |
Output is correct |
12 |
Correct |
32 ms |
28800 KB |
Output is correct |
13 |
Correct |
21 ms |
28800 KB |
Output is correct |
14 |
Correct |
21 ms |
28800 KB |
Output is correct |
15 |
Correct |
21 ms |
28800 KB |
Output is correct |
16 |
Correct |
27 ms |
28792 KB |
Output is correct |
17 |
Correct |
24 ms |
28800 KB |
Output is correct |
18 |
Correct |
35 ms |
28800 KB |
Output is correct |
19 |
Correct |
28 ms |
28796 KB |
Output is correct |
20 |
Correct |
27 ms |
28792 KB |
Output is correct |
21 |
Correct |
26 ms |
28776 KB |
Output is correct |
22 |
Correct |
22 ms |
28800 KB |
Output is correct |
23 |
Correct |
28 ms |
29568 KB |
Output is correct |
24 |
Correct |
28 ms |
29440 KB |
Output is correct |
25 |
Correct |
32 ms |
29568 KB |
Output is correct |
26 |
Correct |
30 ms |
29560 KB |
Output is correct |
27 |
Correct |
26 ms |
29568 KB |
Output is correct |
28 |
Correct |
30 ms |
29440 KB |
Output is correct |
29 |
Correct |
25 ms |
29568 KB |
Output is correct |
30 |
Correct |
33 ms |
29596 KB |
Output is correct |
31 |
Correct |
32 ms |
29536 KB |
Output is correct |
32 |
Correct |
29 ms |
29568 KB |
Output is correct |
33 |
Correct |
32 ms |
29560 KB |
Output is correct |
34 |
Correct |
27 ms |
29440 KB |
Output is correct |
35 |
Correct |
24 ms |
29568 KB |
Output is correct |
36 |
Correct |
26 ms |
29568 KB |
Output is correct |
37 |
Correct |
29 ms |
29568 KB |
Output is correct |
38 |
Correct |
27 ms |
29668 KB |
Output is correct |
39 |
Correct |
29 ms |
29544 KB |
Output is correct |
40 |
Correct |
42 ms |
29560 KB |
Output is correct |
41 |
Correct |
28 ms |
29440 KB |
Output is correct |
42 |
Correct |
32 ms |
29568 KB |
Output is correct |
43 |
Correct |
34 ms |
29564 KB |
Output is correct |
44 |
Correct |
31 ms |
29568 KB |
Output is correct |
45 |
Correct |
937 ms |
115900 KB |
Output is correct |
46 |
Correct |
976 ms |
114936 KB |
Output is correct |
47 |
Correct |
914 ms |
114028 KB |
Output is correct |
48 |
Correct |
941 ms |
115820 KB |
Output is correct |
49 |
Correct |
897 ms |
113224 KB |
Output is correct |
50 |
Correct |
902 ms |
113276 KB |
Output is correct |
51 |
Correct |
922 ms |
113408 KB |
Output is correct |
52 |
Correct |
936 ms |
115332 KB |
Output is correct |
53 |
Correct |
934 ms |
115056 KB |
Output is correct |
54 |
Correct |
1039 ms |
118724 KB |
Output is correct |
55 |
Correct |
1018 ms |
115872 KB |
Output is correct |
56 |
Correct |
987 ms |
114248 KB |
Output is correct |
57 |
Correct |
1042 ms |
113316 KB |
Output is correct |
58 |
Correct |
1020 ms |
116364 KB |
Output is correct |
59 |
Correct |
1090 ms |
116100 KB |
Output is correct |
60 |
Correct |
539 ms |
126376 KB |
Output is correct |
61 |
Correct |
909 ms |
114652 KB |
Output is correct |
62 |
Correct |
1012 ms |
116412 KB |
Output is correct |
63 |
Correct |
914 ms |
113360 KB |
Output is correct |
64 |
Correct |
943 ms |
111604 KB |
Output is correct |
65 |
Correct |
998 ms |
117312 KB |
Output is correct |
66 |
Correct |
1158 ms |
112484 KB |
Output is correct |