QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#291057#2863. 苹果树MoRanSky100 ✓3186ms398952kbC++233.2kb2023-12-26 04:39:542023-12-26 04:39:54

Judging History

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

  • [2023-12-26 04:39:54]
  • 评测
  • 测评结果:100
  • 用时:3186ms
  • 内存:398952kb
  • [2023-12-26 04:39:54]
  • 提交

answer

// Skyqwq
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

typedef long long LL;
typedef pair<int, int> PII;

template <typename T> void inline read(T &x) {
    x = 0; int f = 1; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') { f = -1; } s = getchar(); }
    while (s >= '0' && s <= '9') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

template <typename T> bool inline chkMin(T &x, T y) { return y < x ? x = y, 1 : 0; }
template <typename T> bool inline chkMax(T &x, T y) { return y > x ? x = y, 1 : 0; }

const int N = 4e4 + 5, INF = -1e9, S = 100000005, M = 5e5 + 5;

vector<int> g[N];

int n, m, fa[N], a[N], w[N], pre[N], dfn[N], dfncnt, sz[N], s[N], idx, loc[N], vp[N], dy[N], pool[S];

int Z;

int& F(int x, int y) {
    if (!y) return Z;
    if (x > idx) return Z;
    return pool[(x - 1) * m + y];
}

int& H(int x, int y) {
    if (!y) return Z;
    if (x > idx) return Z;
    return pool[(x - 1) * m + y + idx * m];
}

void inline clr() {
    for (int i = 0; i <= idx + 1; i++) {
        g[i].clear();
        dfn[i] = pre[i] = sz[i] = 0;
    }
    dfncnt = idx = 0;
}

void dfs(int u) {
    dfn[u] = ++dfncnt, pre[dfn[u]] = u; sz[u] = 1;
    for (int v: g[u]) {
        dfs(v);
        sz[u] += sz[v];
    }
}

int q[M];

void upd(int z, int t, int w) {
    int hh = 0, tt = 0;
    q[0] = 0;
    for (int i = 1; i <= m; i++) {
        while (hh <= tt && q[hh] < i - t) hh++;
        if (hh <= tt) {
            int j = q[hh];
            chkMax(F(z, i), F(z + 1, j) + (i - j) * w);
        }
        while (hh <= tt && F(z + 1, q[tt]) - q[tt] * w < F(z + 1, i) - i * w) tt--;
        q[++tt] = i;
    }
}

void inline sv() {
    for (int i = idx; i; i--) {
        int u = pre[i], v = i + sz[u];
        int W = min(a[u], m);
        upd(i, W, w[u]);
        for (int j = 0; j <= m; j++) chkMax(F(i, j), F(v, j));
    }
}

void wk() {
    dfs(1);
    for (int i = 1; i <= idx + 1; i++) for (int j = 0; j <= m; j++) F(i, j) = 0;
    sv(); 
    for (int i = 1; i <= n; i++) loc[i] = dfn[i];
    dfncnt = 0;
    for (int i = 1; i <= idx + 1; i++) reverse(g[i].begin(), g[i].end());
    dfs(1);
    for (int i = 1; i <= idx + 1; i++) for (int j = 0; j <= m; j++) H(i, j) = F(i, j), F(i, j) = 0;
    sv();
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            if (vp[i]) assert(dy[i] == pre[dfn[i] + sz[i] - vp[i]]);
            int w = H(loc[i] + sz[i], j) + F(dfn[i] + sz[i] - vp[i], m - j) + s[i];
            chkMax(ans, w);
        }
    }
    printf("%d\n", ans);
}

int main() {
    int T; read(T);
    while (T--) {
        read(n), read(m); idx = n;
        for (int i = 1; i <= n; i++) {
            read(fa[i]), read(a[i]), read(w[i]), s[i] = s[fa[i]] + w[i];
            g[fa[i]].pb(i);
            vp[i] = 0; dy[i] = 0;
            if (a[i] > 1) {
                vp[i] = 1;
                ++idx;
                a[idx] = a[i] - 1;
                w[idx] = w[i], fa[idx] = i;
                g[i].pb(idx);
                dy[i] = idx;
                a[i] = 1;
            }
        }
        wk(); clr();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 10
Accepted
time: 340ms
memory: 56156kb

input:

5
10 300000
0 214224 4
1 300000 75
1 291002 29
1 300000 64
1 300000 49
1 233141 41
1 300000 64
1 141084 99
1 168700 82
1 300000 73
30 100000
0 15818 36
1 63903 41
1 38513 14
1 26382 53
1 42336 90
1 45105 52
1 17960 27
1 18440 75
1 64777 36
1 40886 78
1 33546 97
1 7257 40
1 15815 10
1 37789 74
1 4736...

output:

26998514
9400115
5790773
2919180
1954284

result:

ok 5 lines

Test #2:

score: 10
Accepted
time: 2828ms
memory: 398372kb

input:

5
50 500000
0 77942 22
1 11028 23
1 193686 83
1 76437 29
1 121911 52
1 54169 22
1 167531 41
1 29174 33
1 120984 76
1 160709 30
1 145226 79
1 97062 24
1 62309 94
1 35981 81
1 56221 88
1 150504 22
1 180908 11
1 35384 74
1 13335 34
1 186149 39
1 115543 62
1 138007 81
1 32006 17
1 53886 92
1 122046 69
1...

output:

44321956
22983265
12049557
7843031
6002642

result:

ok 5 lines

Test #3:

score: 10
Accepted
time: 2832ms
memory: 398924kb

input:

5
50 500000
0 157500 63
1 91677 96
1 189652 32
1 178849 20
1 30234 34
1 17346 27
1 45441 20
1 57313 49
1 15769 93
1 67844 9
1 40700 22
1 95201 10
1 38969 34
1 136816 6
1 177243 93
1 49217 41
1 119423 50
1 151926 61
1 171427 17
1 35436 42
1 93072 45
1 63009 11
1 44853 6
1 137123 3
1 29907 18
1 24369 ...

output:

47643513
24293856
12064303
8001885
6039377

result:

ok 5 lines

Test #4:

score: 10
Accepted
time: 3171ms
memory: 395596kb

input:

5
10000 2500
0 13 1
1 57 1
2 2 1
1 46 1
4 17 1
2 6 1
6 16 1
7 55 1
5 19 1
1 52 1
7 21 1
7 5 1
1 8 1
7 11 1
3 12 1
6 58 1
7 44 1
8 2 1
3 33 1
12 40 1
4 21 1
3 29 1
9 54 1
1 15 1
21 51 1
20 31 1
26 52 1
24 6 1
6 7 1
11 37 1
11 13 1
18 5 1
18 56 1
18 57 1
22 28 1
15 11 1
36 9 1
17 23 1
8 14 1
10 36 1
1...

output:

2520
1686
1409
1271
1273

result:

ok 5 lines

Test #5:

score: 10
Accepted
time: 3186ms
memory: 395616kb

input:

5
10000 2500
0 20 1
1 60 1
2 60 1
2 22 1
3 7 1
5 53 1
2 11 1
5 56 1
7 60 1
4 54 1
6 43 1
10 8 1
7 14 1
3 33 1
8 22 1
15 16 1
16 51 1
3 17 1
6 58 1
19 60 1
8 25 1
1 46 1
3 10 1
14 4 1
15 9 1
1 15 1
23 57 1
7 33 1
23 12 1
16 40 1
22 12 1
20 16 1
32 12 1
21 59 1
25 48 1
24 8 1
12 30 1
29 6 1
12 28 1
20...

output:

2522
1686
1410
1272
1272

result:

ok 5 lines

Test #6:

score: 10
Accepted
time: 329ms
memory: 55920kb

input:

5
10 300000
0 300000 29
1 300000 41
1 263990 6
2 300000 72
4 252904 71
5 300000 63
6 300000 72
7 300000 44
8 276586 51
5 300000 82
30 100000
0 7050 10
1 39332 30
1 36411 25
1 26514 87
1 19127 82
3 44321 29
3 20699 11
2 66113 30
3 16336 37
7 19195 60
2 46443 84
10 1054 10
4 51491 24
8 16597 70
8 2862...

output:

24600443
9606144
5526246
2935257
1794967

result:

ok 5 lines

Test #7:

score: 10
Accepted
time: 335ms
memory: 55752kb

input:

5
10 300000
0 47560 46
1 62149 33
2 216458 81
2 300000 61
4 300000 57
1 129855 9
2 300000 42
7 300000 19
1 300000 92
9 296263 18
30 100000
0 61117 1
1 45799 63
1 9649 45
1 18116 72
1 18387 42
1 44605 95
6 25171 78
1 4219 40
3 6788 36
4 41762 21
4 27012 68
1 59556 61
2 32623 82
2 16806 53
1 14628 27
...

output:

27600197
9379029
5934237
2943055
1888042

result:

ok 5 lines

Test #8:

score: 10
Accepted
time: 2843ms
memory: 398400kb

input:

5
50 500000
0 157523 65
1 154464 1
2 167139 39
1 163059 83
3 79984 85
3 56952 87
1 107593 67
7 42791 72
2 75424 31
5 16785 37
9 133227 93
4 54407 59
2 64038 50
11 138884 18
8 156062 55
4 97058 19
8 26722 27
12 186040 55
10 106293 48
9 40651 79
16 144301 66
15 8049 54
20 49439 13
6 180909 27
15 11123...

output:

47012299
23497335
12055969
7944961
5886821

result:

ok 5 lines

Test #9:

score: 10
Accepted
time: 2827ms
memory: 398952kb

input:

5
50 500000
0 64746 92
1 64860 89
2 191375 87
1 99632 66
3 121105 34
4 128842 65
1 87763 39
4 1005 5
7 186742 47
4 55933 41
9 71754 71
6 97186 71
9 10091 31
1 93751 80
11 194462 42
11 5584 33
11 175327 16
14 31413 11
6 98214 62
3 87185 36
17 183983 94
12 165652 93
12 44836 16
7 36713 74
9 86703 50
2...

output:

46473418
22923915
11659557
7954419
6010012

result:

ok 5 lines

Test #10:

score: 10
Accepted
time: 2850ms
memory: 398892kb

input:

5
50 500000
0 58709 60
1 147644 31
2 128698 51
2 112654 68
4 161392 84
2 113912 68
5 135166 37
1 20645 61
5 24834 79
8 61607 99
9 184175 16
8 169087 88
11 130486 30
13 21745 79
5 75130 87
14 176165 45
7 19827 24
9 21255 31
14 76195 87
4 64564 27
7 113384 16
16 106899 78
21 185971 67
11 198997 75
7 3...

output:

47020545
24130352
11964592
7824132
5890167

result:

ok 5 lines