#include "closing.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#pragma gcc-optimize("O3, unrolls-loops")
const int nx=2e5+5;
vector<pair<ll, ll>> g[nx];
struct segtree
{
struct node
{
ll sm, f;
node(ll sm=0, ll f=0): sm(sm), f(f){}
friend node operator+(const node &a, const node &b) {return node(a.sm+b.sm, a.f+b.f);}
} d[4*nx];
void build(int l, int r, int i)
{
d[i]=node(0, 0);
if (l==r) return;
int md=(l+r)/2;
build(l, md, 2*i);
build(md+1, r, 2*i+1);
}
void update(int l, int r, int i, int idx, int f, ll vl)
{
if (idx<l||r<idx) return;
if (l==r) return d[i]=node(d[i].sm+f*vl, d[i].f+f), void();
int md=(l+r)/2;
update(l, md, 2*i, idx, f, vl);
update(md+1, r, 2*i+1, idx, f, vl);
d[i]=d[2*i]+d[2*i+1];
}
ll query(int l, int r, int i, ll vl)
{
if (vl>=d[i].sm) return d[i].f;
if (l==r) return 0;
int md=(l+r)/2;
if (d[2*i].sm>vl) return query(l, md, 2*i, vl);
else return d[2*i].f+query(md+1, r, 2*i+1, vl-d[2*i].sm);
}
} s;
void dfs(int u, int p, ll cw, vector<ll> &x)
{
x[u]=cw;
for (auto [v, w]:g[u]) if (v!=p) dfs(v, u, cw+w, x);
}
int max_score2(int N, int X, int Y, long long K,
std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
for (int i=0; i<N; i++) g[i].clear();
for (int i=0; i<U.size(); i++) g[U[i]].push_back({V[i], W[i]}), g[V[i]].push_back({U[i], W[i]});
int res=0;
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
vector<ll> vs(N);
vs[X]=vs[Y]=1;
pq.push({0, X});
pq.push({0, Y});
while (!pq.empty())
{
auto [a, u]=pq.top();
pq.pop();
if (a>K) break;
K-=a;
res++;
vs[u]=1;
for (auto [v, w]:g[u]) if (!vs[v]) pq.push({a+w, v});
}
return res;
}
int max_score(int N, int X, int Y, long long K,
std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
for (int i=0; i<N-1; i++) if (U[i]!=i||V[i]!=i+1) return max_score2(N, X, Y, K, U, V, W);
if (X>Y) swap(X, Y);
for (int i=0; i<N; i++) g[i].clear();
for (int i=0; i<N-1; i++) g[U[i]].push_back({V[i], W[i]}), g[V[i]].push_back({U[i], W[i]});
vector<ll> a(N), b(N), idxa(N), idxb(N);
ll mx=0, t=0;
map<pair<ll, ll>, ll> mp;
dfs(X, X, 0, a);
dfs(Y, Y, 0, b);
for (int i=0; i<N; i++) mp[{a[i], 1}]=0, mp[{b[i], 2}]=0;
for (auto &[x, y]:mp) y=++t;
for (int i=0; i<N; i++) idxa[i]=mp[{a[i], 1}], idxb[i]=mp[{b[i], 2}];
priority_queue<ll, vector<ll>, greater<ll>> pq;
for (int i=0; i<N; i++) pq.push(a[i]), pq.push(b[i]);
ll tmp=K;
while (!pq.empty()&&pq.top()<=tmp) mx++, tmp-=pq.top(), pq.pop();
for (int i=0; i<N; i++)
{
s.build(1, 2*N, 1);
ll cur=0, res=0;
for (int j=0; j<=min(i-1, X); j++) s.update(1, 2*N, 1, idxa[j], 1, a[j]);
for (int j=X+1; j<i; j++) cur+=a[j], res++;
for (int j=i; j<Y; j++) cur+=b[j], res++;
for (int j=max(i, Y); j<N; j++) s.update(1, 2*N, 1, idxb[j], 1, b[j]);
for (int j=i; j<N; j++)
{
cur+=max(a[j], b[j]);
res+=2;
if (j<Y) res--, cur-=b[j];
else s.update(1, 2*N, 1, idxb[j], -1, b[j]);
if (cur>K) break;
mx=max(mx, res+s.query(1, 2*N, 1, K-cur));
}
}
return mx;
}
Compilation message
closing.cpp:7: warning: ignoring '#pragma gcc ' [-Wunknown-pragmas]
7 | #pragma gcc-optimize("O3, unrolls-loops")
|
closing.cpp: In function 'int max_score2(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:58:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int i=0; i<U.size(); i++) g[U[i]].push_back({V[i], W[i]}), g[V[i]].push_back({U[i], W[i]});
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
17500 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
36436 KB |
Output is correct |
2 |
Correct |
62 ms |
40276 KB |
Output is correct |
3 |
Correct |
35 ms |
22872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
17500 KB |
Output is correct |
2 |
Correct |
5 ms |
17656 KB |
Output is correct |
3 |
Correct |
3 ms |
17500 KB |
Output is correct |
4 |
Correct |
4 ms |
17496 KB |
Output is correct |
5 |
Correct |
5 ms |
17500 KB |
Output is correct |
6 |
Correct |
4 ms |
17500 KB |
Output is correct |
7 |
Correct |
4 ms |
17500 KB |
Output is correct |
8 |
Correct |
4 ms |
17500 KB |
Output is correct |
9 |
Correct |
4 ms |
17500 KB |
Output is correct |
10 |
Correct |
4 ms |
17500 KB |
Output is correct |
11 |
Correct |
4 ms |
17500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
17500 KB |
Output is correct |
2 |
Correct |
5 ms |
17656 KB |
Output is correct |
3 |
Correct |
3 ms |
17500 KB |
Output is correct |
4 |
Correct |
4 ms |
17496 KB |
Output is correct |
5 |
Correct |
5 ms |
17500 KB |
Output is correct |
6 |
Correct |
4 ms |
17500 KB |
Output is correct |
7 |
Correct |
4 ms |
17500 KB |
Output is correct |
8 |
Correct |
4 ms |
17500 KB |
Output is correct |
9 |
Correct |
4 ms |
17500 KB |
Output is correct |
10 |
Correct |
4 ms |
17500 KB |
Output is correct |
11 |
Correct |
4 ms |
17500 KB |
Output is correct |
12 |
Correct |
4 ms |
17500 KB |
Output is correct |
13 |
Correct |
6 ms |
17684 KB |
Output is correct |
14 |
Correct |
4 ms |
17588 KB |
Output is correct |
15 |
Correct |
4 ms |
17500 KB |
Output is correct |
16 |
Correct |
4 ms |
17500 KB |
Output is correct |
17 |
Correct |
4 ms |
17500 KB |
Output is correct |
18 |
Correct |
5 ms |
17500 KB |
Output is correct |
19 |
Correct |
14 ms |
17788 KB |
Output is correct |
20 |
Correct |
9 ms |
17756 KB |
Output is correct |
21 |
Correct |
9 ms |
17752 KB |
Output is correct |
22 |
Correct |
8 ms |
17752 KB |
Output is correct |
23 |
Correct |
5 ms |
17756 KB |
Output is correct |
24 |
Correct |
5 ms |
17752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
17500 KB |
Output is correct |
2 |
Correct |
5 ms |
17656 KB |
Output is correct |
3 |
Correct |
3 ms |
17500 KB |
Output is correct |
4 |
Correct |
4 ms |
17496 KB |
Output is correct |
5 |
Correct |
5 ms |
17500 KB |
Output is correct |
6 |
Correct |
4 ms |
17500 KB |
Output is correct |
7 |
Correct |
4 ms |
17500 KB |
Output is correct |
8 |
Correct |
4 ms |
17500 KB |
Output is correct |
9 |
Correct |
4 ms |
17500 KB |
Output is correct |
10 |
Correct |
4 ms |
17500 KB |
Output is correct |
11 |
Correct |
4 ms |
17500 KB |
Output is correct |
12 |
Correct |
4 ms |
17500 KB |
Output is correct |
13 |
Correct |
6 ms |
17684 KB |
Output is correct |
14 |
Correct |
4 ms |
17588 KB |
Output is correct |
15 |
Correct |
4 ms |
17500 KB |
Output is correct |
16 |
Correct |
4 ms |
17500 KB |
Output is correct |
17 |
Correct |
4 ms |
17500 KB |
Output is correct |
18 |
Correct |
5 ms |
17500 KB |
Output is correct |
19 |
Correct |
14 ms |
17788 KB |
Output is correct |
20 |
Correct |
9 ms |
17756 KB |
Output is correct |
21 |
Correct |
9 ms |
17752 KB |
Output is correct |
22 |
Correct |
8 ms |
17752 KB |
Output is correct |
23 |
Correct |
5 ms |
17756 KB |
Output is correct |
24 |
Correct |
5 ms |
17752 KB |
Output is correct |
25 |
Correct |
12 ms |
17500 KB |
Output is correct |
26 |
Correct |
600 ms |
18268 KB |
Output is correct |
27 |
Correct |
257 ms |
18264 KB |
Output is correct |
28 |
Correct |
60 ms |
18264 KB |
Output is correct |
29 |
Correct |
231 ms |
18268 KB |
Output is correct |
30 |
Correct |
240 ms |
18268 KB |
Output is correct |
31 |
Correct |
42 ms |
18520 KB |
Output is correct |
32 |
Correct |
42 ms |
18524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
17500 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
17500 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
17500 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
17500 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
17500 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |