QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#864479#6969. 幻想乡战略游戏msk_sama0 182ms27016kbC++203.1kb2025-01-20 17:18:372025-01-20 17:18:46

Judging History

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

  • [2025-01-20 17:18:46]
  • 评测
  • 测评结果:0
  • 用时:182ms
  • 内存:27016kb
  • [2025-01-20 17:18:37]
  • 提交

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 l<=r?qry(r)-qry(l-1):0;}    
}t1,t2;
void upd(int u,ll k){
    t1.upd(dfn[u],k);s1+=k,s2+=k*d[u];int x=u;
    for(;u;u=fa[top[u]]) t2.upd(dfn[u],k*d[x]),a[u]+=k*d[x];
}
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: 10996kb

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:

-22032
7430217
12699897
84148972
85037820
733116681
739126719
837206280
1065126461
1272631525
1493516149
1715235118
1787613108
1959232181
2345294055
2445830827
2602500963
2573077400
2844519827
2872342347
2880230463
3005438847
3012381327
3188288565
3260393767
3354314848
3680278792
3862028562
39402495...

result:

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

Test #2:

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

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:

47574145
97887793
186799462
189079894
292423850
320590058
600753046
604042821
976676081
1045525791
1115445245
1407201683
1432668425
1441808099
1442749967
1735448381
1739305091
2064543443
2158006589
2166538972
2166796201
2303592361
2314347497
2344254857
2380758207
2496439977
2754105513
2820338421
294...

result:

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

Test #3:

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

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:

-9072
6791367
35417612
435829236
455989311
674976303
675066458
717123568
879753068
880513784
1025678981
1102920009
1112533818
1140491748
1636358726
1661743939
1978150004
2082258170
2086965158
2184980690
2189219474
2263262744
2450753768
2583457850
2588682714
2609321593
2681362765
2760904885
278839342...

result:

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

Test #4:

score: 0
Wrong Answer
time: 70ms
memory: 27000kb

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: 70ms
memory: 27016kb

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: 182ms
memory: 18972kb

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:

-614510
-6955344
-3035505
-3748124
-20448592
-15835588
-17656258
-22435106
-24508082
-29099474
-40851005
-42681925
-39407354
-42133752
-42636086
-36454066
-38300716
-40293708
-37949928
-42551448
-44557569
-46405898
-47755754
-48668011
-51181329
-50844780
-52495065
-52523705
-48871121
-53690604
-5145...

result:

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

Test #7:

score: 0
Wrong Answer
time: 159ms
memory: 21060kb

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:

-5385333527
-5344288909
-18055549135
-20574445465
-29644186567
-30496188228
-33479510810
-33023572462
-25483372854
-28963432797
-30586041339
-25767045167
-26311561922
-27675897542
-28227975222
-36320553597
-36524772834
-41935262118
-43142752076
-47659499456
-46898789981
-35038345853
-35065625349
-33...

result:

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

Test #8:

score: 0
Wrong Answer
time: 82ms
memory: 18248kb

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:

-53119668
-49140636
-15594061
-13251361
-29908399
-4775129
-28517010
-30049422
-31199747
-25949042
-33003554
-38106912
-38894736
-42009720
-38753964
-34618844
-38730055
-44075197
-46464905
-47485799
-49595299
-48558610
-51821714
-50178413
-53997368
-47065073
-48590105
-51012910
-55630450
-67853789
-...

result:

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

Test #9:

score: 0
Wrong Answer
time: 94ms
memory: 21568kb

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
1516196674
4130617762
5570285050
7851626200
11910209806
19446638488
23035672796
28350412304
33702310229
34183484615
34853662855
36129368665
38633832159
40165139153
43494191657
45663120641
45736446074
48106746266
50835813094
51386044829
51986216405
53711564359
55780868203
56328770872
596798...

result:

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

Test #10:

score: 0
Wrong Answer
time: 97ms
memory: 21576kb

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:

56316368
232726904
4644814453
4780983003
9195155531
14145803289
14709549481
16619103736
18548644458
18957723810
19152779888
19233913388
25089433810
26059443086
31641210838
31888209373
33319368043
34357773092
35023021996
38114964286
39410874451
41727467387
45361208648
45955761448
46488420562
46952584...

result:

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

Test #11:

score: 0
Wrong Answer
time: 93ms
memory: 21744kb

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:

21715110
1406815452
5489676992
5954439095
6316656956
12993166606
16253202262
20760373692
21676554616
21943674196
30831006604
31682677824
35829852654
39889175299
40230336259
41971219231
45490096117
46305475877
51906315002
54350070110
55632701755
56175294295
56345858577
58303808001
63956262185
6764253...

result:

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

Test #12:

score: 0
Wrong Answer
time: 102ms
memory: 21480kb

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
12685401396
13735840849
17479702607
23842737059
29064225726
30815140962
31001993336
34061111174
34334890930
34634390449
42713622199
42716604439
45217694709
46582934277
46754422857
49278002499
53831362834
55460947464
57574327478
61794014166
65552408606
656379...

result:

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

Test #13:

score: 0
Wrong Answer
time: 92ms
memory: 21452kb

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:

517795344
2986423554
3201529924
11975689265
17548744047
23872209650
24618596444
24874846100
25122069806
28451567138
30403304975
31011403361
34047097484
34502740045
35488629517
35808960669
37588741881
38499219094
39637600822
40624011002
43224424448
43444811208
43499042957
46221421517
50635837391
5239...

result:

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

Test #14:

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

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:

633811563
3903652898
8763933648
12011760348
13032719190
12089264250
16354152974
16667577791
20629109087
21571802299
23497456315
26960658547
30944133483
32317243949
33025021409
35105925330
35256119442
39699855984
42299776164
42831407812
49201689016
49488824350
56795047660
57042486272
58523728762
5964...

result:

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

Test #15:

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

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
6531144104
7867099654
8399812368
10687938714
14093763156
21152948949
21658394719
22521591127
25125070303
25829077867
30152913835
30402206735
30954519815
34844805335
36221293339
37269960483
38988660650
39212961265
41250114964
46504687742
47026567752
4821104413...

result:

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

Test #16:

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

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
11441810984
14845487282
15662789528
18142308453
20793387009
21734565600
22926760367
23498494457
28881671498
33466381730
35548819780
36227199293
39321445139
41023802715
41826891183
48209811873
48415595003
49936360433
52333141693
49287810201
54933836...

result:

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

Test #17:

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

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
1399126803
7270101859
8136230269
10099074145
10107173470
12115173976
12840194833
12847592553
13786545768
14193696330
15370868430
15990350508
17534784420
21958612136
28151491283
34651297376
36517129648
40951852893
51515800593
52969511703
54562332018
57442597692
58232282200
63418598584
637047...

result:

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

Test #18:

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

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:

17965985
2237339292
2634695610
2669955255
4905770463
5435138703
10172462474
9761457872
10693237592
16957963410
14896437032
17764029565
18369423997
17316749204
22332501914
27239418500
31725366302
36626049155
38405014915
39191905560
40088904744
41804177358
43662975033
43708431684
44800354560
457457423...

result:

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

Test #19:

score: 0
Wrong Answer
time: 93ms
memory: 21640kb

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:

104818120
2923495323
5341277493
6204928484
8064672209
12084464081
12046305567
15223354013
15831853903
16423512921
18105551145
18341640867
19941481692
20325072384
22853364280
26959030642
27787532127
35468872519
36105892819
40324747039
41493368159
42213715774
45138676174
47745897386
49183197108
494873...

result:

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

Test #20:

score: 0
Wrong Answer
time: 94ms
memory: 21500kb

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:

1382329600
2068255596
2113456260
1920339970
4817386830
6527282542
6768454642
7308316730
9221962241
10042815491
13011152146
13296334770
20168111448
21494761824
26956230564
26020863505
26886991663
27663626578
32945456745
37035255120
38063445380
39858346884
44082674291
47563150421
48296103054
538013514...

result:

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