#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,popcnt,sse4,abm")
#include <bits/stdc++.h>
#include "fish.h"
using namespace std;
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const ll INF = 1e18;
const int SIZE = 1e5 + 5;
const int K = 10000;
int n;
vector<pair<int, int>> v[SIZE];
ll ans, zero[2], fzero[2], all[2];
vector<pair<int, ll>> inc[2], dek[2];
#define SORTBYTE(TYPE, FR, TO, LEN, BIT) {\
memset(bucket, 0, sizeof(bucket));\
for (auto it = (FR) + LEN; it != (FR); it--)\
++bucket[(it[-1].F >> BIT) & 255];\
for (int *it = bucket; it != bucket + 255; it++)\
it[1] += it[0];\
for (auto it = (FR) + LEN; it != (FR); it--)\
(TO)[--bucket[(it[-1].F >> BIT) & 255]] = it[-1];\
}
int bucket[256];
pair<int, ll> tmp[SIZE];
inline void chmax(ll &x, const ll &y) {
x = max(x, y);
}
ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
n = N;
FOR (i, 0, M - 1) {
X[i]++, Y[i]++;
v[X[i]].pb(Y[i], W[i]);
}
fzero[0] = all[0] = -INF;
FOR (i, 1, n) {
int f = i & 1;
if (v[i].size() >= K) {
SORTBYTE(int, v[i].begin(), tmp, v[i].size(), 0);
SORTBYTE(int, tmp, v[i].begin(), v[i].size(), 8);
SORTBYTE(int, v[i].begin(), tmp, v[i].size(), 16);
SORTBYTE(int, tmp, v[i].begin(), v[i].size(), 24);
} else {
sort(v[i].begin(), v[i].end());
}
zero[f] = fzero[f] = all[f] = -INF;
inc[f].clear(), dek[f].clear();
ll mx = zero[!f], sum = 0;
int pos = 0;
for (auto [j, x] : v[i - 1]) {
while (pos < inc[!f].size() && inc[!f][pos].F < j) {
mx = max(mx, inc[!f][pos].S - sum);
pos++;
}
sum += x;
inc[f].pb(j, sum + mx);
}
while (pos < inc[!f].size() && inc[!f][pos].F < n) {
mx = max(mx, inc[!f][pos].S - sum);
pos++;
}
chmax(all[f], sum + mx);
mx = all[!f], sum = 0, pos = (int) dek[!f].size() - 1;
for (auto [j, x] : v[i]) sum += x;
mx += sum;
for (auto it = v[i].rbegin(); it != v[i].rend(); it++) {
auto [j, x] = *it;
j--;
while (pos >= 0 && dek[!f][pos].F > j) {
mx = max(mx, dek[!f][pos].S + sum);
pos--;
}
sum -= x;
dek[f].pb(j, mx - sum);
}
while (pos >= 0 && dek[!f][pos].F > 0) {
mx = max(mx, dek[!f][pos].S + sum);
pos--;
}
chmax(fzero[f], mx - sum);
reverse(dek[f].begin(), dek[f].end());
chmax(zero[f], zero[!f]);
chmax(zero[f], fzero[!f]);
chmax(all[f], fzero[!f]);
chmax(all[f], all[!f]);
}
int f = n & 1;
ll ans = 0;
chmax(ans, zero[f]);
chmax(ans, fzero[f]);
chmax(ans, all[f]);
return ans;
}
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:63:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | while (pos < inc[!f].size() && inc[!f][pos].F < j) {
| ~~~~^~~~~~~~~~~~~~~~
fish.cpp:70:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | while (pos < inc[!f].size() && inc[!f][pos].F < n) {
| ~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
10760 KB |
Output is correct |
2 |
Correct |
31 ms |
11924 KB |
Output is correct |
3 |
Correct |
3 ms |
2644 KB |
Output is correct |
4 |
Correct |
3 ms |
2644 KB |
Output is correct |
5 |
Correct |
83 ms |
18424 KB |
Output is correct |
6 |
Correct |
100 ms |
13984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
62 ms |
15860 KB |
Output is correct |
3 |
Correct |
63 ms |
18284 KB |
Output is correct |
4 |
Correct |
25 ms |
10680 KB |
Output is correct |
5 |
Correct |
31 ms |
11884 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
3 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
25 ms |
10824 KB |
Output is correct |
13 |
Correct |
31 ms |
11884 KB |
Output is correct |
14 |
Correct |
25 ms |
9264 KB |
Output is correct |
15 |
Correct |
28 ms |
9860 KB |
Output is correct |
16 |
Correct |
25 ms |
9284 KB |
Output is correct |
17 |
Correct |
29 ms |
9712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2644 KB |
Output is correct |
2 |
Correct |
3 ms |
2540 KB |
Output is correct |
3 |
Correct |
22 ms |
5496 KB |
Output is correct |
4 |
Correct |
14 ms |
4540 KB |
Output is correct |
5 |
Correct |
32 ms |
8048 KB |
Output is correct |
6 |
Correct |
29 ms |
8020 KB |
Output is correct |
7 |
Correct |
30 ms |
8148 KB |
Output is correct |
8 |
Correct |
31 ms |
8148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2660 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2660 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2636 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
14 ms |
4180 KB |
Output is correct |
18 |
Correct |
14 ms |
4436 KB |
Output is correct |
19 |
Correct |
17 ms |
4348 KB |
Output is correct |
20 |
Correct |
14 ms |
4424 KB |
Output is correct |
21 |
Correct |
14 ms |
4340 KB |
Output is correct |
22 |
Correct |
28 ms |
6156 KB |
Output is correct |
23 |
Correct |
4 ms |
2900 KB |
Output is correct |
24 |
Correct |
10 ms |
3640 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
4 ms |
2968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2660 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2636 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
14 ms |
4180 KB |
Output is correct |
18 |
Correct |
14 ms |
4436 KB |
Output is correct |
19 |
Correct |
17 ms |
4348 KB |
Output is correct |
20 |
Correct |
14 ms |
4424 KB |
Output is correct |
21 |
Correct |
14 ms |
4340 KB |
Output is correct |
22 |
Correct |
28 ms |
6156 KB |
Output is correct |
23 |
Correct |
4 ms |
2900 KB |
Output is correct |
24 |
Correct |
10 ms |
3640 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
4 ms |
2968 KB |
Output is correct |
27 |
Correct |
3 ms |
2812 KB |
Output is correct |
28 |
Correct |
63 ms |
10936 KB |
Output is correct |
29 |
Correct |
91 ms |
13720 KB |
Output is correct |
30 |
Correct |
92 ms |
13080 KB |
Output is correct |
31 |
Correct |
93 ms |
13004 KB |
Output is correct |
32 |
Correct |
84 ms |
14540 KB |
Output is correct |
33 |
Correct |
96 ms |
13096 KB |
Output is correct |
34 |
Correct |
101 ms |
13004 KB |
Output is correct |
35 |
Correct |
36 ms |
6844 KB |
Output is correct |
36 |
Correct |
92 ms |
13916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2644 KB |
Output is correct |
2 |
Correct |
3 ms |
2540 KB |
Output is correct |
3 |
Correct |
22 ms |
5496 KB |
Output is correct |
4 |
Correct |
14 ms |
4540 KB |
Output is correct |
5 |
Correct |
32 ms |
8048 KB |
Output is correct |
6 |
Correct |
29 ms |
8020 KB |
Output is correct |
7 |
Correct |
30 ms |
8148 KB |
Output is correct |
8 |
Correct |
31 ms |
8148 KB |
Output is correct |
9 |
Correct |
30 ms |
8012 KB |
Output is correct |
10 |
Correct |
31 ms |
6480 KB |
Output is correct |
11 |
Correct |
63 ms |
10472 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
3 ms |
2644 KB |
Output is correct |
19 |
Correct |
3 ms |
2644 KB |
Output is correct |
20 |
Correct |
3 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
34 ms |
7032 KB |
Output is correct |
23 |
Correct |
61 ms |
10388 KB |
Output is correct |
24 |
Correct |
63 ms |
10384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
10760 KB |
Output is correct |
2 |
Correct |
31 ms |
11924 KB |
Output is correct |
3 |
Correct |
3 ms |
2644 KB |
Output is correct |
4 |
Correct |
3 ms |
2644 KB |
Output is correct |
5 |
Correct |
83 ms |
18424 KB |
Output is correct |
6 |
Correct |
100 ms |
13984 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
62 ms |
15860 KB |
Output is correct |
9 |
Correct |
63 ms |
18284 KB |
Output is correct |
10 |
Correct |
25 ms |
10680 KB |
Output is correct |
11 |
Correct |
31 ms |
11884 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
3 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
25 ms |
10824 KB |
Output is correct |
19 |
Correct |
31 ms |
11884 KB |
Output is correct |
20 |
Correct |
25 ms |
9264 KB |
Output is correct |
21 |
Correct |
28 ms |
9860 KB |
Output is correct |
22 |
Correct |
25 ms |
9284 KB |
Output is correct |
23 |
Correct |
29 ms |
9712 KB |
Output is correct |
24 |
Correct |
3 ms |
2644 KB |
Output is correct |
25 |
Correct |
3 ms |
2540 KB |
Output is correct |
26 |
Correct |
22 ms |
5496 KB |
Output is correct |
27 |
Correct |
14 ms |
4540 KB |
Output is correct |
28 |
Correct |
32 ms |
8048 KB |
Output is correct |
29 |
Correct |
29 ms |
8020 KB |
Output is correct |
30 |
Correct |
30 ms |
8148 KB |
Output is correct |
31 |
Correct |
31 ms |
8148 KB |
Output is correct |
32 |
Correct |
2 ms |
2644 KB |
Output is correct |
33 |
Correct |
1 ms |
2644 KB |
Output is correct |
34 |
Correct |
1 ms |
2644 KB |
Output is correct |
35 |
Correct |
1 ms |
2644 KB |
Output is correct |
36 |
Correct |
1 ms |
2644 KB |
Output is correct |
37 |
Correct |
1 ms |
2644 KB |
Output is correct |
38 |
Correct |
1 ms |
2644 KB |
Output is correct |
39 |
Correct |
1 ms |
2644 KB |
Output is correct |
40 |
Correct |
2 ms |
2660 KB |
Output is correct |
41 |
Correct |
2 ms |
2644 KB |
Output is correct |
42 |
Correct |
2 ms |
2644 KB |
Output is correct |
43 |
Correct |
2 ms |
2644 KB |
Output is correct |
44 |
Correct |
2 ms |
2644 KB |
Output is correct |
45 |
Correct |
2 ms |
2644 KB |
Output is correct |
46 |
Correct |
2 ms |
2636 KB |
Output is correct |
47 |
Correct |
2 ms |
2644 KB |
Output is correct |
48 |
Correct |
14 ms |
4180 KB |
Output is correct |
49 |
Correct |
14 ms |
4436 KB |
Output is correct |
50 |
Correct |
17 ms |
4348 KB |
Output is correct |
51 |
Correct |
14 ms |
4424 KB |
Output is correct |
52 |
Correct |
14 ms |
4340 KB |
Output is correct |
53 |
Correct |
28 ms |
6156 KB |
Output is correct |
54 |
Correct |
4 ms |
2900 KB |
Output is correct |
55 |
Correct |
10 ms |
3640 KB |
Output is correct |
56 |
Correct |
2 ms |
2644 KB |
Output is correct |
57 |
Correct |
4 ms |
2968 KB |
Output is correct |
58 |
Correct |
3 ms |
2812 KB |
Output is correct |
59 |
Correct |
63 ms |
10936 KB |
Output is correct |
60 |
Correct |
91 ms |
13720 KB |
Output is correct |
61 |
Correct |
92 ms |
13080 KB |
Output is correct |
62 |
Correct |
93 ms |
13004 KB |
Output is correct |
63 |
Correct |
84 ms |
14540 KB |
Output is correct |
64 |
Correct |
96 ms |
13096 KB |
Output is correct |
65 |
Correct |
101 ms |
13004 KB |
Output is correct |
66 |
Correct |
36 ms |
6844 KB |
Output is correct |
67 |
Correct |
92 ms |
13916 KB |
Output is correct |
68 |
Correct |
30 ms |
8012 KB |
Output is correct |
69 |
Correct |
31 ms |
6480 KB |
Output is correct |
70 |
Correct |
63 ms |
10472 KB |
Output is correct |
71 |
Correct |
2 ms |
2644 KB |
Output is correct |
72 |
Correct |
1 ms |
2644 KB |
Output is correct |
73 |
Correct |
2 ms |
2644 KB |
Output is correct |
74 |
Correct |
1 ms |
2644 KB |
Output is correct |
75 |
Correct |
1 ms |
2644 KB |
Output is correct |
76 |
Correct |
2 ms |
2644 KB |
Output is correct |
77 |
Correct |
3 ms |
2644 KB |
Output is correct |
78 |
Correct |
3 ms |
2644 KB |
Output is correct |
79 |
Correct |
3 ms |
2644 KB |
Output is correct |
80 |
Correct |
2 ms |
2644 KB |
Output is correct |
81 |
Correct |
34 ms |
7032 KB |
Output is correct |
82 |
Correct |
61 ms |
10388 KB |
Output is correct |
83 |
Correct |
63 ms |
10384 KB |
Output is correct |
84 |
Correct |
91 ms |
14508 KB |
Output is correct |
85 |
Correct |
85 ms |
14448 KB |
Output is correct |
86 |
Correct |
106 ms |
14468 KB |
Output is correct |
87 |
Correct |
104 ms |
14540 KB |
Output is correct |
88 |
Correct |
2 ms |
2644 KB |
Output is correct |
89 |
Correct |
103 ms |
14496 KB |
Output is correct |
90 |
Correct |
102 ms |
14140 KB |
Output is correct |
91 |
Correct |
94 ms |
13712 KB |
Output is correct |