#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
int A[200000];
vpii stars[200000];
int l[200000],r[200000];
vi S;
bool comp(int a,int b) {
return r[a]-l[a] < r[b]-l[b];
}
int order[200000];
LLI dp[200000];
int low[200000];
int tree1[1 << 19];
int build(int s,int e,int i) {
if (s == e) {
tree1[i] = s;
return 0;
}
int mid = (s+e) / 2;
build(s,mid,2*i+1),build(mid+1,e,2*i+2);
tree1[i] = (low[tree1[2*i+1]] < low[tree1[2*i+2]]) ? tree1[2*i+1]:tree1[2*i+2];
return 0;
}
int update(int s,int e,int ai,int i) {
if ((s > ai) || (e < ai)) return 0;
else if (s == e) return 0;
int mid = (s+e) / 2;
update(s,mid,ai,2*i+1),update(mid+1,e,ai,2*i+2);
tree1[i] = (low[tree1[2*i+1]] < low[tree1[2*i+2]]) ? tree1[2*i+1]:tree1[2*i+2];
return 0;
}
int query(int s,int e,int qs,int qe,int i) {
if ((s > qe) || (e < qs)) return qs;
else if ((s >= qs) && (e <= qe)) return tree1[i];
int mid = (s+e) / 2;
int l = query(s,mid,qs,qe,2*i+1),r = query(mid+1,e,qs,qe,2*i+2);
return (low[l] < low[r]) ? l:r;
}
LLI tree2[1 << 19],lazy[1 << 19];
int prop(int s,int e,int i) {
tree2[i] += lazy[i];
if (s != e) lazy[2*i+1] += lazy[i],lazy[2*i+2] += lazy[i];
lazy[i] = 0;
return 0;
}
int update2(int s,int e,int as,int ae,int i,LLI num) {
prop(s,e,i);
if ((s > ae) || (e < as)) return 0;
else if ((s >= as) && (e <= ae)) {
lazy[i] += num;
prop(s,e,i);
return 0;
}
int mid = (s+e) / 2;
update2(s,mid,as,ae,2*i+1,num),update2(mid+1,e,as,ae,2*i+2,num);
tree2[i] = max(tree2[2*i+1],tree2[2*i+2]);
return 0;
}
LLI query2(int s,int e,int qs,int qe,int i) {
prop(s,e,i);
if ((s > qe) || (e < qs)) return 0;
else if ((s >= qs) && (e <= qe)) return tree2[i];
int mid = (s+e) / 2;
return max(query2(s,mid,qs,qe,2*i+1),query2(mid+1,e,qs,qe,2*i+2));
}
int main() {
int i;
int N,M,X,Y,C;
LLI sum = 0;
scanf("%d",&N);
for (i = 0; i < N; i++) scanf("%d",&A[i]);
scanf("%d",&M);
for (i = 0; i < M; i++) scanf("%d %d %d",&X,&Y,&C),stars[X-1].pb(mp(Y,C)),sum += C;
for (i = 0; i < N; i++) {
while (!S.empty() && (A[S.back()] <= A[i])) S.pop_back();
if (!S.empty()) l[i] = S.back();
else l[i] = -1;
S.pb(i);
}
S.clear();
for (i = N-1; i >= 0; i--) {
while (!S.empty() && (A[S.back()] < A[i])) S.pop_back();
if (!S.empty()) r[i] = S.back();
else r[i] = N;
S.pb(i);
}
for (i = 0; i < N; i++) {
order[i] = i,sort(stars[i].begin(),stars[i].end(),greater<pii>());
low[i] = stars[i].empty() ? 1e9:stars[i].back().first;
}
sort(order,order+N,comp);
build(0,N-1,0);
LLI ans = 0;
for (i = 0; i < N; i++) {
int u = order[i],v;
LLI maxl = query2(0,N-1,l[u]+1,u-1,0),maxr = query2(0,N-1,u+1,r[u]-1,0);
if (u-1 >= l[u]+1) {
v = query(0,N-1,l[u]+1,u-1,0);
while (low[v] <= A[u]) {
maxl = max(maxl,query2(0,N-1,v,v,0)+stars[v].back().second);
stars[v].pop_back(),low[v] = stars[v].empty() ? 1e9:stars[v].back().first;
update(0,N-1,v,0),v = query(0,N-1,l[u]+1,u-1,0);
}
}
if (r[u]-1 >= u+1) {
v = query(0,N-1,u+1,r[u]-1,0);
while (low[v] <= A[u]) {
maxr = max(maxr,query2(0,N-1,v,v,0)+stars[v].back().second);
stars[v].pop_back(),low[v] = stars[v].empty() ? 1e9:stars[v].back().first;
update(0,N-1,v,0),v = query(0,N-1,u+1,r[u]-1,0);
}
}
update2(0,N-1,l[u]+1,u,0,maxr),update2(0,N-1,u,r[u]-1,0,maxl);
ans = max(ans,query2(0,N-1,u,u,0));
}
for (i = 0; i < N; i++) {
LLI x = query2(0,N-1,i,i,0);
while (!stars[i].empty()) ans = max(ans,stars[i].back().second+x),stars[i].pop_back();
}
printf("%lld\n",sum-ans);
return 0;
}
Compilation message
constellation3.cpp: In function 'int main()':
constellation3.cpp:130:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&N);
~~~~~^~~~~~~~~
constellation3.cpp:131:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (i = 0; i < N; i++) scanf("%d",&A[i]);
~~~~~^~~~~~~~~~~~
constellation3.cpp:132:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&M);
~~~~~^~~~~~~~~
constellation3.cpp:133:78: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (i = 0; i < M; i++) scanf("%d %d %d",&X,&Y,&C),stars[X-1].pb(mp(Y,C)),sum += C;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
9 ms |
5120 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
9 ms |
5120 KB |
Output is correct |
8 |
Correct |
8 ms |
5120 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
9 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
8 ms |
5120 KB |
Output is correct |
13 |
Correct |
8 ms |
4992 KB |
Output is correct |
14 |
Correct |
9 ms |
5120 KB |
Output is correct |
15 |
Correct |
9 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
5120 KB |
Output is correct |
17 |
Correct |
8 ms |
5120 KB |
Output is correct |
18 |
Correct |
8 ms |
5120 KB |
Output is correct |
19 |
Correct |
8 ms |
5120 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
8 ms |
5120 KB |
Output is correct |
22 |
Correct |
8 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
9 ms |
5120 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
9 ms |
5120 KB |
Output is correct |
8 |
Correct |
8 ms |
5120 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
9 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
8 ms |
5120 KB |
Output is correct |
13 |
Correct |
8 ms |
4992 KB |
Output is correct |
14 |
Correct |
9 ms |
5120 KB |
Output is correct |
15 |
Correct |
9 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
5120 KB |
Output is correct |
17 |
Correct |
8 ms |
5120 KB |
Output is correct |
18 |
Correct |
8 ms |
5120 KB |
Output is correct |
19 |
Correct |
8 ms |
5120 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
8 ms |
5120 KB |
Output is correct |
22 |
Correct |
8 ms |
5120 KB |
Output is correct |
23 |
Correct |
12 ms |
5248 KB |
Output is correct |
24 |
Correct |
14 ms |
5248 KB |
Output is correct |
25 |
Correct |
13 ms |
5248 KB |
Output is correct |
26 |
Correct |
12 ms |
5248 KB |
Output is correct |
27 |
Correct |
13 ms |
5280 KB |
Output is correct |
28 |
Correct |
13 ms |
5248 KB |
Output is correct |
29 |
Correct |
12 ms |
5248 KB |
Output is correct |
30 |
Correct |
12 ms |
5248 KB |
Output is correct |
31 |
Correct |
12 ms |
5248 KB |
Output is correct |
32 |
Correct |
12 ms |
5248 KB |
Output is correct |
33 |
Correct |
12 ms |
5248 KB |
Output is correct |
34 |
Correct |
12 ms |
5248 KB |
Output is correct |
35 |
Correct |
13 ms |
5248 KB |
Output is correct |
36 |
Correct |
12 ms |
5248 KB |
Output is correct |
37 |
Correct |
13 ms |
5248 KB |
Output is correct |
38 |
Correct |
12 ms |
5248 KB |
Output is correct |
39 |
Correct |
12 ms |
5248 KB |
Output is correct |
40 |
Correct |
12 ms |
5248 KB |
Output is correct |
41 |
Correct |
12 ms |
5248 KB |
Output is correct |
42 |
Correct |
13 ms |
5248 KB |
Output is correct |
43 |
Correct |
12 ms |
5248 KB |
Output is correct |
44 |
Correct |
12 ms |
5248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
9 ms |
5120 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
9 ms |
5120 KB |
Output is correct |
8 |
Correct |
8 ms |
5120 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
9 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
8 ms |
5120 KB |
Output is correct |
13 |
Correct |
8 ms |
4992 KB |
Output is correct |
14 |
Correct |
9 ms |
5120 KB |
Output is correct |
15 |
Correct |
9 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
5120 KB |
Output is correct |
17 |
Correct |
8 ms |
5120 KB |
Output is correct |
18 |
Correct |
8 ms |
5120 KB |
Output is correct |
19 |
Correct |
8 ms |
5120 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
8 ms |
5120 KB |
Output is correct |
22 |
Correct |
8 ms |
5120 KB |
Output is correct |
23 |
Correct |
12 ms |
5248 KB |
Output is correct |
24 |
Correct |
14 ms |
5248 KB |
Output is correct |
25 |
Correct |
13 ms |
5248 KB |
Output is correct |
26 |
Correct |
12 ms |
5248 KB |
Output is correct |
27 |
Correct |
13 ms |
5280 KB |
Output is correct |
28 |
Correct |
13 ms |
5248 KB |
Output is correct |
29 |
Correct |
12 ms |
5248 KB |
Output is correct |
30 |
Correct |
12 ms |
5248 KB |
Output is correct |
31 |
Correct |
12 ms |
5248 KB |
Output is correct |
32 |
Correct |
12 ms |
5248 KB |
Output is correct |
33 |
Correct |
12 ms |
5248 KB |
Output is correct |
34 |
Correct |
12 ms |
5248 KB |
Output is correct |
35 |
Correct |
13 ms |
5248 KB |
Output is correct |
36 |
Correct |
12 ms |
5248 KB |
Output is correct |
37 |
Correct |
13 ms |
5248 KB |
Output is correct |
38 |
Correct |
12 ms |
5248 KB |
Output is correct |
39 |
Correct |
12 ms |
5248 KB |
Output is correct |
40 |
Correct |
12 ms |
5248 KB |
Output is correct |
41 |
Correct |
12 ms |
5248 KB |
Output is correct |
42 |
Correct |
13 ms |
5248 KB |
Output is correct |
43 |
Correct |
12 ms |
5248 KB |
Output is correct |
44 |
Correct |
12 ms |
5248 KB |
Output is correct |
45 |
Correct |
876 ms |
23788 KB |
Output is correct |
46 |
Correct |
828 ms |
23800 KB |
Output is correct |
47 |
Correct |
824 ms |
23928 KB |
Output is correct |
48 |
Correct |
922 ms |
23800 KB |
Output is correct |
49 |
Correct |
822 ms |
23800 KB |
Output is correct |
50 |
Correct |
804 ms |
23800 KB |
Output is correct |
51 |
Correct |
793 ms |
23800 KB |
Output is correct |
52 |
Correct |
832 ms |
23928 KB |
Output is correct |
53 |
Correct |
832 ms |
23928 KB |
Output is correct |
54 |
Correct |
931 ms |
24308 KB |
Output is correct |
55 |
Correct |
873 ms |
29428 KB |
Output is correct |
56 |
Correct |
842 ms |
28920 KB |
Output is correct |
57 |
Correct |
886 ms |
29052 KB |
Output is correct |
58 |
Correct |
906 ms |
29172 KB |
Output is correct |
59 |
Correct |
850 ms |
29172 KB |
Output is correct |
60 |
Correct |
630 ms |
27760 KB |
Output is correct |
61 |
Correct |
858 ms |
28576 KB |
Output is correct |
62 |
Correct |
906 ms |
29164 KB |
Output is correct |
63 |
Correct |
809 ms |
28664 KB |
Output is correct |
64 |
Correct |
815 ms |
28280 KB |
Output is correct |
65 |
Correct |
911 ms |
29044 KB |
Output is correct |
66 |
Correct |
813 ms |
28588 KB |
Output is correct |