QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#876493#9732. Gathering MushroomsyuanruiqiAC ✓88ms43804kbC++263.7kb2025-01-30 22:06:482025-01-30 22:06:49

Judging History

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

  • [2025-01-30 22:06:49]
  • 评测
  • 测评结果:AC
  • 用时:88ms
  • 内存:43804kb
  • [2025-01-30 22:06:48]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using pii = pair<int, int>;
constexpr int maxn = 200000 + 10;
int t[maxn], a[maxn], d[maxn];
vector<int> e[maxn];
int f[maxn];
i64 g[maxn];
vector<int> p[maxn];
int id[maxn], pd[maxn];
int dep[maxn], pre[maxn], len[maxn];
int lst[maxn];
int du[maxn];
int n, k;
int now, cyc;
void dfs(int u)
{
    du[dep[u]] = u;
    p[t[u]].emplace_back(dep[u]);
    if (!d[u])
    {
        if (p[t[u]].size() >= k) f[u] = du[p[t[u]][p[t[u]].size() - k]], g[u] = dep[u] - p[t[u]][p[t[u]].size() - k];
        else if (lst[t[u]])
        {
            f[u] = pre[f[lst[t[u]]]];
            g[u] = g[lst[t[u]]] - len[f[lst[t[u]]]];
            if (d[lst[t[u]]]) g[u] += (id[lst[t[u]]] - now + cyc) % cyc + dep[u];
            else g[u] += dep[u] - dep[lst[t[u]]];
        }
        else f[u] = 0, g[u] = 0x3f3f3f3f3f3f3f3f;
    }
    int x = lst[t[u]];
    lst[t[u]] = u;
    for (int v : e[u])
    {
        dep[v] = dep[u] + 1;
        dfs(v);
    }
    lst[t[u]] = x;
    p[t[u]].pop_back();
}
void dwn(int u)
{
    for (int v : e[u])
    {
        if (g[u] + 1 < g[v]) g[v] = g[u] + 1, f[v] = f[u];
        dwn(v);
    }
}
void solve()
{
    cin >> n >> k;
    for (int i=1;i<=n;++i) e[i].clear();
    for (int i=1;i<=n;++i) cin >> t[i]; 
    for (int i=1;i<=n;++i) cin >> a[i];
    memset(d, 0, sizeof(d[0]) * (n + 5));
    for (int i=1;i<=n;++i) ++d[a[i]];
    queue<int> q;
    for (int i=1;i<=n;++i) if (!d[i]) q.push(i);
    while (q.size())
    {
        int u = q.front(); q.pop();
        if (!--d[a[u]]) q.push(a[u]);
    }
    memset(f, 0, sizeof(f[0]) * (n + 5));
    for (int i=1;i<=n;++i) if (!d[i]) e[a[i]].emplace_back(i);
    for (int u=1;u<=n;++u)
        if (!f[u] && d[u])
        {
            vector<int> c;
            int v = u;
            do
            {
                c.emplace_back(v);
                v = a[v];
            } while (v != u);
            int x = 0;
            for (int v : c)
            {
                pd[v] = p[t[v]].size();
                p[t[v]].emplace_back(x);
                id[v] = x++;
            }
            for (int v : c)
            {
                int u = p[t[v]][(pd[v] + k - 1) % p[t[v]].size()];
                f[v] = c[u];
                g[v] = (i64) (k - 1) / p[t[v]].size() * x;
                g[v] += (u - id[v] + x) % x;
                u = p[t[v]][(pd[v] + p[t[v]].size() - 1) % p[t[v]].size()];
                pre[v] = c[u];
                len[v] = (id[v] - u + x) % x;
                if (!len[v]) len[v] = x;
            }
            vector<int> d = c;
            reverse(d.begin(), d.end());
            for (int v : d) lst[t[v]] = v;
            for (int v : d) p[t[v]].clear();
            cyc = x;
            for (int v : d) now = id[v], dep[v] = 0, dfs(v), lst[t[v]] = v;
            int lst = d.back();
            for (int t=0;t<2;++t) for (int v : d)
            {
                if (g[lst] + 1 < g[v]) g[v] = g[lst] + 1, f[v] = f[lst];
                lst = v;
            }
            for (int v : c) dwn(v);
            for (int v : c) ::lst[t[v]] = 0;
        }
    i64 ans = 0;
    // for (int i=1;i<=n;++i)
    // {
    //     map<int, int> mp;
    //     int u = i;
    //     for (;;)
    //     {
    //         ++mp[t[u]];
    //         if (mp[t[u]] == k) break;
    //         u = a[u];
    //     }
    //     assert(u == f[i]);
    // }
    for (int i=1;i<=n;++i) ans += (i64) i * t[f[i]];
    cout << ans << '\n';
    // for (int i=1;i<=n;++i) cout << t[f[i]] << ' ';
    // cout << '\n';
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) solve();
    return 0;
}

这程序好像有点Bug,我给组数据试试?

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 14064kb

input:

3
5 3
2 2 1 3 3
2 5 1 2 4
5 4
2 2 1 3 3
2 5 1 2 4
3 10
1 2 3
1 3 2

output:

41
45
14

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 18ms
memory: 9804kb

input:

6000
19 48
18 19 18 19 11 9 15 19 12 18 11 18 9 18 9 18 19 11 15
12 14 18 8 1 3 19 5 13 14 15 2 14 5 19 2 19 12 9
15 23
3 1 1 3 6 1 4 1 1 6 6 4 12 4 6
14 1 8 8 6 6 12 14 6 8 5 7 14 2 5
9 140979583
4 5 8 9 2 7 6 8 2
8 9 4 6 9 2 4 7 8
4 976357580
2 3 1 3
2 1 1 4
6 508962809
4 3 4 3 4 4
4 5 4 5 5 6
13 ...

output:

3420
260
254
26
84
759
126
30
1092
1
2493
2422
168
360
298
324
2424
2520
220
228
1107
9
3486
1
796
81
340
272
600
3196
32
495
40
128
140
665
1635
702
68
96
90
288
29
588
16
234
445
2928
140
40
477
1197
19
1994
1082
32
522
672
20
390
32
2204
1938
42
21
885
4
1539
196
420
11
1709
801
720
1
556
40
17
2...

result:

ok 6000 lines

Test #3:

score: 0
Accepted
time: 53ms
memory: 26448kb

input:

1
200000 40000
46988 88148 28442 9596 17281 27561 58024 1062 138912 175273 194682 27958 11240 187099 28869 177531 154933 83035 11300 178646 6952 44234 168671 169483 187602 178519 99885 98196 64731 100802 16974 85402 50616 126862 159025 116795 83016 127770 3067 56860 19833 64583 11977 100045 198272 1...

output:

2654974404037027

result:

ok single line: '2654974404037027'

Test #4:

score: 0
Accepted
time: 53ms
memory: 26556kb

input:

1
200000 10000000
172532 195148 85227 155659 14990 35802 156340 83797 197030 21580 88873 28715 24950 12770 82745 113383 51071 21570 147715 85175 15394 148817 70297 38951 36754 55240 181122 186649 17533 4230 68223 4978 154960 144826 26781 185779 18292 195221 34441 37597 148301 81791 67645 197829 3693...

output:

1991871314556006

result:

ok single line: '1991871314556006'

Test #5:

score: 0
Accepted
time: 52ms
memory: 26852kb

input:

1
200000 1000000000
176004 145470 198726 37315 113426 105334 46063 63520 103219 121771 57158 185334 126226 141718 133227 158448 119801 62952 10829 175859 190290 80534 82892 42340 164975 79666 102327 14872 75277 57094 37650 183007 86587 47862 133654 48675 26581 199005 14582 75902 5988 102670 169993 1...

output:

2910045863489510

result:

ok single line: '2910045863489510'

Test #6:

score: 0
Accepted
time: 54ms
memory: 23096kb

input:

1
200000 40000
118673 82089 118673 118673 129221 118673 125539 106895 82089 129221 125539 118673 82089 129221 118673 125539 106895 106895 129221 118673 129221 82089 82089 125539 129221 129221 118673 106895 82089 129221 106895 82089 106895 82089 106895 106895 129221 118673 106895 82089 106895 82089 8...

output:

2404687852449892

result:

ok single line: '2404687852449892'

Test #7:

score: 0
Accepted
time: 52ms
memory: 23168kb

input:

1
200000 10000000
110770 103054 103054 67926 87448 110770 103054 67926 87448 87448 87448 87448 176259 67926 176259 110770 103054 110770 176259 67926 67926 67926 87448 87448 87448 67926 110770 67926 67926 67926 176259 87448 87448 176259 87448 103054 110770 176259 103054 87448 87448 176259 103054 1107...

output:

1871051161222455

result:

ok single line: '1871051161222455'

Test #8:

score: 0
Accepted
time: 54ms
memory: 23296kb

input:

1
200000 1000000000
92387 35422 92387 192988 35422 35422 92387 192988 35422 192988 192988 124898 35422 124898 124898 92387 35422 192988 124898 35422 132532 92387 132532 192988 35422 92387 124898 132532 35422 92387 35422 35422 124898 132532 92387 92387 192988 192988 124898 92387 92387 35422 124898 19...

output:

2226090142242870

result:

ok single line: '2226090142242870'

Test #9:

score: 0
Accepted
time: 69ms
memory: 30540kb

input:

1
200000 40000
116752 38573 63445 128225 135901 46112 158662 189088 148798 191324 45035 154544 80127 91833 149943 48432 92060 43958 137795 77575 36781 45299 178350 100696 60034 61055 85213 69563 78385 21262 146118 102661 187678 130733 9787 16393 117846 185565 60069 3846 141624 104285 39616 57159 868...

output:

1143189663913379

result:

ok single line: '1143189663913379'

Test #10:

score: 0
Accepted
time: 62ms
memory: 31128kb

input:

1
200000 10000000
140527 170359 145004 37893 122429 127925 3461 95526 78195 26127 92887 141377 182681 136498 39270 58777 71455 38154 157727 24834 179001 155490 60975 147064 75506 36710 46141 189903 21088 196674 125247 103266 25995 4763 41038 180967 4283 21509 10323 142860 120149 127930 146726 109420...

output:

677866460957128

result:

ok single line: '677866460957128'

Test #11:

score: 0
Accepted
time: 68ms
memory: 30996kb

input:

1
200000 1000000000
127458 93467 147620 82303 77297 18842 171477 180789 73198 66451 114690 7452 145760 97912 182102 47863 91886 284 142563 117252 129365 135867 136635 64262 196952 183491 187881 182341 82756 135693 126974 12627 194511 30468 29983 30464 28044 129494 64477 122463 141768 103686 105366 2...

output:

1985383835721543

result:

ok single line: '1985383835721543'

Test #12:

score: 0
Accepted
time: 31ms
memory: 18780kb

input:

1
200000 40000
131243 50411 183082 141585 81827 174160 175932 74477 31310 20599 29372 136846 36578 133487 134440 147839 115280 45462 77063 5065 29725 137589 80437 170073 105648 191314 31432 157802 148931 184650 177611 69456 152253 158494 111351 10329 108646 119661 65317 177563 88191 60803 135866 555...

output:

1940771592135540

result:

ok single line: '1940771592135540'

Test #13:

score: 0
Accepted
time: 33ms
memory: 22344kb

input:

1
200000 10000000
152041 174470 165831 197804 141842 124891 172029 99878 29839 80130 188275 172670 60080 40554 177721 183620 68381 153260 43684 146559 5617 163940 5828 28239 152505 98252 150314 82119 197777 166408 147215 105141 138523 31398 31463 145584 147517 170652 189441 191295 120537 44537 57915...

output:

1651721014252886

result:

ok single line: '1651721014252886'

Test #14:

score: 0
Accepted
time: 26ms
memory: 15996kb

input:

1
200000 1000000000
113494 96820 113494 96820 113494 186155 113494 72596 113494 96820 96820 72596 72596 72596 113494 186155 72596 72596 72596 186155 140986 96820 113494 72596 186155 186155 140986 96820 186155 140986 113494 186155 186155 186155 96820 140986 72596 140986 140986 96820 72596 96820 96820...

output:

1451927259600000

result:

ok single line: '1451927259600000'

Test #15:

score: 0
Accepted
time: 69ms
memory: 31968kb

input:

1
200000 40000
4081 16238 11969 107578 198622 110168 135045 77728 131657 124891 24584 12007 91833 83365 54207 177065 31062 5567 193813 13669 194769 184436 37796 38153 154996 103928 4152 167404 164454 179449 1292 90714 631 188401 121030 170338 82614 92709 101075 197243 118713 156023 89703 132201 1498...

output:

551622758100000

result:

ok single line: '551622758100000'

Test #16:

score: 0
Accepted
time: 65ms
memory: 31544kb

input:

1
200000 10000000
118036 3793 5512 165907 172930 121912 186134 148346 83443 148336 65861 79032 153603 834 89892 55452 62702 146776 16844 122756 146483 134711 163729 88863 155117 48251 152885 125608 16053 13493 24658 118369 116062 126154 145675 46280 108793 91814 162570 11989 193985 96015 104182 6961...

output:

631963159800000

result:

ok single line: '631963159800000'

Test #17:

score: 0
Accepted
time: 66ms
memory: 31528kb

input:

1
200000 1000000000
33836 100196 59375 75273 128299 104538 94912 26541 50585 134718 98137 40094 123637 167966 149636 26105 22668 121583 92264 94615 56134 10763 140098 100436 16529 60575 111368 27690 73533 193629 180594 81747 109487 21125 29088 194323 135905 95040 18540 81652 176196 50575 186436 1418...

output:

1518987594900000

result:

ok single line: '1518987594900000'

Test #18:

score: 0
Accepted
time: 55ms
memory: 29880kb

input:

1
200000 40000
81104 81104 124637 80107 79894 80107 79894 124637 79894 81104 80107 108634 124637 80107 80107 124637 124637 81104 80107 80107 80107 80107 108634 79894 124637 79894 124637 124637 80107 79894 108634 124637 108634 79894 108634 124637 81104 80107 79894 80107 81104 124637 80107 80107 81104...

output:

2492752463700000

result:

ok single line: '2492752463700000'

Test #19:

score: 0
Accepted
time: 50ms
memory: 28704kb

input:

1
200000 10000000
113380 113380 6166 113380 73481 196022 142288 113380 142288 113380 196022 113380 73481 73481 142288 6166 6166 142288 142288 6166 6166 6166 6166 73481 73481 113380 196022 73481 196022 73481 73481 196022 113380 113380 6166 6166 196022 6166 6166 73481 113380 6166 113380 113380 73481 6...

output:

3920459602200000

result:

ok single line: '3920459602200000'

Test #20:

score: 0
Accepted
time: 58ms
memory: 31756kb

input:

1
200000 1000000000
55030 139062 93934 139062 139062 188123 93934 93934 55030 93934 93934 93934 188123 139062 55030 79446 79446 93934 93934 79446 93934 79446 188123 188123 55030 79446 55030 139062 93934 79446 188123 79446 93934 188123 139062 188123 188123 93934 55030 93934 55030 188123 93934 79446 5...

output:

1100605503000000

result:

ok single line: '1100605503000000'

Test #21:

score: 0
Accepted
time: 84ms
memory: 43804kb

input:

1
200000 40000
58977 6108 192639 180048 171812 123648 62338 77818 104884 77820 6656 90023 106001 100891 74036 166651 44552 176104 164299 22650 142254 62112 94221 64472 87477 117679 67351 88407 19219 20120 131885 182802 182606 76083 37465 44793 124801 101708 21282 72200 190182 51075 25878 8311 13717 ...

output:

2574846010902855

result:

ok single line: '2574846010902855'

Test #22:

score: 0
Accepted
time: 69ms
memory: 33956kb

input:

1
200000 10000000
32788 36106 63672 13633 133399 118569 177557 164892 70843 121881 44540 191592 148535 126625 128051 88393 11348 191253 59176 1824 105567 87004 192147 103542 76045 169545 147713 110812 53394 193545 30238 132934 71048 87462 48364 120934 93056 95812 50355 38222 11588 87032 123090 10803...

output:

2729241306046102

result:

ok single line: '2729241306046102'

Test #23:

score: 0
Accepted
time: 88ms
memory: 40308kb

input:

1
200000 1000000000
131865 21550 68612 79003 122691 149098 36210 58257 146491 195940 148723 135722 78009 36153 173518 139189 147549 29416 116017 31887 130076 74555 168769 146610 170223 18391 151390 123127 106284 103512 44593 31031 126874 171878 21855 119249 153019 42452 116328 199508 151523 139591 1...

output:

1666108330500000

result:

ok single line: '1666108330500000'

Test #24:

score: 0
Accepted
time: 29ms
memory: 18768kb

input:

1
200000 40000
136112 186291 100280 154449 159476 81489 98318 122844 76856 173595 31743 87091 54046 10622 107038 56139 99877 20376 189082 88641 13927 189464 183573 49637 103012 125344 52481 40353 3113 6858 76911 66861 180277 196605 151580 155980 135714 39503 93410 20548 126999 124971 82085 59247 111...

output:

2441525425533106

result:

ok single line: '2441525425533106'

Test #25:

score: 0
Accepted
time: 36ms
memory: 19160kb

input:

1
200000 10000000
162806 129138 143698 143684 52876 16339 82915 48852 115812 118744 45914 176901 77564 68400 135411 163307 10299 105681 3950 159948 77979 167276 3657 49709 185308 97441 79567 107905 179443 138462 1243 31895 156534 38122 171958 51476 17616 90813 28861 54137 63981 33759 189645 82105 19...

output:

2144464394695140

result:

ok single line: '2144464394695140'

Test #26:

score: 0
Accepted
time: 29ms
memory: 19364kb

input:

1
200000 1000000000
78833 145328 38486 129536 60040 9191 30204 80118 112190 47996 93256 178969 179815 100815 132643 100687 150108 137864 37775 26735 138430 169640 72378 29061 17640 29159 190247 113685 153946 121291 196453 35286 199171 88414 14895 84356 188662 83162 13022 112994 121011 131189 27284 1...

output:

3315416577000000

result:

ok single line: '3315416577000000'

Test #27:

score: 0
Accepted
time: 25ms
memory: 15692kb

input:

1
200000 40000
171393 130093 24874 162947 91299 171393 91299 24874 91299 91299 171393 24874 171393 130093 130093 130093 130093 130093 91299 91299 24874 162947 171393 162947 91299 91299 171393 91299 171393 24874 162947 91299 24874 130093 171393 162947 162947 171393 162947 24874 91299 162947 24874 912...

output:

2601873009300000

result:

ok single line: '2601873009300000'

Test #28:

score: 0
Accepted
time: 27ms
memory: 14044kb

input:

1
200000 10000000
192275 54817 112201 192275 54817 112201 173719 173719 96044 54817 96044 192275 54817 173719 192275 96044 173719 112201 54817 112201 112201 173719 54817 173719 192275 192275 192275 192275 96044 112201 173719 173719 112201 173719 96044 173719 173719 54817 192275 192275 112201 96044 1...

output:

3474397371900000

result:

ok single line: '3474397371900000'

Test #29:

score: 0
Accepted
time: 28ms
memory: 14148kb

input:

1
200000 1000000000
170945 122563 21753 21753 122563 21753 21753 170945 21753 170945 4558 122563 76598 21753 4558 122563 4558 21753 4558 122563 170945 21753 122563 4558 4558 170945 76598 170945 21753 76598 76598 170945 21753 170945 170945 76598 122563 4558 4558 76598 21753 21753 170945 76598 170945 ...

output:

91160455800000

result:

ok single line: '91160455800000'

Test #30:

score: 0
Accepted
time: 37ms
memory: 22264kb

input:

1
200000 40000
57272 148259 103913 35535 50615 42658 98719 181844 114732 939 195461 181376 140503 163961 70721 88757 84109 32701 174170 115475 34835 110046 125377 93603 71528 195519 112461 176353 51147 9256 162324 87296 23871 58118 63695 162693 31707 187748 178014 63229 103282 156388 81694 199276 15...

output:

3133634911846493

result:

ok single line: '3133634911846493'

Test #31:

score: 0
Accepted
time: 40ms
memory: 22436kb

input:

1
200000 10000000
112500 90246 62921 195911 136745 199776 44489 40804 99619 17241 119733 56381 139049 166317 40861 78167 49111 156547 114170 109702 101822 1745 174847 17521 53424 18555 11372 93868 115988 64517 42497 76581 2809 66033 78528 37839 154393 188229 79261 192324 31000 20862 147336 153027 12...

output:

3651098255400000

result:

ok single line: '3651098255400000'

Test #32:

score: 0
Accepted
time: 40ms
memory: 22476kb

input:

1
200000 1000000000
132700 119807 185544 154491 117888 74807 94376 195743 82179 66540 129245 122016 35981 99582 175418 194522 149003 154339 9846 20288 25216 101070 144 3633 143873 43429 17472 168735 139908 3295 171802 117288 142673 135841 37402 140757 88637 68374 76847 150625 188354 11779 157491 101...

output:

1245466695260976

result:

ok single line: '1245466695260976'

Test #33:

score: 0
Accepted
time: 26ms
memory: 29508kb

input:

1
200000 1000000000
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...

output:

36271232538

result:

ok single line: '36271232538'

Test #34:

score: 0
Accepted
time: 31ms
memory: 21668kb

input:

101
804 569255960
501 134 501 605 605 501 134 605 605 501 605 501 134 605 605 134 407 501 407 501 605 407 134 134 605 605 134 501 501 134 407 501 501 407 134 501 605 134 134 134 501 134 407 605 605 501 501 407 134 501 501 407 501 407 134 134 605 605 605 407 407 605 134 605 605 605 134 407 605 501 13...

output:

185188538
146878847
245029219
352163694
185664666
94118714
415029105
144072128
190478600
164704080
51196302
300909977
225338145
126775054
155573423
200853919
166334503
216122690
183227794
68795056
189590304
37198542
261504691
115368648
93078486
111333313
287317913
126177627
261544569
168326353
22496...

result:

ok 101 lines

Extra Test:

score: 0
Extra Test Passed