// Zende bad Shoma nasime faghat !
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 2e5 + 10;
const int Log = 20;
const int Inf = 1'000'000'001;
struct Fenwick {
ll fen[N];
Fenwick (){
memset(fen, 0, sizeof fen);
}
void Add(int idx, ll d){
for(; idx < N; idx += (idx & (-idx)))
fen[idx] += d;
}
ll Get(int idx){
ll res = 0;
for(; idx; idx -= (idx & (-idx)))
res += fen[idx];
return res;
}
void AddLR(int L, int R, ll d){
Add(L, d);
Add(R, -d);
}
};
Fenwick fr, fl;
int n, A[N];
pii sp[Log][N];
int BigR(int idx, pii V){
int res = idx;
for(int l = Log - 1; l >= 0; l--)
if(sp[l][res] <= V)
res += 1 << l;
return res;
}
int BigL(int idx, pii V){
int res = idx;
for(int l = Log - 1; l >= 0; l--)
if(sp[l][max(0, res - (1 << l) + 1)] <= V)
res -= 1 << l;
return res;
}
pii Max(int L, int R){
L ++;
pii res = {-1, -1};
for(int l = Log - 1; l >= 0; l--){
if(L + (1 << l) <= R){
res = max(res, sp[l][L]);
L += 1 << l;
}
}
return res;
}
int m;
int X[N], Y[N], C[N];
map<pii, vector<int> > mp;
map<pii, ll> dp;
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) cin >> A[i];
A[0] = A[n + 1] = Inf;
for(int i = 0; i <= n + 1; i++){
sp[0][i] = pii(A[i], i);
}
for(int l = 1; l < Log; l++)
for(int i = 0; i <= n + 1; i++)
sp[l][i] = max(sp[l - 1][i], sp[l - 1][min(n + 1, i + (1 << (l-1)) )] );
vector<pii> seg;
for(int i = 1; i <= n; i++){
//cerr << BigL(i, pii(A[i], i)) << ' ' << BigR(i, pii(A[i], i)) << '\n';
seg.pb({i, BigR(i, pii(A[i], i) )});
seg.pb({BigL(i, pii(A[i], i) ), i});
}
seg.pb({0, n + 1});
sort(all(seg)); seg.resize(unique(all(seg)) - seg.begin());
sort(all(seg), [&](pii A, pii B){
if(A.S - A.F == B.S - B.F) return A.F < B.F;
return A.S - A.F < B.S - B.F;
});
ll Sum = 0;
cin >> m;
for(int i = 1; i <= m; i++){
cin >> X[i] >> Y[i] >> C[i];
mp[pii( BigL(X[i], pii(Y[i], -1)), BigR(X[i], pii(Y[i], -1)) )].pb(i);
Sum += C[i];
}
//debug("Start");
ll res;
for(auto [L, R] : seg){
if(R - L == 1) continue;
int mx = Max(L, R).S;
res = dp[{L, mx}] + dp[{mx, R}];
vector<int> &V = mp[{L, R}];
for(auto st : V){
/*debug(st);
if(st == 4){
cerr << fr.Get(X[st]) << " " << fr.Get(R) << '\n';
cerr << "! " << st << " : " << (fr.Get(X[st]) - fr.Get(R)) << ", " << (fl.Get(X[st]) - fl.Get(L)) << '\n';
}*/
res = max(res, C[st] + (fr.Get(X[st]) - fr.Get(R)) + (fl.Get(X[st]) - fl.Get(L)) ) ;
}
dp[{L, R}] = res;
//cerr << L << "->" << R << " : " << res << '\n';
if(pii(L, R) == pii(0, n + 1)) continue;
if(pii(A[L], L) < pii(A[R], R) ){
//cerr << "# " << L << ' ' << R << ' ' << res << '\n';
fr.AddLR(BigL(L, pii(A[L], L)) + 1, L + 1, res);
} else {
fl.AddLR(R, BigR(R, pii(A[R], R)), res);
}
}
cout << Sum - dp[{0, n + 1}] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3712 KB |
Output is correct |
2 |
Correct |
3 ms |
3712 KB |
Output is correct |
3 |
Correct |
3 ms |
3712 KB |
Output is correct |
4 |
Correct |
3 ms |
3840 KB |
Output is correct |
5 |
Correct |
4 ms |
3840 KB |
Output is correct |
6 |
Correct |
3 ms |
3712 KB |
Output is correct |
7 |
Correct |
3 ms |
3712 KB |
Output is correct |
8 |
Correct |
3 ms |
3712 KB |
Output is correct |
9 |
Correct |
3 ms |
3712 KB |
Output is correct |
10 |
Correct |
3 ms |
3712 KB |
Output is correct |
11 |
Correct |
3 ms |
3712 KB |
Output is correct |
12 |
Correct |
3 ms |
3712 KB |
Output is correct |
13 |
Correct |
3 ms |
3712 KB |
Output is correct |
14 |
Correct |
3 ms |
3712 KB |
Output is correct |
15 |
Correct |
3 ms |
3712 KB |
Output is correct |
16 |
Correct |
3 ms |
3840 KB |
Output is correct |
17 |
Correct |
3 ms |
3712 KB |
Output is correct |
18 |
Correct |
3 ms |
3712 KB |
Output is correct |
19 |
Correct |
3 ms |
3712 KB |
Output is correct |
20 |
Correct |
3 ms |
3712 KB |
Output is correct |
21 |
Correct |
3 ms |
3712 KB |
Output is correct |
22 |
Correct |
3 ms |
3712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3712 KB |
Output is correct |
2 |
Correct |
3 ms |
3712 KB |
Output is correct |
3 |
Correct |
3 ms |
3712 KB |
Output is correct |
4 |
Correct |
3 ms |
3840 KB |
Output is correct |
5 |
Correct |
4 ms |
3840 KB |
Output is correct |
6 |
Correct |
3 ms |
3712 KB |
Output is correct |
7 |
Correct |
3 ms |
3712 KB |
Output is correct |
8 |
Correct |
3 ms |
3712 KB |
Output is correct |
9 |
Correct |
3 ms |
3712 KB |
Output is correct |
10 |
Correct |
3 ms |
3712 KB |
Output is correct |
11 |
Correct |
3 ms |
3712 KB |
Output is correct |
12 |
Correct |
3 ms |
3712 KB |
Output is correct |
13 |
Correct |
3 ms |
3712 KB |
Output is correct |
14 |
Correct |
3 ms |
3712 KB |
Output is correct |
15 |
Correct |
3 ms |
3712 KB |
Output is correct |
16 |
Correct |
3 ms |
3840 KB |
Output is correct |
17 |
Correct |
3 ms |
3712 KB |
Output is correct |
18 |
Correct |
3 ms |
3712 KB |
Output is correct |
19 |
Correct |
3 ms |
3712 KB |
Output is correct |
20 |
Correct |
3 ms |
3712 KB |
Output is correct |
21 |
Correct |
3 ms |
3712 KB |
Output is correct |
22 |
Correct |
3 ms |
3712 KB |
Output is correct |
23 |
Correct |
7 ms |
4480 KB |
Output is correct |
24 |
Correct |
7 ms |
4480 KB |
Output is correct |
25 |
Correct |
7 ms |
4480 KB |
Output is correct |
26 |
Correct |
7 ms |
4480 KB |
Output is correct |
27 |
Correct |
7 ms |
4480 KB |
Output is correct |
28 |
Correct |
7 ms |
4480 KB |
Output is correct |
29 |
Correct |
7 ms |
4480 KB |
Output is correct |
30 |
Correct |
7 ms |
4480 KB |
Output is correct |
31 |
Correct |
7 ms |
4480 KB |
Output is correct |
32 |
Correct |
7 ms |
4480 KB |
Output is correct |
33 |
Correct |
9 ms |
4480 KB |
Output is correct |
34 |
Correct |
7 ms |
4480 KB |
Output is correct |
35 |
Correct |
7 ms |
4480 KB |
Output is correct |
36 |
Correct |
7 ms |
4608 KB |
Output is correct |
37 |
Correct |
7 ms |
4480 KB |
Output is correct |
38 |
Correct |
10 ms |
4480 KB |
Output is correct |
39 |
Correct |
7 ms |
4480 KB |
Output is correct |
40 |
Correct |
9 ms |
4456 KB |
Output is correct |
41 |
Correct |
7 ms |
4480 KB |
Output is correct |
42 |
Correct |
7 ms |
4480 KB |
Output is correct |
43 |
Correct |
8 ms |
4480 KB |
Output is correct |
44 |
Correct |
9 ms |
4480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3712 KB |
Output is correct |
2 |
Correct |
3 ms |
3712 KB |
Output is correct |
3 |
Correct |
3 ms |
3712 KB |
Output is correct |
4 |
Correct |
3 ms |
3840 KB |
Output is correct |
5 |
Correct |
4 ms |
3840 KB |
Output is correct |
6 |
Correct |
3 ms |
3712 KB |
Output is correct |
7 |
Correct |
3 ms |
3712 KB |
Output is correct |
8 |
Correct |
3 ms |
3712 KB |
Output is correct |
9 |
Correct |
3 ms |
3712 KB |
Output is correct |
10 |
Correct |
3 ms |
3712 KB |
Output is correct |
11 |
Correct |
3 ms |
3712 KB |
Output is correct |
12 |
Correct |
3 ms |
3712 KB |
Output is correct |
13 |
Correct |
3 ms |
3712 KB |
Output is correct |
14 |
Correct |
3 ms |
3712 KB |
Output is correct |
15 |
Correct |
3 ms |
3712 KB |
Output is correct |
16 |
Correct |
3 ms |
3840 KB |
Output is correct |
17 |
Correct |
3 ms |
3712 KB |
Output is correct |
18 |
Correct |
3 ms |
3712 KB |
Output is correct |
19 |
Correct |
3 ms |
3712 KB |
Output is correct |
20 |
Correct |
3 ms |
3712 KB |
Output is correct |
21 |
Correct |
3 ms |
3712 KB |
Output is correct |
22 |
Correct |
3 ms |
3712 KB |
Output is correct |
23 |
Correct |
7 ms |
4480 KB |
Output is correct |
24 |
Correct |
7 ms |
4480 KB |
Output is correct |
25 |
Correct |
7 ms |
4480 KB |
Output is correct |
26 |
Correct |
7 ms |
4480 KB |
Output is correct |
27 |
Correct |
7 ms |
4480 KB |
Output is correct |
28 |
Correct |
7 ms |
4480 KB |
Output is correct |
29 |
Correct |
7 ms |
4480 KB |
Output is correct |
30 |
Correct |
7 ms |
4480 KB |
Output is correct |
31 |
Correct |
7 ms |
4480 KB |
Output is correct |
32 |
Correct |
7 ms |
4480 KB |
Output is correct |
33 |
Correct |
9 ms |
4480 KB |
Output is correct |
34 |
Correct |
7 ms |
4480 KB |
Output is correct |
35 |
Correct |
7 ms |
4480 KB |
Output is correct |
36 |
Correct |
7 ms |
4608 KB |
Output is correct |
37 |
Correct |
7 ms |
4480 KB |
Output is correct |
38 |
Correct |
10 ms |
4480 KB |
Output is correct |
39 |
Correct |
7 ms |
4480 KB |
Output is correct |
40 |
Correct |
9 ms |
4456 KB |
Output is correct |
41 |
Correct |
7 ms |
4480 KB |
Output is correct |
42 |
Correct |
7 ms |
4480 KB |
Output is correct |
43 |
Correct |
8 ms |
4480 KB |
Output is correct |
44 |
Correct |
9 ms |
4480 KB |
Output is correct |
45 |
Correct |
1120 ms |
89824 KB |
Output is correct |
46 |
Correct |
1062 ms |
89056 KB |
Output is correct |
47 |
Correct |
1052 ms |
87820 KB |
Output is correct |
48 |
Correct |
1025 ms |
89948 KB |
Output is correct |
49 |
Correct |
1043 ms |
87208 KB |
Output is correct |
50 |
Correct |
993 ms |
87268 KB |
Output is correct |
51 |
Correct |
1020 ms |
87396 KB |
Output is correct |
52 |
Correct |
1059 ms |
89284 KB |
Output is correct |
53 |
Correct |
1051 ms |
89180 KB |
Output is correct |
54 |
Correct |
1135 ms |
89696 KB |
Output is correct |
55 |
Correct |
1091 ms |
87648 KB |
Output is correct |
56 |
Correct |
1146 ms |
86520 KB |
Output is correct |
57 |
Correct |
1086 ms |
85712 KB |
Output is correct |
58 |
Correct |
940 ms |
85344 KB |
Output is correct |
59 |
Correct |
990 ms |
84960 KB |
Output is correct |
60 |
Correct |
582 ms |
89544 KB |
Output is correct |
61 |
Correct |
1053 ms |
88548 KB |
Output is correct |
62 |
Correct |
1092 ms |
86880 KB |
Output is correct |
63 |
Correct |
1089 ms |
87392 KB |
Output is correct |
64 |
Correct |
1033 ms |
85728 KB |
Output is correct |
65 |
Correct |
1137 ms |
87656 KB |
Output is correct |
66 |
Correct |
1083 ms |
86580 KB |
Output is correct |