#include <bits/stdc++.h>
#include "fish.h"
#define ll long long
#define ar array
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
using namespace std;
const ll INF = 1e18;
template<typename T> bool ckmin(T &a, const T &b ) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b ) { return a < b ? a = b, 1 : 0; }
struct seg {
int N; vector<ll> tree;
seg(int n) : N(1<<(__lg(n)+1)), tree(2*N, -1e18) {}
void update(int pos, ll x) {
for (int i = pos + N; i; i >>= 1) ckmax(tree[i], x);
}
ll query(int node, int nl, int nr, int ql, int qr) {
if (ql > nr || qr < nl) return -1e18;
if (ql <= nl && nr <= qr) return tree[node];
int mid = (nl+nr)/2;
return max(query(node*2, nl, mid, ql, qr), query(node*2+1, mid+1, nr, ql, qr));
}
ll query(int l, int r) {
return query(1, 0, N-1, l, r);
}
};
long long max_weights(int n, int m, std::vector<int> x, std::vector<int> y, std::vector<int> w) {
vector<vector<ar<ll, 2>>> each(n);
for (int i = 0; i < m; i++) {
each[x[i]].push_back({y[i], w[i]});
each[x[i]].push_back({y[i]-1, 0});
}
for (int i = 0; i < n; i++) each[i].push_back({n-1, 0}), each[i].push_back({-1, 0});
for (int i = 0; i < n; i++) {
sort(all(each[i]));
vector<ar<ll, 2>> nw;
ll s = 0;
for (int j = 0; j < sz(each[i]); j++) {
int r = j;
while (r+1 < sz(each[i]) && each[i][r+1][0] == each[i][r][0]) r++;
for (int k = j; k <= r; k++) s += each[i][k][1];
nw.push_back({each[i][j][0], s});
j = r;
}
assert(is_sorted(all(nw)));
swap(each[i], nw);
}
vector<vector<ll>> dp;
auto query = [&](int pos, int x) {
return prev(upper_bound(all(each[pos]), ar<ll, 2>{x, LLONG_MAX})) - each[pos].begin();
};
vector<vector<ar<int, 3>>> val(n);
for (int i = 0; i < n; i++) {
val[i].resize(sz(each[i]));
for (int k = 0; k < sz(each[i]); k++) {
for (int j = -1; j <= 1; j++) {
if (i + j >= 0 && i + j < n) {
val[i][k][j+1] = query(i + j, each[i][k][0]);
}
}
}
}
vector<vector<ll>> dp3, dp4, dp5;
for (int i = 0; i < n; i++) {
vector<vector<ll>> dp2;
const int N = sz(each[i]);
dp2.resize(i ? sz(each[i-1]) : 1, vector<ll>(N, -INF));
if (!i) {
for (int j = 0; j < N; j++) {
dp2[0][j] = 0;
}
swap(dp, dp2);
}
else {
dp3 = dp, dp4 = dp;
for (int i = 1; i < sz(dp3); i++) for (int j = 0; j < sz(dp[i]); j++) ckmax(dp3[i][j], dp3[i-1][j]);
for (int j = sz(dp4)-1; j >= 0; j--) for (int k = 0; k < sz(dp[j]); k++) dp4[j][k] = dp[j][k] + (i-2 >= 0 ? each[i-1][val[i-2][j][2]][1] : 0) - each[i-1][k][1];
for (int j = sz(dp4)-2; j >= 0; j--) for (int k = 0; k < sz(dp4[j]); k++) ckmax(dp4[j][k], dp4[j+1][k]);
vector<seg> st(sz(dp[0]), seg(sz(dp)));
for (int j = 0; j < sz(dp); j++) {
for (int k = 0; k < sz(dp[j]); k++) {
st[k].update(j, dp[j][k]);
}
}
for (int k = 0; k < sz(dp[0]); k++) {
for (int l = 0; l < N; l++) {
int ans = 0;
{
int l2 = 0, r = i-2 >= 0 ? sz(each[i-2]) - 1 : -1;
while (l2 <= r) {
int mid = (l2+r)/2;
if (val[i-2][mid][2] <= val[i][l][0]) ans = mid, l2 = mid+1;
else r = mid-1;
}
}
{
int L = k, R = val[i][l][0];
ll cost = L <= R ? each[i-1][R][1] - each[i-1][L][1] : 0;
ckmax(dp2[k][l], dp3[ans][k] + cost);
}
{
if (ans + 1 < sz(dp)) {
assert(i-2 >= 0);
int l2 = ans+1, r2 = sz(dp)-1, ans2 = sz(dp);
while (l2 <= r2) {
int mid2 = (l2+r2)/2;
if (val[i-2][mid2][2] >= k) ans2 = mid2, r2 = mid2-1;
else l2 = mid2+1;
}
//for (int j = l2; j < ans2; j++) ckmax(dp2[k][l], dp[j][k]);
if (l2 <= ans2 - 1) ckmax(dp2[k][l], st[k].query(l2, ans2 - 1));
if (ans2 < sz(dp)) ckmax(dp2[k][l], dp4[ans2][k]);
}
}
}
}
swap(dp, dp2);
}
}
ll ans = -INF;
for (int i = 0; i < sz(dp); i++) {
for (int j = 0; j < sz(dp[i]); j++) {
int L = j, R = val[n-2][i][2];
ckmax(ans, dp[i][j] + (L <= R ? each[n-1][R][1] - each[n-1][L][1] : 0));
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
110 ms |
38736 KB |
Output is correct |
2 |
Correct |
138 ms |
42984 KB |
Output is correct |
3 |
Correct |
57 ms |
12892 KB |
Output is correct |
4 |
Correct |
43 ms |
12952 KB |
Output is correct |
5 |
Correct |
295 ms |
61528 KB |
Output is correct |
6 |
Correct |
302 ms |
47172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
432 KB |
Output is correct |
2 |
Execution timed out |
1032 ms |
2097152 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
12860 KB |
Output is correct |
2 |
Correct |
80 ms |
12948 KB |
Output is correct |
3 |
Correct |
93 ms |
16292 KB |
Output is correct |
4 |
Correct |
94 ms |
15956 KB |
Output is correct |
5 |
Correct |
97 ms |
21840 KB |
Output is correct |
6 |
Correct |
98 ms |
21072 KB |
Output is correct |
7 |
Correct |
90 ms |
21596 KB |
Output is correct |
8 |
Correct |
94 ms |
21712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
436 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Incorrect |
1 ms |
348 KB |
1st lines differ - on the 1st token, expected: '278622587073', found: '277471986209' |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
436 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Incorrect |
1 ms |
348 KB |
1st lines differ - on the 1st token, expected: '278622587073', found: '277471986209' |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
436 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Incorrect |
1 ms |
348 KB |
1st lines differ - on the 1st token, expected: '278622587073', found: '277471986209' |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
12860 KB |
Output is correct |
2 |
Correct |
80 ms |
12948 KB |
Output is correct |
3 |
Correct |
93 ms |
16292 KB |
Output is correct |
4 |
Correct |
94 ms |
15956 KB |
Output is correct |
5 |
Correct |
97 ms |
21840 KB |
Output is correct |
6 |
Correct |
98 ms |
21072 KB |
Output is correct |
7 |
Correct |
90 ms |
21596 KB |
Output is correct |
8 |
Correct |
94 ms |
21712 KB |
Output is correct |
9 |
Correct |
149 ms |
23100 KB |
Output is correct |
10 |
Incorrect |
119 ms |
13936 KB |
1st lines differ - on the 1st token, expected: '36454348383152', found: '36369094783841' |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
110 ms |
38736 KB |
Output is correct |
2 |
Correct |
138 ms |
42984 KB |
Output is correct |
3 |
Correct |
57 ms |
12892 KB |
Output is correct |
4 |
Correct |
43 ms |
12952 KB |
Output is correct |
5 |
Correct |
295 ms |
61528 KB |
Output is correct |
6 |
Correct |
302 ms |
47172 KB |
Output is correct |
7 |
Correct |
1 ms |
432 KB |
Output is correct |
8 |
Execution timed out |
1032 ms |
2097152 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |