#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
struct pi{
int x,y; ll w;
};
struct pi2{
int y; ll up,down;
};
struct seg{
ll tree[1<<18];
int sz=1<<17;
void up(ll v,int tg){
tg|=sz;
for(tree[tg]=max(tree[tg],v);tg>>=1;)
tree[tg]=max(tree[tg<<1],tree[tg<<1|1]);
}
void init(ll v,int tg){
for(tree[tg|=sz]=v;tg>>=1;)
tree[tg]=max(tree[tg<<1],tree[tg<<1|1]);
}
ll qu(int l,int r){
ll re=0;
for(l|=sz,r|=sz;l<=r;l=(l+1)>>1,r=(r-1)>>1){
if(l&1) re=max(re,tree[l++]);
if(~r&1) re=max(re,tree[r--]);
}
return re;
}
};
seg t1,t2;
vector<pii> v;
vector<pi> megi;
ll dp[100010],inf=1e18;;
map<int,ll> mp[1000010];
bool comp(pi &a,pi &b){
if(a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
ll max_weights(int N, int M, std::vector<int> X, std::vector<int> Y,
std::vector<int> W) {
for(int i=0;i<N;i++) v.push_back({i,0});
for(int i=0;i<M;i++){
if(X[i]>0) v.push_back({X[i]-1,Y[i]+1});
if(X[i]<N-1) v.push_back({X[i]+1,Y[i]+1});
}
for(int i=0;i<M;i++) megi.push_back({X[i],Y[i],W[i]});
sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end());
sort(megi.begin(),megi.end(),comp);
int l1=0,l2=0,m=0;
for(int i=0;i<(1<<18);i++) t1.tree[i]=t2.tree[i]=-inf;
for(int i=0;i<N;i++){
ll cnt=0;
mp[i][-1]=0;
while(m<M&&megi[m].x==i){
cnt+=megi[m].w;
mp[i][megi[m].y]=cnt;
//printf("%d %d %lld\n",i,megi[m].y,cnt);
m++;
}
}
while(l2<(int)v.size()&&v[l2].first==0){
auto it = mp[1].upper_bound(v[l2].second-1); it--;
t2.up(it->second,v[l2].second);
l2++;
}
for(int i=1;i<N;i++){
vector<pi2> tmp;
while(l2<(int)v.size()&&v[l2].first==i){
auto it = mp[i-1].upper_bound(v[l2].second-1); it--;
ll up=t1.qu(0, v[l2].second)+it->second;
if(i==1) up=max(up,it->second);
else up=max(up,dp[i-2]+it->second);
it = mp[i].upper_bound(v[l2].second-1); it--;
ll down=t2.qu(v[l2].second, N)-it->second;
tmp.push_back({v[l2].second,up,down});
dp[i]=max(dp[i],max(up,down));
//printf("%d %d %lld %lld %lld\n",i,v[l2].second,up,down,it->second);
l2++;
}
if(i==N-1) break;
while(l1<(int)v.size()&&v[l1].first==i-1){
t1.init(-inf, v[l1].second);
t2.init(-inf, v[l1].second);
l1++;
}
for(auto x:tmp){
auto it = mp[i].upper_bound(x.y-1); it--;
t1.up(x.up-it->second, x.y);
it = mp[i+1].upper_bound(x.y-1); it--;
t2.up(max(x.up,x.down)+it->second, x.y);
}
}
return dp[N-1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
146 ms |
71132 KB |
Output is correct |
2 |
Correct |
182 ms |
73996 KB |
Output is correct |
3 |
Correct |
77 ms |
59156 KB |
Output is correct |
4 |
Correct |
74 ms |
59124 KB |
Output is correct |
5 |
Correct |
543 ms |
99572 KB |
Output is correct |
6 |
Correct |
515 ms |
94404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
51284 KB |
Output is correct |
2 |
Correct |
279 ms |
81096 KB |
Output is correct |
3 |
Correct |
330 ms |
86568 KB |
Output is correct |
4 |
Correct |
150 ms |
71132 KB |
Output is correct |
5 |
Correct |
176 ms |
73920 KB |
Output is correct |
6 |
Correct |
23 ms |
51276 KB |
Output is correct |
7 |
Correct |
23 ms |
51276 KB |
Output is correct |
8 |
Correct |
24 ms |
51284 KB |
Output is correct |
9 |
Correct |
24 ms |
51332 KB |
Output is correct |
10 |
Correct |
75 ms |
59076 KB |
Output is correct |
11 |
Correct |
72 ms |
59088 KB |
Output is correct |
12 |
Correct |
168 ms |
71784 KB |
Output is correct |
13 |
Correct |
204 ms |
75732 KB |
Output is correct |
14 |
Correct |
171 ms |
69424 KB |
Output is correct |
15 |
Correct |
183 ms |
71244 KB |
Output is correct |
16 |
Correct |
163 ms |
69484 KB |
Output is correct |
17 |
Correct |
178 ms |
71196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
59168 KB |
Output is correct |
2 |
Correct |
68 ms |
59140 KB |
Output is correct |
3 |
Correct |
123 ms |
64608 KB |
Output is correct |
4 |
Correct |
114 ms |
63340 KB |
Output is correct |
5 |
Correct |
173 ms |
70912 KB |
Output is correct |
6 |
Correct |
171 ms |
70844 KB |
Output is correct |
7 |
Correct |
177 ms |
70836 KB |
Output is correct |
8 |
Correct |
175 ms |
70940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
51276 KB |
Output is correct |
2 |
Correct |
22 ms |
51304 KB |
Output is correct |
3 |
Correct |
27 ms |
51308 KB |
Output is correct |
4 |
Correct |
23 ms |
51336 KB |
Output is correct |
5 |
Correct |
24 ms |
51284 KB |
Output is correct |
6 |
Correct |
24 ms |
51340 KB |
Output is correct |
7 |
Correct |
24 ms |
51284 KB |
Output is correct |
8 |
Correct |
25 ms |
51232 KB |
Output is correct |
9 |
Correct |
25 ms |
51348 KB |
Output is correct |
10 |
Correct |
27 ms |
51684 KB |
Output is correct |
11 |
Correct |
25 ms |
51476 KB |
Output is correct |
12 |
Correct |
28 ms |
51504 KB |
Output is correct |
13 |
Correct |
25 ms |
51312 KB |
Output is correct |
14 |
Correct |
25 ms |
51512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
51276 KB |
Output is correct |
2 |
Correct |
22 ms |
51304 KB |
Output is correct |
3 |
Correct |
27 ms |
51308 KB |
Output is correct |
4 |
Correct |
23 ms |
51336 KB |
Output is correct |
5 |
Correct |
24 ms |
51284 KB |
Output is correct |
6 |
Correct |
24 ms |
51340 KB |
Output is correct |
7 |
Correct |
24 ms |
51284 KB |
Output is correct |
8 |
Correct |
25 ms |
51232 KB |
Output is correct |
9 |
Correct |
25 ms |
51348 KB |
Output is correct |
10 |
Correct |
27 ms |
51684 KB |
Output is correct |
11 |
Correct |
25 ms |
51476 KB |
Output is correct |
12 |
Correct |
28 ms |
51504 KB |
Output is correct |
13 |
Correct |
25 ms |
51312 KB |
Output is correct |
14 |
Correct |
25 ms |
51512 KB |
Output is correct |
15 |
Correct |
25 ms |
51388 KB |
Output is correct |
16 |
Correct |
26 ms |
51644 KB |
Output is correct |
17 |
Correct |
83 ms |
57432 KB |
Output is correct |
18 |
Correct |
78 ms |
57284 KB |
Output is correct |
19 |
Correct |
74 ms |
57288 KB |
Output is correct |
20 |
Correct |
75 ms |
57284 KB |
Output is correct |
21 |
Correct |
68 ms |
57300 KB |
Output is correct |
22 |
Correct |
118 ms |
63260 KB |
Output is correct |
23 |
Correct |
37 ms |
52380 KB |
Output is correct |
24 |
Correct |
64 ms |
55200 KB |
Output is correct |
25 |
Correct |
26 ms |
51460 KB |
Output is correct |
26 |
Correct |
35 ms |
52396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
51276 KB |
Output is correct |
2 |
Correct |
22 ms |
51304 KB |
Output is correct |
3 |
Correct |
27 ms |
51308 KB |
Output is correct |
4 |
Correct |
23 ms |
51336 KB |
Output is correct |
5 |
Correct |
24 ms |
51284 KB |
Output is correct |
6 |
Correct |
24 ms |
51340 KB |
Output is correct |
7 |
Correct |
24 ms |
51284 KB |
Output is correct |
8 |
Correct |
25 ms |
51232 KB |
Output is correct |
9 |
Correct |
25 ms |
51348 KB |
Output is correct |
10 |
Correct |
27 ms |
51684 KB |
Output is correct |
11 |
Correct |
25 ms |
51476 KB |
Output is correct |
12 |
Correct |
28 ms |
51504 KB |
Output is correct |
13 |
Correct |
25 ms |
51312 KB |
Output is correct |
14 |
Correct |
25 ms |
51512 KB |
Output is correct |
15 |
Correct |
25 ms |
51388 KB |
Output is correct |
16 |
Correct |
26 ms |
51644 KB |
Output is correct |
17 |
Correct |
83 ms |
57432 KB |
Output is correct |
18 |
Correct |
78 ms |
57284 KB |
Output is correct |
19 |
Correct |
74 ms |
57288 KB |
Output is correct |
20 |
Correct |
75 ms |
57284 KB |
Output is correct |
21 |
Correct |
68 ms |
57300 KB |
Output is correct |
22 |
Correct |
118 ms |
63260 KB |
Output is correct |
23 |
Correct |
37 ms |
52380 KB |
Output is correct |
24 |
Correct |
64 ms |
55200 KB |
Output is correct |
25 |
Correct |
26 ms |
51460 KB |
Output is correct |
26 |
Correct |
35 ms |
52396 KB |
Output is correct |
27 |
Correct |
30 ms |
51976 KB |
Output is correct |
28 |
Correct |
286 ms |
80416 KB |
Output is correct |
29 |
Correct |
438 ms |
92528 KB |
Output is correct |
30 |
Correct |
458 ms |
92204 KB |
Output is correct |
31 |
Correct |
458 ms |
92304 KB |
Output is correct |
32 |
Correct |
347 ms |
91532 KB |
Output is correct |
33 |
Correct |
475 ms |
92308 KB |
Output is correct |
34 |
Correct |
469 ms |
91732 KB |
Output is correct |
35 |
Correct |
178 ms |
66776 KB |
Output is correct |
36 |
Correct |
449 ms |
90396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
59168 KB |
Output is correct |
2 |
Correct |
68 ms |
59140 KB |
Output is correct |
3 |
Correct |
123 ms |
64608 KB |
Output is correct |
4 |
Correct |
114 ms |
63340 KB |
Output is correct |
5 |
Correct |
173 ms |
70912 KB |
Output is correct |
6 |
Correct |
171 ms |
70844 KB |
Output is correct |
7 |
Correct |
177 ms |
70836 KB |
Output is correct |
8 |
Correct |
175 ms |
70940 KB |
Output is correct |
9 |
Correct |
191 ms |
70848 KB |
Output is correct |
10 |
Correct |
151 ms |
67000 KB |
Output is correct |
11 |
Correct |
288 ms |
82612 KB |
Output is correct |
12 |
Correct |
23 ms |
51284 KB |
Output is correct |
13 |
Correct |
25 ms |
51288 KB |
Output is correct |
14 |
Correct |
25 ms |
51260 KB |
Output is correct |
15 |
Correct |
24 ms |
51320 KB |
Output is correct |
16 |
Correct |
24 ms |
51276 KB |
Output is correct |
17 |
Correct |
23 ms |
51284 KB |
Output is correct |
18 |
Correct |
78 ms |
59088 KB |
Output is correct |
19 |
Correct |
74 ms |
59168 KB |
Output is correct |
20 |
Correct |
69 ms |
59084 KB |
Output is correct |
21 |
Correct |
69 ms |
59096 KB |
Output is correct |
22 |
Correct |
217 ms |
70964 KB |
Output is correct |
23 |
Correct |
344 ms |
83248 KB |
Output is correct |
24 |
Correct |
314 ms |
83060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
146 ms |
71132 KB |
Output is correct |
2 |
Correct |
182 ms |
73996 KB |
Output is correct |
3 |
Correct |
77 ms |
59156 KB |
Output is correct |
4 |
Correct |
74 ms |
59124 KB |
Output is correct |
5 |
Correct |
543 ms |
99572 KB |
Output is correct |
6 |
Correct |
515 ms |
94404 KB |
Output is correct |
7 |
Correct |
23 ms |
51284 KB |
Output is correct |
8 |
Correct |
279 ms |
81096 KB |
Output is correct |
9 |
Correct |
330 ms |
86568 KB |
Output is correct |
10 |
Correct |
150 ms |
71132 KB |
Output is correct |
11 |
Correct |
176 ms |
73920 KB |
Output is correct |
12 |
Correct |
23 ms |
51276 KB |
Output is correct |
13 |
Correct |
23 ms |
51276 KB |
Output is correct |
14 |
Correct |
24 ms |
51284 KB |
Output is correct |
15 |
Correct |
24 ms |
51332 KB |
Output is correct |
16 |
Correct |
75 ms |
59076 KB |
Output is correct |
17 |
Correct |
72 ms |
59088 KB |
Output is correct |
18 |
Correct |
168 ms |
71784 KB |
Output is correct |
19 |
Correct |
204 ms |
75732 KB |
Output is correct |
20 |
Correct |
171 ms |
69424 KB |
Output is correct |
21 |
Correct |
183 ms |
71244 KB |
Output is correct |
22 |
Correct |
163 ms |
69484 KB |
Output is correct |
23 |
Correct |
178 ms |
71196 KB |
Output is correct |
24 |
Correct |
74 ms |
59168 KB |
Output is correct |
25 |
Correct |
68 ms |
59140 KB |
Output is correct |
26 |
Correct |
123 ms |
64608 KB |
Output is correct |
27 |
Correct |
114 ms |
63340 KB |
Output is correct |
28 |
Correct |
173 ms |
70912 KB |
Output is correct |
29 |
Correct |
171 ms |
70844 KB |
Output is correct |
30 |
Correct |
177 ms |
70836 KB |
Output is correct |
31 |
Correct |
175 ms |
70940 KB |
Output is correct |
32 |
Correct |
22 ms |
51276 KB |
Output is correct |
33 |
Correct |
22 ms |
51304 KB |
Output is correct |
34 |
Correct |
27 ms |
51308 KB |
Output is correct |
35 |
Correct |
23 ms |
51336 KB |
Output is correct |
36 |
Correct |
24 ms |
51284 KB |
Output is correct |
37 |
Correct |
24 ms |
51340 KB |
Output is correct |
38 |
Correct |
24 ms |
51284 KB |
Output is correct |
39 |
Correct |
25 ms |
51232 KB |
Output is correct |
40 |
Correct |
25 ms |
51348 KB |
Output is correct |
41 |
Correct |
27 ms |
51684 KB |
Output is correct |
42 |
Correct |
25 ms |
51476 KB |
Output is correct |
43 |
Correct |
28 ms |
51504 KB |
Output is correct |
44 |
Correct |
25 ms |
51312 KB |
Output is correct |
45 |
Correct |
25 ms |
51512 KB |
Output is correct |
46 |
Correct |
25 ms |
51388 KB |
Output is correct |
47 |
Correct |
26 ms |
51644 KB |
Output is correct |
48 |
Correct |
83 ms |
57432 KB |
Output is correct |
49 |
Correct |
78 ms |
57284 KB |
Output is correct |
50 |
Correct |
74 ms |
57288 KB |
Output is correct |
51 |
Correct |
75 ms |
57284 KB |
Output is correct |
52 |
Correct |
68 ms |
57300 KB |
Output is correct |
53 |
Correct |
118 ms |
63260 KB |
Output is correct |
54 |
Correct |
37 ms |
52380 KB |
Output is correct |
55 |
Correct |
64 ms |
55200 KB |
Output is correct |
56 |
Correct |
26 ms |
51460 KB |
Output is correct |
57 |
Correct |
35 ms |
52396 KB |
Output is correct |
58 |
Correct |
30 ms |
51976 KB |
Output is correct |
59 |
Correct |
286 ms |
80416 KB |
Output is correct |
60 |
Correct |
438 ms |
92528 KB |
Output is correct |
61 |
Correct |
458 ms |
92204 KB |
Output is correct |
62 |
Correct |
458 ms |
92304 KB |
Output is correct |
63 |
Correct |
347 ms |
91532 KB |
Output is correct |
64 |
Correct |
475 ms |
92308 KB |
Output is correct |
65 |
Correct |
469 ms |
91732 KB |
Output is correct |
66 |
Correct |
178 ms |
66776 KB |
Output is correct |
67 |
Correct |
449 ms |
90396 KB |
Output is correct |
68 |
Correct |
191 ms |
70848 KB |
Output is correct |
69 |
Correct |
151 ms |
67000 KB |
Output is correct |
70 |
Correct |
288 ms |
82612 KB |
Output is correct |
71 |
Correct |
23 ms |
51284 KB |
Output is correct |
72 |
Correct |
25 ms |
51288 KB |
Output is correct |
73 |
Correct |
25 ms |
51260 KB |
Output is correct |
74 |
Correct |
24 ms |
51320 KB |
Output is correct |
75 |
Correct |
24 ms |
51276 KB |
Output is correct |
76 |
Correct |
23 ms |
51284 KB |
Output is correct |
77 |
Correct |
78 ms |
59088 KB |
Output is correct |
78 |
Correct |
74 ms |
59168 KB |
Output is correct |
79 |
Correct |
69 ms |
59084 KB |
Output is correct |
80 |
Correct |
69 ms |
59096 KB |
Output is correct |
81 |
Correct |
217 ms |
70964 KB |
Output is correct |
82 |
Correct |
344 ms |
83248 KB |
Output is correct |
83 |
Correct |
314 ms |
83060 KB |
Output is correct |
84 |
Correct |
580 ms |
99716 KB |
Output is correct |
85 |
Correct |
593 ms |
99732 KB |
Output is correct |
86 |
Correct |
521 ms |
94384 KB |
Output is correct |
87 |
Correct |
541 ms |
95104 KB |
Output is correct |
88 |
Correct |
25 ms |
51276 KB |
Output is correct |
89 |
Correct |
534 ms |
95776 KB |
Output is correct |
90 |
Correct |
465 ms |
94620 KB |
Output is correct |
91 |
Correct |
417 ms |
94716 KB |
Output is correct |