#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
long long max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W){
vector<pair<int,int>> v[N];
vector<long long>pre[N];
for(int i = 0;i<M;i++){
v[X[i]].push_back({Y[i],W[i]});
}
for(int i = 0;i<N;i++){
v[i].push_back({N,0});
sort(v[i].begin(),v[i].end());
pre[i].assign(v[i].size(),0);
for(int j = 0;j<v[i].size();j++){
pre[i][j] = v[i][j].second + (j?pre[i][j-1]:0);
}
}
vector<pair<int,long long>> smaller = {{-1,0}};
vector<pair<int,long long>> bigger = {{-1,0}};
long long oldmaxi = 0;
long long maxi = 0;
for(int i = 0;i<N;i++){
vector<pair<int,long long>> nwsmaller;
vector<pair<int,long long>> nwbigger;
int nwsz = v[i].size();
int sz = smaller.size();
for(int j = 0;j<nwsz;j++){
nwsmaller.push_back({v[i][j].first-1,-1e18});
nwbigger.push_back({v[i][j].first-1,-1e18});
}
//1. operation
vector<pair<int,long long>> vals;
int pt = -1;
for(int j = 0;j<sz;j++){
while(pt != nwsz - 1 && v[i][pt + 1].first <= smaller[j].first)
pt++;
vals.push_back({smaller[j].first,smaller[j].second + (pt == -1?0:pre[i][pt])});
}
for(int j = sz-2;j>=0;j--){
vals[j].second = max(vals[j].second,vals[j+1].second);
}
pt = 0;
for(int j = 0;j < nwsz;j++){
while(pt != sz && vals[pt].first < nwsmaller[j].first)
pt++;
nwsmaller[j].second = max(nwsmaller[j].second,(pt != sz?vals[pt].second:(long long)-1e18) - (j?pre[i][j-1]:0));
}
//2. operation
pt = -1;
for(int j = 0;j < nwsz;j++){
while(i && pt != sz - 1&& v[i-1][pt+1].first <= nwbigger[j].first)
pt++;
nwbigger[j].second = max(nwbigger[j].second,oldmaxi + (pt != -1?pre[i-1][pt]:0));
}
//3. operation
vals.clear();
for(int j = 0;j<sz;j++){
vals.push_back(smaller[j]);
}
for(int j = 1;j<sz;j++){
vals[j].second = max(vals[j].second,vals[j-1].second);
}
pt = -1;
for(int j = 0;j<nwsz;j++){
while(pt != sz - 1 && vals[pt + 1].first < nwbigger[j].first)
pt++;
nwbigger[j].second = max(nwbigger[j].second,(pt != -1?vals[pt].second:(long long)-1e18));
}
//4. operation
vals.clear();
pt = -1;
for(int j = 0;j<sz;j++){
while(i && pt != sz -1 && v[i-1][pt + 1].first <= bigger[j].first)
pt++;
vals.push_back({bigger[j].first,bigger[j].second - (pt != -1?pre[i-1][pt]:0)});
}
for(int j = 1;j<sz;j++){
vals[j].second = max(vals[j].second,vals[j-1].second);
}
pt = -1;
int pt2 = -1;
for(int j = 0;j<nwsz;j++){
while(pt != sz - 1 && vals[pt + 1].first < nwbigger[j].first)
pt++;
while(i && pt2 != sz - 1&& v[i-1][pt2+1].first <= nwbigger[j].first)
pt2++;
nwbigger[j].second = max(nwbigger[j].second,(pt != -1?vals[pt].second:(long long)-1e18) + (pt2 != -1?pre[i-1][pt2]:0));
}
//5. operation
vals.clear();
pt = -1;
for(int j = 0;j<sz;j++){
while(i && pt != nwsz-1 && v[i][pt + 1].first <= bigger[j].first)
pt++;
vals.push_back({bigger[j].first,bigger[j].second + (pt != -1?pre[i][pt]:0)});
}
for(int j = sz-2;j>=0;j--){
vals[j].second = max(vals[j].second,vals[j+1].second);
}
// cout << "vals" << endl;
// for(auto u:vals){
// cout << u.first << ' ' << u.second << endl;
// }
pt = 0;
pt2 = -1;
for(int j = 0;j<nwsz;j++){
while(pt != sz && vals[pt].first < nwsmaller[j].first)
pt++;
while(pt2 != nwsz -1&& v[i][pt2+1].first <= nwsmaller[j].first)
pt2++;
// cout << "num"<< endl;
// cout << pt << ' ' << pt2 << endl;
nwsmaller[j].second = max(nwsmaller[j].second,(pt != sz?vals[pt].second:(long long)-1e18) - (pt2 != -1?pre[i][pt2]:0));
}
//operations done
oldmaxi = maxi;
smaller = nwsmaller;
bigger = nwbigger;
maxi = -1e18;
for(auto u:smaller){
maxi = max(maxi,u.second);
}
for(auto u:bigger){
maxi = max(maxi,u.second);
}
// cout << "HEY:" << i << endl;
// for(auto u:smaller){
// cout << u.first << ' ' << u.second << endl;
// }
// for(auto u:bigger){
// cout << u.first << ' ' << u.second << endl;
// }
}
return maxi;
}
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:15:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | for(int j = 0;j<v[i].size();j++){
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
19076 KB |
Output is correct |
2 |
Correct |
71 ms |
21888 KB |
Output is correct |
3 |
Correct |
21 ms |
11220 KB |
Output is correct |
4 |
Correct |
21 ms |
11252 KB |
Output is correct |
5 |
Correct |
151 ms |
30596 KB |
Output is correct |
6 |
Correct |
135 ms |
23024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
85 ms |
25960 KB |
Output is correct |
3 |
Correct |
99 ms |
29752 KB |
Output is correct |
4 |
Correct |
54 ms |
19128 KB |
Output is correct |
5 |
Correct |
61 ms |
21836 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
21 ms |
11220 KB |
Output is correct |
11 |
Correct |
21 ms |
11256 KB |
Output is correct |
12 |
Correct |
52 ms |
19172 KB |
Output is correct |
13 |
Correct |
60 ms |
21948 KB |
Output is correct |
14 |
Correct |
60 ms |
17792 KB |
Output is correct |
15 |
Correct |
62 ms |
20412 KB |
Output is correct |
16 |
Correct |
52 ms |
17860 KB |
Output is correct |
17 |
Correct |
56 ms |
19648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
11248 KB |
Output is correct |
2 |
Correct |
21 ms |
11220 KB |
Output is correct |
3 |
Correct |
41 ms |
11316 KB |
Output is correct |
4 |
Correct |
35 ms |
12112 KB |
Output is correct |
5 |
Correct |
62 ms |
13508 KB |
Output is correct |
6 |
Correct |
70 ms |
13600 KB |
Output is correct |
7 |
Correct |
59 ms |
13516 KB |
Output is correct |
8 |
Correct |
59 ms |
13524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
16 ms |
2260 KB |
Output is correct |
18 |
Correct |
16 ms |
2504 KB |
Output is correct |
19 |
Correct |
15 ms |
2388 KB |
Output is correct |
20 |
Correct |
15 ms |
2364 KB |
Output is correct |
21 |
Correct |
15 ms |
2368 KB |
Output is correct |
22 |
Correct |
30 ms |
4436 KB |
Output is correct |
23 |
Correct |
4 ms |
596 KB |
Output is correct |
24 |
Correct |
11 ms |
1620 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
16 ms |
2260 KB |
Output is correct |
18 |
Correct |
16 ms |
2504 KB |
Output is correct |
19 |
Correct |
15 ms |
2388 KB |
Output is correct |
20 |
Correct |
15 ms |
2364 KB |
Output is correct |
21 |
Correct |
15 ms |
2368 KB |
Output is correct |
22 |
Correct |
30 ms |
4436 KB |
Output is correct |
23 |
Correct |
4 ms |
596 KB |
Output is correct |
24 |
Correct |
11 ms |
1620 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
75 ms |
10416 KB |
Output is correct |
29 |
Correct |
107 ms |
13900 KB |
Output is correct |
30 |
Correct |
112 ms |
13132 KB |
Output is correct |
31 |
Correct |
114 ms |
13148 KB |
Output is correct |
32 |
Correct |
102 ms |
14436 KB |
Output is correct |
33 |
Correct |
117 ms |
13188 KB |
Output is correct |
34 |
Correct |
113 ms |
13152 KB |
Output is correct |
35 |
Correct |
43 ms |
5608 KB |
Output is correct |
36 |
Correct |
113 ms |
13788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
11248 KB |
Output is correct |
2 |
Correct |
21 ms |
11220 KB |
Output is correct |
3 |
Correct |
41 ms |
11316 KB |
Output is correct |
4 |
Correct |
35 ms |
12112 KB |
Output is correct |
5 |
Correct |
62 ms |
13508 KB |
Output is correct |
6 |
Correct |
70 ms |
13600 KB |
Output is correct |
7 |
Correct |
59 ms |
13516 KB |
Output is correct |
8 |
Correct |
59 ms |
13524 KB |
Output is correct |
9 |
Correct |
56 ms |
13604 KB |
Output is correct |
10 |
Correct |
48 ms |
8864 KB |
Output is correct |
11 |
Correct |
96 ms |
17500 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
22 ms |
11220 KB |
Output is correct |
19 |
Correct |
27 ms |
11252 KB |
Output is correct |
20 |
Correct |
21 ms |
11156 KB |
Output is correct |
21 |
Correct |
22 ms |
11188 KB |
Output is correct |
22 |
Correct |
63 ms |
14048 KB |
Output is correct |
23 |
Correct |
99 ms |
17524 KB |
Output is correct |
24 |
Correct |
121 ms |
17520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
19076 KB |
Output is correct |
2 |
Correct |
71 ms |
21888 KB |
Output is correct |
3 |
Correct |
21 ms |
11220 KB |
Output is correct |
4 |
Correct |
21 ms |
11252 KB |
Output is correct |
5 |
Correct |
151 ms |
30596 KB |
Output is correct |
6 |
Correct |
135 ms |
23024 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
85 ms |
25960 KB |
Output is correct |
9 |
Correct |
99 ms |
29752 KB |
Output is correct |
10 |
Correct |
54 ms |
19128 KB |
Output is correct |
11 |
Correct |
61 ms |
21836 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
21 ms |
11220 KB |
Output is correct |
17 |
Correct |
21 ms |
11256 KB |
Output is correct |
18 |
Correct |
52 ms |
19172 KB |
Output is correct |
19 |
Correct |
60 ms |
21948 KB |
Output is correct |
20 |
Correct |
60 ms |
17792 KB |
Output is correct |
21 |
Correct |
62 ms |
20412 KB |
Output is correct |
22 |
Correct |
52 ms |
17860 KB |
Output is correct |
23 |
Correct |
56 ms |
19648 KB |
Output is correct |
24 |
Correct |
21 ms |
11248 KB |
Output is correct |
25 |
Correct |
21 ms |
11220 KB |
Output is correct |
26 |
Correct |
41 ms |
11316 KB |
Output is correct |
27 |
Correct |
35 ms |
12112 KB |
Output is correct |
28 |
Correct |
62 ms |
13508 KB |
Output is correct |
29 |
Correct |
70 ms |
13600 KB |
Output is correct |
30 |
Correct |
59 ms |
13516 KB |
Output is correct |
31 |
Correct |
59 ms |
13524 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
16 ms |
2260 KB |
Output is correct |
49 |
Correct |
16 ms |
2504 KB |
Output is correct |
50 |
Correct |
15 ms |
2388 KB |
Output is correct |
51 |
Correct |
15 ms |
2364 KB |
Output is correct |
52 |
Correct |
15 ms |
2368 KB |
Output is correct |
53 |
Correct |
30 ms |
4436 KB |
Output is correct |
54 |
Correct |
4 ms |
596 KB |
Output is correct |
55 |
Correct |
11 ms |
1620 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
4 ms |
596 KB |
Output is correct |
58 |
Correct |
2 ms |
596 KB |
Output is correct |
59 |
Correct |
75 ms |
10416 KB |
Output is correct |
60 |
Correct |
107 ms |
13900 KB |
Output is correct |
61 |
Correct |
112 ms |
13132 KB |
Output is correct |
62 |
Correct |
114 ms |
13148 KB |
Output is correct |
63 |
Correct |
102 ms |
14436 KB |
Output is correct |
64 |
Correct |
117 ms |
13188 KB |
Output is correct |
65 |
Correct |
113 ms |
13152 KB |
Output is correct |
66 |
Correct |
43 ms |
5608 KB |
Output is correct |
67 |
Correct |
113 ms |
13788 KB |
Output is correct |
68 |
Correct |
56 ms |
13604 KB |
Output is correct |
69 |
Correct |
48 ms |
8864 KB |
Output is correct |
70 |
Correct |
96 ms |
17500 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
1 ms |
212 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
22 ms |
11220 KB |
Output is correct |
78 |
Correct |
27 ms |
11252 KB |
Output is correct |
79 |
Correct |
21 ms |
11156 KB |
Output is correct |
80 |
Correct |
22 ms |
11188 KB |
Output is correct |
81 |
Correct |
63 ms |
14048 KB |
Output is correct |
82 |
Correct |
99 ms |
17524 KB |
Output is correct |
83 |
Correct |
121 ms |
17520 KB |
Output is correct |
84 |
Correct |
151 ms |
25132 KB |
Output is correct |
85 |
Correct |
140 ms |
26088 KB |
Output is correct |
86 |
Correct |
186 ms |
20940 KB |
Output is correct |
87 |
Correct |
164 ms |
21988 KB |
Output is correct |
88 |
Correct |
0 ms |
212 KB |
Output is correct |
89 |
Correct |
154 ms |
22000 KB |
Output is correct |
90 |
Correct |
147 ms |
23000 KB |
Output is correct |
91 |
Correct |
133 ms |
23548 KB |
Output is correct |