#include "closing.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T1, typename T2>
using indexed_map = tree<T1, T2, less<T1>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define pb push_back
#define loop(x, i) for (int i = 0; i < x; i++)
#define rev(x, i) for (int i = (int)(x) - 1; i >= 0; i--)
#define ALL(x) begin(x), end(x)
typedef pair<int, int> ii;
typedef signed i32;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<i32> vi32;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
i32 max_score(i32 N, i32 X, i32 Y, int K, vi32 U, vi32 V, vi32 W)
{
vvii adj(N);
loop(N - 1, i)
{
adj[U[i]].pb({V[i], W[i]});
adj[V[i]].pb({U[i], W[i]});
}
vi xTime(N), yTime(N);
auto dfs = [&](auto &&self, int i, int prev, vi &time) -> void
{
for (auto [j, w] : adj[i])
{
if (j == prev)
continue;
time[j] = time[i] + w;
self(self, j, i, time);
}
};
dfs(dfs, X, X, xTime);
dfs(dfs, Y, Y, yTime);
vi pSum = {0}, sufSum(N);
vi pSum2 = {0}, sufSum2(N);
loop(N - 1, i)
{
pSum.pb(pSum.back() + W[i]);
pSum2.pb(pSum2.back() + pSum.back());
}
rev(N - 1, i)
{
sufSum[i] = sufSum[i + 1] + W[i];
sufSum2[i] = sufSum2[i + 1] + sufSum[i];
}
auto sumDis = [&](int l, int r)
{
if (r < l)
{
// gebruk sufSum
throw;
}
int minCount = (r * (r - 1)) / 2 - (l * (l - 1)) / 2;
int dis = pSum2[r] - pSum2[l] - minCount * pSum[l];
return dis;
};
int res = 0;
int lxDis = 0;
for (int lx = X; lx >= 0; lx--)
{
lxDis += pSum[X] - pSum[lx];
int rxDis = 0;
for (int rx = X; rx < N; rx++)
{
if (rx > Y)
{
rxDis += max(xTime[rx] - yTime[rx], 0LL);
}
else
rxDis += pSum[rx] - pSum[X];
int lyDis = 0;
for (int ly = Y; ly >= 0; ly--)
{
if (ly <= rx)
{
lyDis += max(yTime[ly] - xTime[ly], 0LL);
}
else
lyDis += pSum[Y] - pSum[ly];
int ryDis = 0;
for (int ry = Y; ry < N; ry++)
{
ryDis += pSum[ry] - pSum[Y];
int dis = rxDis + lxDis + ryDis + lyDis;
if (dis <= K)
{
int cnt = (rx - lx + 1) + (ry - ly + 1);
res = max(res, cnt);
}
}
}
}
}
/*for (int both = 0; both < N; both++)
{
int best = 0, bestDis = INT_MAX;
for (int i = X; i <= Y; i++)
{
int dis = sumDis(X, i) + sumDis(i, Y);
if (dis < bestDis)
{
best = i;
bestDis = dis;
}
}
}*/
return res;
}
Compilation message
closing.cpp: In function 'i32 max_score(i32, i32, i32, long long int, vi32, vi32, vi32)':
closing.cpp:57:10: warning: variable 'sumDis' set but not used [-Wunused-but-set-variable]
57 | auto sumDis = [&](int l, int r)
| ^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1031 ms |
35768 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
1st lines differ - on the 1st token, expected: '30', found: '35' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
1st lines differ - on the 1st token, expected: '30', found: '35' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
1st lines differ - on the 1st token, expected: '30', found: '35' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
1st lines differ - on the 1st token, expected: '30', found: '35' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
1st lines differ - on the 1st token, expected: '30', found: '35' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
1st lines differ - on the 1st token, expected: '30', found: '35' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
1st lines differ - on the 1st token, expected: '30', found: '35' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
1st lines differ - on the 1st token, expected: '30', found: '35' |
4 |
Halted |
0 ms |
0 KB |
- |