QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#864470#6969. 幻想乡战略游戏msk_sama5 116ms26948kbC++203.0kb2025-01-20 17:12:312025-01-20 17:12:32

Judging History

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

  • [2025-01-20 17:12:32]
  • 评测
  • 测评结果:5
  • 用时:116ms
  • 内存:26948kb
  • [2025-01-20 17:12:31]
  • 提交

answer

#include <set>
#include <map>
#include <queue>
#include <ctime>
#include <cstdio>
#include <vector>
#include <cassert>
#include <cstring>
#include <algorithm>
#define fi first
#define se second
#define ep emplace
#define MISAKA main
#define ll long long
#define eb emplace_back
#define pii pair<int,int>
#define rg(x) x.begin(),x.end()
#define pc(x) __builtin_popcount(x)
#define mems(a,x) memset(a,x,sizeof(a))
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define _rep(i,a,b) for(int i=(a);i>=(b);--i)
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define FIO(FILE) freopen(FILE".in","r",stdin),freopen(FILE".out","w",stdout)
using namespace std;
bool __st;
inline int read(){
    char c=getchar();int f=1,x=0;
    for(;c<48||c>57;c=getchar())if(c=='-') f=-1;
    for(;47<c&&c<58;c=getchar()) x=(x<<3)+(x<<1)+(c^48);
    return x*f;
}
const int N=1e5+10,mod=998244353;
int n,q,rt,fa[N],vis[N],son[N],sz[N],top[N],dfn[N],tim;ll d[N],a[N],s1,s2;
vector<pii> g[N];vector<int> G[N];
void dfs1(int u,int f){
    fa[u]=f,sz[u]=1;
    for(auto [v,w]:g[u])if(v!=f){
        d[v]=d[u]+w,dfs1(v,u),sz[u]+=sz[v];
        if(sz[v]>sz[son[u]]) son[u]=v;
    }
}
void dfs2(int u,int tp){
    dfn[u]=++tim,top[u]=tp;if(son[u]) dfs2(son[u],tp);
    for(auto [v,w]:g[u])if(v!=fa[u]&&v!=son[u]) dfs2(v,v);
}
#define go(u) for(auto [v,w]:g[u])if(v!=f&&!vis[v]) 
void getsz(int u,int f){sz[u]=1;go(u)getsz(v,u),sz[u]+=sz[v];}
void find(int u,int f,int n){
    int mx=n-sz[u];
    go(u) find(v,u,n),mx=max(mx,sz[v]);
    if(mx<=n/2) rt=u;
}
int bd(int u){
    getsz(u,0);find(u,0,sz[u]);int f=-1;
    vis[u=rt]=1;go(u) G[u].eb(bd(v));return u;
}
struct bit{
    ll c[N];bit(){mems(c,0);}
    void upd(int x,ll k){for(;x<=n;x+=x&-x)c[x]+=k;}
    ll qry(int x){ll r=0;for(;x;x^=x&-x)r+=c[x];return r;}
    ll qry(int l,int r){return qry(r)-qry(l-1);}    
}t1,t2;
void upd(int u,ll k){
    t1.upd(dfn[u],k);s1+=k,s2+=k*d[u];
    for(;u;u=fa[top[u]]) t2.upd(dfn[u],k*d[u]),a[u]+=k*d[u];
}
ll qry(int u){
    ll s=d[u]*t1.qry(dfn[u],dfn[u]+sz[u]-1);int _=u;
    for(;u;u=fa[top[u]]){
        s+=t2.qry(dfn[top[u]],dfn[u]-1);
        int x=top[u],y=fa[x],z=son[y];
        if(!y) break;
        s+=a[y]+d[y]*(-t1.qry(dfn[x],dfn[x]+sz[x]-1)+t1.qry(dfn[z],dfn[z]+sz[z]-1));
    }
    return s1*d[_]+s2-2ll*s;
}
ll solve(int u,ll x){
    if(G[u].empty()) return x;
    int id=-1;ll p=x;
    for(int v:G[u]){
        ll t=qry(v);
        if(t<p) id=v,p=t;
    }
    return id==-1?x:solve(id,p);
}
void misaka(){
    n=read(),q=read();
    rep(i,2,n){
        int u=read(),v=read(),w=read();
        g[u].eb(v,w);g[v].eb(u,w);
    }
    rt=bd(1);dfs1(1,0);dfs2(1,1);
    while(q--){
        int u=read(),k=read();
        upd(u,k);
        printf("%lld\n",solve(rt,qry(rt)));
    }
}
bool __ed;
signed MISAKA(){
    #ifdef LOCAL_MSK
    atexit([](){
    debug("\n%.3lfs  ",(double)clock()/CLOCKS_PER_SEC);
    debug("%.3lfMB\n",abs(&__st-&__ed)/1024./1024);});
    #endif
    
    int T=1;
    while(T--) misaka();
    return 0;
}

詳細信息


Pretests


Final Tests

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 9424kb

input:

5000 2000
1 2 701
2 3 811
3 4 548
4 5 703
5 6 32
6 7 435
7 8 368
8 9 978
9 10 68
10 11 550
11 12 667
12 13 270
13 14 486
14 15 217
15 16 486
16 17 326
17 18 418
18 19 361
19 20 344
20 21 116
21 22 373
22 23 38
23 24 563
24 25 591
25 26 800
26 27 94
27 28 210
28 29 548
29 30 278
30 31 169
31 32 1000
...

output:

10752
7430217
14064585
85513660
86402508
734481369
740491407
838570968
1067135161
1275552241
1496436865
1718155834
1790533824
1962430993
2349404883
2449941655
2607510847
2577175268
2849529711
2877352231
2885278221
3010486605
3017429085
3193336323
3265441525
3359362606
3685811046
3869008096
394722912...

result:

wrong answer 1st numbers differ - expected: '0', found: '10752'

Test #2:

score: 0
Wrong Answer
time: 1ms
memory: 10916kb

input:

5000 2000
1 2 576
2 3 745
3 4 672
4 5 812
5 6 770
6 7 261
7 8 191
8 9 534
9 10 209
10 11 576
11 12 184
12 13 212
13 14 996
14 15 466
15 16 360
16 17 127
17 18 180
18 19 804
19 20 74
20 21 504
21 22 912
22 23 253
23 24 113
24 25 418
25 26 723
26 27 596
27 28 92
28 29 845
29 30 215
30 31 96
31 32 224
...

output:

48970443
99284091
188195760
190476192
293820148
322221052
602701560
605991335
978624595
1047474305
1117393759
1410168109
1436203021
1447155738
1447296813
1738820727
1743464899
2069047987
2162511133
2171043516
2171301741
2308220877
2318976013
2348883373
2385728483
2501410253
2759075789
2826583481
295...

result:

wrong answer 1st numbers differ - expected: '0', found: '48970443'

Test #3:

score: 0
Wrong Answer
time: 1ms
memory: 10012kb

input:

5000 2000
1 2 149
2 3 122
3 4 778
4 5 764
5 6 514
6 7 905
7 8 289
8 9 151
9 10 976
10 11 315
11 12 429
12 13 517
13 14 488
14 15 679
15 16 354
16 17 597
17 18 583
18 19 378
19 20 128
20 21 990
21 22 123
22 23 893
23 24 859
24 25 140
25 26 188
26 27 142
27 28 620
28 29 735
29 30 662
30 31 261
31 32 9...

output:

0
6791367
36080264
436491888
456651963
676773171
676869672
718926782
881556282
882316998
1029354851
1106602225
1116209688
1144167618
1640231262
1665616475
1982022540
2086981476
2091688464
2189892852
2194162956
2268206226
2456085078
2588789160
2594014024
2614652903
2687217675
2766759795
2794248332
27...

result:

wrong answer 2nd numbers differ - expected: '6320496', found: '6791367'

Test #4:

score: 0
Wrong Answer
time: 75ms
memory: 26828kb

input:

100000 100000
1 2 31
2 3 400
3 4 488
4 5 524
5 6 665
6 7 63
7 8 381
8 9 375
9 10 936
10 11 157
11 12 657
12 13 346
13 14 408
14 15 288
15 16 138
16 17 785
17 18 15
18 19 109
19 20 319
20 21 659
21 22 773
22 23 703
23 24 995
24 25 974
25 26 159
26 27 163
27 28 468
28 29 915
29 30 245
30 31 634
31 32 ...

output:

2578560
6537843850
22263441753
26281474205
35948586725
41376387695
53413640855
70041831459
72888863907
74091590354
77913715139
93395124361
100693273031
101897488303
103840745101
105202379326
113299573740
124627913378
126456708254
129151682927
130365244453
140132527380
143772468231
148605639756
16200...

result:

wrong answer 1st numbers differ - expected: '0', found: '2578560'

Test #5:

score: 0
Wrong Answer
time: 72ms
memory: 26948kb

input:

100000 100000
1 2 460
2 3 195
3 4 548
4 5 573
5 6 835
6 7 147
7 8 107
8 9 619
9 10 881
10 11 982
11 12 185
12 13 152
13 14 98
14 15 153
15 16 14
16 17 925
17 18 651
18 19 226
19 20 175
20 21 968
21 22 25
22 23 695
23 24 597
24 25 353
25 26 803
26 27 958
27 28 598
28 29 535
29 30 330
30 31 743
31 32 ...

output:

32025684
6869202150
8590033189
20942263067
41894090407
45275929878
49497514086
52877742168
57814378452
61555908042
78327457164
93840847028
99828446390
99934842020
101013101249
109345169084
117876864105
134615753506
140163060898
142867046570
148940871974
157062564004
161088296740
169713953977
1703137...

result:

wrong answer 1st numbers differ - expected: '0', found: '32025684'

Test #6:

score: 0
Wrong Answer
time: 116ms
memory: 19008kb

input:

100000 100000
1 2 121
2 3 761
1 4 427
1 5 833
1 6 28
6 7 336
5 8 977
8 9 347
1 10 124
3 11 13
10 12 525
4 13 407
7 14 546
3 15 279
2 16 707
11 17 835
13 18 300
17 19 138
6 20 763
3 21 607
17 22 118
22 23 192
7 24 865
21 25 86
23 26 98
10 27 691
25 28 643
20 29 285
10 30 526
24 31 194
8 32 227
31 33 ...

output:

0
2941172
11343892
12937046
17879886
21202062
24568854
28456406
31274942
37254449
41489361
44170481
46367587
48553879
48992030
53792475
57142878
60713086
63117418
69318778
72151672
74564208
76224438
80214018
81348649
82938589
85545349
85600149
88350213
94639672
97551658
98692936
100619614
101454654
...

result:

wrong answer 3rd numbers differ - expected: '11343512', found: '11343892'

Test #7:

score: 0
Wrong Answer
time: 87ms
memory: 21064kb

input:

100000 100000
1 2 76
1 3 842
1 4 532
1 5 416
2 6 353
3 7 770
4 8 416
5 9 241
6 10 682
7 11 639
8 12 302
9 13 785
10 14 784
11 15 607
12 16 889
13 17 792
14 18 891
15 19 663
16 20 88
17 21 854
18 22 681
19 23 95
20 24 81
21 25 175
22 26 329
23 27 558
24 28 422
25 29 850
26 30 919
27 31 674
28 32 474
...

output:

2713852078
2787520891
6527489031
13501480013
17001684975
20527483734
21221730688
21677669036
29217868644
31079539101
39838151812
42766863995
43311506798
44675938098
45227927586
53320789257
53524963982
58935150002
60142944888
64659903596
65420584367
77280628303
77307909463
78481309735
83871278773
841...

result:

wrong answer 1st numbers differ - expected: '0', found: '2713852078'

Test #8:

score: 5
Accepted
time: 65ms
memory: 18008kb

input:

100000 100000
1 2 788
1 3 928
2 4 165
2 5 545
3 6 762
3 7 259
4 8 786
4 9 494
5 10 881
5 11 172
6 12 371
6 13 908
7 14 884
7 15 398
8 16 995
8 17 914
9 18 464
9 19 684
10 20 300
10 21 706
11 22 398
11 23 134
12 24 818
12 25 927
13 26 557
13 27 323
14 28 344
14 29 394
15 30 990
15 31 819
16 32 904
16...

output:

0
13759914
16740024
18992578
22378132
23867055
28122747
30335307
31749537
36290022
45900805
51670102
52846582
56682532
59579852
63392221
69141725
76492224
79540010
80805718
81557318
82453646
86256913
87664693
92106913
98535653
100697649
104040813
109689267
116355009
119615745
127498560
129757164
133...

result:

ok 100000 numbers

Test #9:

score: 0
Wrong Answer
time: 95ms
memory: 21592kb

input:

100000 100000
1 2 494
2 3 30
3 4 81
4 5 890
5 6 547
6 7 811
7 8 245
8 9 175
9 10 126
10 11 871
11 12 826
12 13 705
13 14 527
14 15 360
15 16 447
16 17 800
17 18 794
18 19 340
19 20 789
20 21 335
21 22 873
22 23 826
23 24 770
24 25 537
25 26 591
26 27 523
27 28 455
28 29 599
29 30 912
30 31 789
31 32...

output:

270855592
1518440134
4132671170
5572338458
7853679608
11913556352
19450848436
23039882744
28354622252
33706520177
34187694563
34857911203
36133617013
38638704907
40170011901
43499064405
45668328581
45741654014
48111954206
50841021034
51391252769
51991424345
53719161653
55788465497
56336480488
596884...

result:

wrong answer 1st numbers differ - expected: '0', found: '270855592'

Test #10:

score: 0
Wrong Answer
time: 100ms
memory: 21528kb

input:

100000 100000
1 2 386
2 3 954
3 4 980
4 5 584
5 6 346
6 7 854
7 8 763
8 9 824
9 10 909
10 11 174
11 12 635
12 13 498
13 14 578
14 15 468
15 16 248
16 17 739
17 18 175
18 19 531
19 20 93
20 21 939
21 22 795
22 23 250
23 24 54
24 25 124
25 26 273
26 27 758
27 28 597
28 29 279
29 30 149
30 31 869
31 32...

output:

56848488
233501088
4645588637
4781778747
9197708211
14148377529
14712123721
16622382470
18551944752
18961024104
19156080182
19237384202
25093110920
26063380126
31645147878
31892406579
33324231099
34362636148
35027885052
38119827342
39415995397
41732588333
45366329594
45960882394
46493541508
46958416...

result:

wrong answer 1st numbers differ - expected: '0', found: '56848488'

Test #11:

score: 0
Wrong Answer
time: 98ms
memory: 21488kb

input:

100000 100000
1 2 126
2 3 500
3 4 234
4 5 710
5 6 254
6 7 818
7 8 959
8 9 242
9 10 768
10 11 479
11 12 5
12 13 453
13 14 774
14 15 371
15 16 170
16 17 685
17 18 202
18 19 992
19 20 647
20 21 307
21 22 925
22 23 410
23 24 328
24 25 153
25 26 807
26 27 719
27 28 590
28 29 812
29 30 515
30 31 778
31 32...

output:

22780974
1407881316
5490742856
5955630175
6317848036
12994357686
16258591150
20766266644
21682447568
21949567148
30837697804
31689369024
35837437414
39896760059
40237921019
41978803991
45497680877
46313060637
51913899762
54358674574
55641545449
56184694781
56356642313
58314591737
63967045921
6765332...

result:

wrong answer 1st numbers differ - expected: '0', found: '22780974'

Test #12:

score: 0
Wrong Answer
time: 104ms
memory: 21608kb

input:

100000 100000
1 2 949
2 3 876
3 4 748
4 5 477
5 6 381
6 7 771
7 8 32
8 9 235
9 10 81
10 11 184
11 12 358
12 13 786
13 14 421
14 15 673
15 16 841
16 17 184
17 18 556
18 19 350
19 20 930
20 21 584
21 22 882
22 23 109
23 24 189
24 25 160
25 26 883
26 27 817
27 28 234
28 29 150
29 30 817
30 31 254
31 32...

output:

14914754
316568469
2297578023
12477820910
12686728388
13737772397
17481634155
23845995599
29067484266
30818399502
31005356676
34064474514
34338254270
34637753789
42716985539
42719967779
45221058049
46586297617
46757786197
49281365839
53834726174
55464310804
57577690818
61797377506
65557685946
656432...

result:

wrong answer 1st numbers differ - expected: '0', found: '14914754'

Test #13:

score: 0
Wrong Answer
time: 91ms
memory: 21720kb

input:

100000 100000
1 2 973
2 3 609
3 4 712
4 5 864
5 6 415
6 7 296
7 8 367
8 9 407
9 10 987
10 11 396
11 12 942
12 13 406
13 14 109
14 15 178
15 16 95
16 17 60
17 18 647
18 19 895
19 20 347
20 21 857
21 22 887
22 23 800
23 24 280
24 25 166
25 26 144
26 27 929
27 28 138
28 29 118
29 30 408
30 31 703
31 32...

output:

518748876
2987282004
3203341906
11977501247
17550556029
23874165208
24620552002
24876801658
25124030266
28453775638
30405513475
31014655561
34050349684
34505992245
35492539509
35812870661
37592651873
38503129086
39641510814
40627920994
43228632368
43449019128
43503250877
46225743747
50641207221
5240...

result:

wrong answer 1st numbers differ - expected: '0', found: '518748876'

Test #14:

score: 0
Wrong Answer
time: 98ms
memory: 21468kb

input:

100000 100000
1 2 249
2 3 541
3 4 679
4 5 789
5 6 301
6 7 218
7 8 281
8 9 730
9 10 906
10 11 54
11 12 568
12 13 610
13 14 445
14 15 120
15 16 370
16 17 214
17 18 876
18 19 701
19 20 935
20 21 227
21 22 492
22 23 280
23 24 627
24 25 1
25 26 451
26 27 163
27 28 554
28 29 759
29 30 386
30 31 150
31 32 ...

output:

635873931
3905715266
8765996016
12015630166
13037545918
12093919608
16359765242
16673190059
20634721355
21578936255
23504590271
26969483057
30952957993
32326068459
33034741359
35115645926
35265840038
39709576580
42309496760
42842509272
49212790476
49499925810
56806149120
57056151724
58537568024
5966...

result:

wrong answer 1st numbers differ - expected: '0', found: '635873931'

Test #15:

score: 0
Wrong Answer
time: 111ms
memory: 21760kb

input:

100000 100000
1 2 24
2 3 258
3 4 408
4 5 587
5 6 611
6 7 919
7 8 412
8 9 229
9 10 579
10 11 931
11 12 77
12 13 824
13 14 174
14 15 492
15 16 513
16 17 904
17 18 903
18 19 147
19 20 219
20 21 753
21 22 578
22 23 658
23 24 288
24 25 816
25 26 592
26 27 988
27 28 433
28 29 643
29 30 392
30 31 663
31 32...

output:

1293980
1112340896
1664738426
1739707892
6532554966
7868716356
8401429070
10689555416
14095379858
21154565651
21660642531
22523838939
25127318115
25831325679
30157904815
30407197715
30959510795
34850650091
36227138095
37275805239
38994505406
39218806021
41255959720
46510532498
47032412508
4821688889...

result:

wrong answer 1st numbers differ - expected: '0', found: '1293980'

Test #16:

score: 0
Wrong Answer
time: 100ms
memory: 21416kb

input:

100000 100000
1 2 567
2 3 812
3 4 146
4 5 65
5 6 419
6 7 86
7 8 920
8 9 832
9 10 130
10 11 53
11 12 894
12 13 247
13 14 137
14 15 712
15 16 282
16 17 393
17 18 618
18 19 535
19 20 799
20 21 227
21 22 355
22 23 181
23 24 767
24 25 350
25 26 42
26 27 710
27 28 378
28 29 428
29 30 595
30 31 234
31 32 6...

output:

4034160
2177012225
2194379308
2482790396
5528031560
11442424072
14846100370
15663402616
18142921541
20794000097
21735178688
22927373455
23499107545
28885147862
33469858094
35552296144
36230675657
39324921503
41027362395
41831277065
48214197755
48419980885
49940905857
52337687117
49292684415
54939284...

result:

wrong answer 1st numbers differ - expected: '0', found: '4034160'

Test #17:

score: 0
Wrong Answer
time: 95ms
memory: 21748kb

input:

100000 100000
1 2 408
2 3 524
3 4 486
4 5 497
5 6 598
6 7 638
7 8 752
8 9 365
9 10 217
10 11 192
11 12 711
12 13 282
13 14 659
14 15 134
15 16 240
16 17 128
17 18 651
18 19 975
19 20 113
20 21 159
21 22 861
22 23 111
23 24 957
24 25 814
25 26 386
26 27 25
27 28 791
28 29 446
29 30 151
30 31 790
31 3...

output:

13534600
1399470135
7270846331
8136974741
10100109297
10108208622
12117725352
12842925993
12850323713
13789301876
14196452438
15373624538
15996729772
17541163684
21964991400
28158657969
34658464062
36521349204
40957919811
51523376531
52977087641
54570120396
57450386070
58240070578
63427260716
637133...

result:

wrong answer 1st numbers differ - expected: '0', found: '13534600'

Test #18:

score: 0
Wrong Answer
time: 105ms
memory: 21732kb

input:

100000 100000
1 2 716
2 3 57
3 4 220
4 5 368
5 6 120
6 7 843
7 8 313
8 9 211
9 10 444
10 11 886
11 12 40
12 13 811
13 14 938
14 15 14
15 16 745
16 17 725
17 18 247
18 19 592
19 20 92
20 21 483
21 22 645
22 23 393
23 24 294
24 25 247
25 26 25
26 27 667
27 28 432
28 29 852
29 30 644
30 31 694
31 32 86...

output:

20673553
2237427100
2634783418
2670043063
4905858271
5435226511
10172550282
9761457872
10693237592
16959127122
14897600744
17765193277
18370587709
17317912916
22333665626
27240995768
31726943570
36629373245
38408339005
39195229650
40092228834
41807501448
43666299123
43711755774
44803678650
457491592...

result:

wrong answer 1st numbers differ - expected: '0', found: '20673553'

Test #19:

score: 0
Wrong Answer
time: 90ms
memory: 21748kb

input:

100000 100000
1 2 405
2 3 375
3 4 830
4 5 199
5 6 993
6 7 711
7 8 797
8 9 864
9 10 402
10 11 809
11 12 852
12 13 250
13 14 5
14 15 268
15 16 403
16 17 63
17 18 589
18 19 502
19 20 568
20 21 663
21 22 922
22 23 605
23 24 824
24 25 585
25 26 169
26 27 951
27 28 65
28 29 116
29 30 71
30 31 561
31 32 31...

output:

105845080
2924522283
5343307395
6206254928
8065998653
12085790525
12048634953
15225656457
15834156347
16425815365
18109690021
18345818551
19945659376
20329478452
22857770348
26963436710
27791938195
35473278587
36110298887
40329153107
41497925027
42218272642
45143233042
47750454254
49188083278
494924...

result:

wrong answer 1st numbers differ - expected: '0', found: '105845080'

Test #20:

score: 0
Wrong Answer
time: 96ms
memory: 21704kb

input:

100000 100000
1 2 246
2 3 34
3 4 21
4 5 38
5 6 476
6 7 769
7 8 511
8 9 127
9 10 165
10 11 71
11 12 638
12 13 416
13 14 29
14 15 681
15 16 594
16 17 708
17 18 560
18 19 948
19 20 433
20 21 773
21 22 963
22 23 302
23 24 194
24 25 676
25 26 414
26 27 229
27 28 790
28 29 474
29 30 517
30 31 279
31 32 53...

output:

1383164832
2069090828
2114291492
1921607090
4819489182
6529384894
6770556994
7310419082
9224064593
10046155093
13014491748
13299981476
20173112650
21499763026
26961231766
26024246881
26890789633
27667424548
32951634301
37041432676
38069622936
39864524440
44089566687
47570707857
48305133244
538103816...

result:

wrong answer 1st numbers differ - expected: '0', found: '1383164832'