This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "closing.h"
using namespace std;
#define int long long
struct BIT
{
int n;
vector<long long> ft;
void init(int N)
{
n = N + 5;
ft.assign(n + 5, 0);
}
void add(int pos, long long val)
{
for (pos = pos + 1; pos <= n; pos += pos & -pos) ft[pos] += val;
}
long long get(int pos, long long res = 0)
{
for (pos = pos + 1; pos > 0; pos -= pos & -pos) res += ft[pos];
return res;
}
};
const int MXN = 2e5 + 5;
int n, x, y;
long long k;
vector<array<long long, 2>> adj[MXN];
long long d[2][MXN], p[3][MXN];
vector<int> pt, PT;
int in[MXN];
void dfs(int a, int p, int t)
{
PT.push_back(a);
if (a != p && a == y) pt = PT;
for (const array<long long, 2> &v : adj[a])
{
if (v[0] == p) continue;
d[t][v[0]] = d[t][a] + v[1];
dfs(v[0], a, t);
}
PT.pop_back();
}
void getcomp(int a, int p, vector<int> &comp)
{
for (array<long long, 2> &v : adj[a])
{
if (v[0] == p) continue;
getcomp(v[0], a, comp);
}
comp.push_back(a);
}
#undef int long long
int max_score(int N, int X, int Y, long long K, vector<int> U, vector<int> V, vector<int> W)
{
#define int long long
n = N, x = X, y = Y, k = K;
for (int i = 0; i < n; i++)
{
d[0][i] = d[1][i] = 0;
adj[i].clear();
in[i] = 0;
}
int f = 1;
for (int i = 0; i < n - 1; i++)
{
adj[U[i]].push_back({V[i], W[i]});
adj[V[i]].push_back({U[i], W[i]});
f &= U[i] == i && V[i] == i + 1;
}
dfs(x, x, 0);
dfs(y, y, 1);
int ans = 0;
{
vector<long long> v;
for (int i = 0; i < n; i++) v.push_back(min(d[0][i], d[1][i]));
sort(v.begin(), v.end());
long long k1 = k;
for (int i = 0; i < n; i++)
{
k1 -= v[i];
if (k1 < 0) break;
ans++;
}
}
if (f)
{
for (int j = 0; j < 3; j++)
{
for (int i = 0; i < N; i++)
{
if (j < 2) p[j][i] = (i ? p[j][i - 1] : 0) + d[j][i];
else p[j][i] = (i ? p[j][i - 1] : 0) + max(d[0][i], d[1][i]);
}
}
for (int i = 0; i < n && d[0][i] <= d[1][i]; i++)
{
BIT ft, cnt;
ft.init(n), cnt.init(n);
vector<array<long long, 2>> v;
vector<int> idx(n, -1);
for (int j = 0; j < min(i, x); j++) v.push_back({d[0][j], j});
for (int j = max(i, y) + 1; j < n; j++) v.push_back({d[1][j], j});
sort(v.begin(), v.end());
for (int j = 0; j < v.size(); j++)
{
idx[v[j][1]] = j;
ft.add(j, v[j][0]);
cnt.add(j, 1);
}
for (int j = i; j < n; j++)
{
if (idx[j] != -1) ft.add(idx[j], -v[idx[j]][0]), cnt.add(idx[j], -1);
if (max(i, x) > min(j, y)) continue;
long long k1 = k - (p[2][j] - (i ? p[2][i - 1] : 0));
if (X < i) k1 -= p[0][i - 1] - (X ? p[0][X - 1] : 0);
if (j < Y) k1 -= p[1][Y] - p[1][j];
if (k1 < 0) continue;
int res = max(j, y) - min(i, x) + 1 + (j - i + 1);
int l = 0, r = n - 1;
while (l < r)
{
int mid = (l + r + 1) >> 1;
if (ft.get(mid) <= k1) l = mid;
else r = mid - 1;
}
if (ft.get(l) <= k1) res += cnt.get(l);
ans = max(ans, res);
}
}
return ans;
}
for (int &i : pt) in[i] = 1;
vector<int> cc[pt.size()];
for (int i = 0; i < pt.size(); i++)
{
getcomp(pt[i], pt[i], cc[i]);
cc[i].pop_back();
}
for (int i = 0; i < pt.size(); i++)
{
for (int j = i; j < pt.size(); j++)
{
int cost = 0, res = pt.size() + (j - i + 1);
for (int k = 0; k < pt.size(); k++)
{
if (k < i) cost += d[0][pt[k]];
else if (k <= j) cost += max(d[0][pt[k]], d[1][pt[k]]);
else cost += d[1][pt[k]];
}
if (cost > k) continue;
vector<long long> v;
for (int k = 0; k < pt.size(); k++)
{
if (k < i) for (int &l : cc[k]) v.push_back(d[0][l]);
else if (k <= j) for (int &l : cc[k]) v.push_back(max(d[0][l], d[1][l]));
else for (int &l : cc[k]) v.push_back(d[1][l]);
}
sort(v.begin(), v.end());
for (long long &val : v)
{
cost += val;
if (cost > k) break;
res++;
}
ans = max(ans, res);
}
}
return ans;
#undef int
}
Compilation message (stderr)
closing.cpp:58:12: warning: extra tokens at end of #undef directive
58 | #undef int long long
| ^~~~
closing.cpp: In function 'int max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:110:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | for (int j = 0; j < v.size(); j++)
| ~~^~~~~~~~~~
closing.cpp:140:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
140 | for (int i = 0; i < pt.size(); i++)
| ~~^~~~~~~~~~~
closing.cpp:145:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
145 | for (int i = 0; i < pt.size(); i++)
| ~~^~~~~~~~~~~
closing.cpp:147:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
147 | for (int j = i; j < pt.size(); j++)
| ~~^~~~~~~~~~~
closing.cpp:150:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
150 | for (int k = 0; k < pt.size(); k++)
| ~~^~~~~~~~~~~
closing.cpp:158:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
158 | for (int k = 0; k < pt.size(); k++)
| ~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |