QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#292681#7118. Closing Timetraining4usaco0 287ms168140kbC++174.2kb2023-12-28 11:02:272024-04-28 08:13:50

Judging History

你现在查看的是最新测评结果

  • [2024-04-28 08:13:50]
  • 管理员手动重测本题所有提交记录
  • 测评结果:0
  • 用时:287ms
  • 内存:168140kb
  • [2023-12-28 11:02:28]
  • 评测
  • 测评结果:0
  • 用时:312ms
  • 内存:170616kb
  • [2023-12-28 11:02:27]
  • 提交

answer

#include <iostream>
#include <vector>
#include <algorithm>
#include <assert.h>
using namespace std;

typedef long long ll;
#define all(v) v.begin(), v.end()
#define ff first
#define ss second
const ll MAXN = 2e5 + 5;
const ll MAXM = 5e6 + 5;   // implicit segtree
const ll INF = 1e18 + 7;
const ll MAX = 2e11 + 5;

ll dist[MAXN][2];
ll a[MAXN], b[MAXN];   // 2 costs
ll ord[MAXN];
ll ans, ans2;  // no overlap, overlap
vector<pair<ll, ll>> adj[MAXN];

ll have;
ll tot = 1;
ll val[MAXM], cnt[MAXM];
ll lc[MAXM], rc[MAXM];
ll sortedDists[2 * MAXN];
vector<ll> costa, costb;

bool comp(ll i, ll j) {
    if(costb[i] == costb[j]) return costa[i] < costa[j];
    return costb[i] < costb[j];
}

inline void pull(ll u) {
    val[u] = val[lc[u]] + val[rc[u]];
    cnt[u] = cnt[lc[u]] + cnt[rc[u]];
}

void ins(ll u, ll l, ll r, ll pos, ll freq) {  // segtree indexed by val so pos = val
    if(l == r) {
        val[u] += pos * freq;
        cnt[u] += freq;
        return;
    }

    ll mid = (l + r) / 2;

    if(pos <= mid) {
        if(lc[u] == 0) lc[u] = ++tot;
        ins(lc[u], l, mid, pos, freq);
    }
    else {
        if(rc[u] == 0) rc[u] = ++tot;
        ins(rc[u], mid + 1, r, pos, freq);
    }
    pull(u);
}

int walk(ll u, ll l, ll r) {
    if(l == r) {
        ll sub;
        if(l == 0) sub = cnt[u];
        else min(cnt[u], have / l);
        have -= l * sub;
        return sub;
    }

    ll mid = (l + r) / 2;
    if(lc[u] && val[lc[u]] > have) return walk(lc[u], l, mid);

    have -= val[lc[u]];
    if(rc[u] && have) return cnt[lc[u]] + walk(rc[u], mid + 1, r);
    return cnt[lc[u]];
}

void dfs(ll u, ll p, ll type) {
    for(auto [v, w] : adj[u]) {
        if(v == p) continue;
        dist[v][type] = dist[u][type] + w;

        dfs(v, u, type);
    }
}

int max_score(int n, int x, int y, ll k, vector<int> u, vector<int> v, vector<int> w) {
    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]});
    }

    dfs(x, -1, 0); dfs(y, -1, 1);

    for(int i = 0; i < n; ++i) {
        a[i] = min(dist[i][0], dist[i][1]);
        b[i] = max(dist[i][0], dist[i][1]);
    }

    // seperate
    for(int i = 0; i < n; ++i) {
        sortedDists[2 * i] = dist[i][0];
        sortedDists[2 * i + 1] = dist[i][1];
    }
    sort(sortedDists, sortedDists + 2 * n);

    ll amnt = k;
    for(int i = 0; i < 2 * n; ++i) {
        if(amnt >= sortedDists[i]) {
            ++ans; amnt -= sortedDists[i];
        }
        else break;
    }

    // overlap
    bool flag = false;
    ll distXY = dist[y][0];    // dist between x,y

    int forced = 0;
    for(int i = 0; i < n; ++i) {
        ord[i] = i;
        if(dist[i][0] + dist[i][1] == distXY) { // on the path
            k -= a[i]; ++forced;  // forced
            costa.push_back(b[i] - a[i]); costb.push_back(INF);
            ins(1, 0, MAX, b[i] - a[i], 1);

            if(k < 0) flag = true;
        }
        else {
            costa.push_back(a[i]);
            costb.push_back(b[i]);
            ins(1, 0, MAX, a[i], 1);
        }
    }

    sort(ord, ord + n, comp);

    ll sum = 0;
    have = k;
    ans2 = forced + walk(1, 0, MAX);

    for(int i = 0; i < n - forced; ++i) {
        ins(1, 0, MAX, costa[ord[i]], -1); ins(1, 0, MAX, costb[ord[i]] - costa[ord[i]], 1);

        sum += costa[ord[i]];
        have = k - sum;
        if(have < 0) break;

        ll maxv = walk(1, 0, MAX) + i + 1;
        ans2 = max(ans2, forced + maxv);
    }

    if(flag) ans2 = 0;
    return max(ans, ans2);
}

// signed main() {
//     cout << "AHHH" << endl;
//     int n, x, y;
//     ll k; cin >> n >> x >> y >> k;
// //
//     vector<int> u(n - 1), v(n - 1), w(n - 1);
//
//     for(int i = 0; i < n - 1; ++i) cin >> u[i] >> v[i] >> w[i];
//
//     // for(int i = 0; i < n - 1; ++i) cin >> u[i];
//     // for(int i = 0; i < n - 1; ++i) cin >> v[i];
//     // for(int i = 0; i < n - 1; ++i) cin >> w[i];
//
//     cout << "doomed" << endl;
//
//     cout << "ans: " << max_score(n, x, y, k, u, v, w) << endl;
//     cout << "done" << endl;
//     return 0;
// }

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 8
Accepted
time: 275ms
memory: 143892kb

input:

cc61ad56a4797fb3f5c9529f73ce6fcedd85669b
1
200000 31011 61157 8517583098
31011 129396 964383
1655 129396 331139
1655 191487 566483
110385 191487 865248
43212 110385 542661
43212 81682 13766
81682 91774 546589
91774 124706 780638
124706 175650 118706
10421 175650 615314
10421 151953 436270
140430 151...

output:

081ce3c351cbf526b37954b9ad30f2b531a7585c
OK
451

result:

ok 

Test #2:

score: 0
Accepted
time: 287ms
memory: 168140kb

input:

cc61ad56a4797fb3f5c9529f73ce6fcedd85669b
1
200000 97133 170892 35489415917
114511 170892 730058
34783 114511 435023
34783 47301 562314
47301 162600 457629
44856 162600 365133
44856 133801 83016
117539 133801 124222
117539 128719 199821
77871 128719 703141
77871 133155 624331
7211 133155 138691
7211 ...

output:

081ce3c351cbf526b37954b9ad30f2b531a7585c
OK
650

result:

ok 

Test #3:

score: -8
Runtime Error

input:

cc61ad56a4797fb3f5c9529f73ce6fcedd85669b
200
1000 611 992 5736784
504 611 954658
504 936 219278
502 936 632439
393 502 177662
267 393 570266
267 291 941365
291 310 168052
310 765 253098
635 765 724932
274 635 842125
274 799 848645
39 799 433118
39 940 705598
553 940 564063
553 960 69665
917 960 6904...

output:


result:


Subtask #2:

score: 0
Wrong Answer

Test #4:

score: 0
Wrong Answer
time: 3ms
memory: 16104kb

input:

cc61ad56a4797fb3f5c9529f73ce6fcedd85669b
1
50 23 25 382806473
0 1 375710
1 2 898637
2 3 10402
3 4 536577
4 5 385023
5 6 71075
6 7 543368
7 8 301497
8 9 174394
9 10 711312
10 11 923006
11 12 675532
12 13 838667
13 14 565729
14 15 979816
15 16 862618
16 17 576015
17 18 177751
18 19 306989
19 20 881492...

output:

081ce3c351cbf526b37954b9ad30f2b531a7585c
OK
140

result:

wrong answer 1st lines differ - on the 1st token, expected: '96', found: '140'

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Wrong Answer

Test #36:

score: 9
Accepted
time: 0ms
memory: 16140kb

input:

cc61ad56a4797fb3f5c9529f73ce6fcedd85669b
1
4 0 1 9
0 2 2
1 2 3
2 3 3

output:

081ce3c351cbf526b37954b9ad30f2b531a7585c
OK
6

result:

ok 

Test #37:

score: -9
Wrong Answer
time: 0ms
memory: 14324kb

input:

cc61ad56a4797fb3f5c9529f73ce6fcedd85669b
1
4 0 1 8
0 2 2
1 2 3
2 3 100

output:

081ce3c351cbf526b37954b9ad30f2b531a7585c
OK
4

result:

wrong answer 1st lines differ - on the 1st token, expected: '5', found: '4'

Subtask #6:

score: 0
Skipped

Dependency #2:

0%

Subtask #7:

score: 0
Skipped

Dependency #3:

0%

Subtask #8:

score: 0
Skipped

Dependency #4:

0%

Subtask #9:

score: 0
Skipped

Dependency #1:

0%