QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#864502 | #6969. 幻想乡战略游戏 | msk_sama | 10 | 118ms | 27200kb | C++20 | 3.0kb | 2025-01-20 17:34:45 | 2025-01-20 17:34:51 |
Judging History
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],F[N],vis[N],son[N],sz[N],top[N],dfn[N],tim;ll d[N],a[N],s1,s2;
vector<pii> g[N],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(v,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];
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]]){
int x=top[u],y=fa[x],z=son[y];
s+=t2.qry(dfn[x],dfn[u]-1);
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(auto [v,x]:G[u]){
ll t=qry(v);
if(t<p) id=x,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: 1ms
memory: 11368kb
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:
6056 6966009 12746745 84519580 84875820 733809225 739840239 838356648 1067082615 1275517833 1496427669 1718120634 1790514420 1962414137 2349379847 2449886905 2607484931 2577146716 2849512683 2877341715 2885266429 3009993125 3016891805 3192371263 3264723205 3358247896 3685321216 3869002860 3947016696...
result:
wrong answer 1st numbers differ - expected: '0', found: '6056'
Test #2:
score: 0
Wrong Answer
time: 4ms
memory: 9764kb
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:
48108195 98832099 187877856 190275504 293802548 322154492 602157668 605462418 978576970 1047279929 1117075025 1409917495 1436169901 1447063538 1447262357 1737907597 1742807674 2068969882 2162222843 2170534881 2170795011 2308024027 2318708043 2348308698 2385266838 2500434258 2758653514 2826534586 295...
result:
wrong answer 1st numbers differ - expected: '0', found: '48108195'
Test #3:
score: 0
Wrong Answer
time: 3ms
memory: 9516kb
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 6579831 35923228 436437648 455969808 676743831 676855817 718838190 881228652 881992628 1029158291 1106359049 1115637978 1143238518 1639689766 1665390475 1981552285 2086941700 2091659536 2189533060 2193754348 2267987458 2455353742 2588570392 2593878424 2614355487 2686489955 2765272715 2792854364 27...
result:
wrong answer 2nd numbers differ - expected: '6320496', found: '6579831'
Test #4:
score: 0
Wrong Answer
time: 78ms
memory: 27032kb
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:
2070590 6537777382 22263357403 26281027950 35948464820 41376321965 53413478315 70041686227 72888764276 74091509084 77913679457 93394910952 100692932901 101897033191 103840476910 105202079530 113299533406 124627680506 126456350808 129151064439 130364684809 140131535170 143771371479 148604393390 16200...
result:
wrong answer 1st numbers differ - expected: '0', found: '2070590'
Test #5:
score: 0
Wrong Answer
time: 72ms
memory: 27200kb
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:
32006631 6869095142 8589935476 20942093083 41893932205 45275851286 49497208539 52877533300 57814331860 61555560101 78327432076 93840602804 99828047030 99934459556 101012739009 109345035964 117876635497 134615722486 140162893390 142866946789 148940851238 157062170567 161087797835 169713237415 1703126...
result:
wrong answer 1st numbers differ - expected: '0', found: '32006631'
Test #6:
score: 5
Accepted
time: 107ms
memory: 19320kb
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 11343512 12924462 17855554 21198058 24537382 28404326 31235630 37238909 41457497 44153177 46367587 48542539 48979206 53792475 57142878 60713086 63117418 69318778 72151672 74564208 76224438 80214018 81348649 82938589 85545349 85600149 88350213 94639672 97551658 98692936 100619614 101454654 ...
result:
ok 100000 numbers
Test #7:
score: 0
Wrong Answer
time: 85ms
memory: 20956kb
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:
2713482254 2787166459 6527454039 13501453783 17001614847 20527477585 21221675657 21677628128 29217844736 31079526416 39837886820 42766863995 43311506798 44675938098 45227927586 53320789257 53524963982 58935150002 60142944888 64659903596 65420584367 77280628303 77307909463 78481309735 83871278773 841...
result:
wrong answer 1st numbers differ - expected: '0', found: '2713482254'
Test #8:
score: 5
Accepted
time: 65ms
memory: 18764kb
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: 98ms
memory: 22384kb
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:
270689944 1518028398 4132548674 5571985586 7853434616 11913472136 19450645556 23039866040 28354436420 33706142945 34187423355 34857677115 36133286877 38638478243 40169840453 43498789949 45668190077 45741411806 48111511550 50840489754 51390699449 51990805601 53718713893 55787814505 56335845968 596879...
result:
wrong answer 1st numbers differ - expected: '0', found: '270689944'
Test #10:
score: 0
Wrong Answer
time: 118ms
memory: 21828kb
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:
56603763 232936563 4645561841 4781758419 9197035461 14148364329 14712096925 16621818920 18551878767 18960923139 19155809087 19237196582 25092928024 26063171654 31645099302 31892333899 33324094939 34362456012 35027722212 38119552998 39415683333 41732437085 45366314150 45960853750 46493497420 46958372...
result:
wrong answer 1st numbers differ - expected: '0', found: '56603763'
Test #11:
score: 0
Wrong Answer
time: 100ms
memory: 21852kb
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:
22601016 1407871592 5490635120 5955073003 6317404228 12994249436 16258091062 20766137629 21682428048 21949441488 30836687980 31688614068 35835966898 39895767923 40237063955 41978128631 45497515253 46312851931 51913312181 54358327741 55641300371 56184586531 56356438901 58314412908 63966450546 6765247...
result:
wrong answer 1st numbers differ - expected: '0', found: '22601016'
Test #12:
score: 0
Wrong Answer
time: 114ms
memory: 22164kb
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:
14477534 316228899 2297121273 12477818402 12685974788 13737751478 17481577285 23845304399 29067471495 30818393130 31005343230 34064468115 34337826462 34637749226 42716449857 42719349117 45221052517 46586283226 46757469029 49280857817 53833533106 55462927804 57576099446 61795253218 65557291994 656428...
result:
wrong answer 1st numbers differ - expected: '0', found: '14477534'
Test #13:
score: 0
Wrong Answer
time: 96ms
memory: 22288kb
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:
518318217 2987203092 3202904106 11977146096 17550548161 23874122658 24620364939 24876717711 25123908642 28453688111 30405468855 31014013161 34050263763 34505989025 35492396575 35812716561 37592272143 38502621246 39640962494 40627434314 43228222508 43448598458 43502832737 46225407257 50641004591 5240...
result:
wrong answer 1st numbers differ - expected: '0', found: '518318217'
Test #14:
score: 0
Wrong Answer
time: 98ms
memory: 21952kb
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:
635585643 3905603624 8765925015 12015495340 13037376523 12093762360 16359639524 16673112848 20634593636 21578743883 23504371403 26969233346 30952749820 32325872844 33034518834 35115401114 35265588602 39709389866 42309336749 42842280813 49212628671 49499766627 56806058592 57055995232 58537380137 5966...
result:
wrong answer 1st numbers differ - expected: '0', found: '635585643'
Test #15:
score: 0
Wrong Answer
time: 106ms
memory: 22000kb
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:
1241432 1112245086 1664524026 1739230182 6531800211 7867905991 8401419556 10688669006 14094671333 21154179731 21659933336 22523339454 25126707745 25830767569 30157035825 30406345475 30958477655 34849373741 36226109310 37274504434 38993322856 39217605046 41255053210 46509918778 47031854733 4821657499...
result:
wrong answer 1st numbers differ - expected: '0', found: '1241432'
Test #16:
score: 0
Wrong Answer
time: 109ms
memory: 22008kb
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:
3944772 2176666759 2194050980 2481565480 5526257326 11441357006 14844348686 15662388768 18142341555 20792801339 21733854552 22925379133 23496912077 28883581088 33467724864 35549914864 36228111271 39321594025 41024271241 41828690129 48210808039 48416510891 49937653245 52334171121 49292540315 54935419...
result:
wrong answer 1st numbers differ - expected: '0', found: '3944772'
Test #17:
score: 0
Wrong Answer
time: 100ms
memory: 21916kb
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:
13420365 1399317009 7270821492 8136552057 10099759025 10107502605 12117153213 12842395533 12849776413 13788788677 14196015861 15373237218 15996725516 17540764155 21964721539 28158641393 34658392718 36521049636 40957870086 51523176101 52976950094 54569896404 57450293352 58239944506 63427065947 637130...
result:
wrong answer 1st numbers differ - expected: '0', found: '13420365'
Test #18:
score: 0
Wrong Answer
time: 111ms
memory: 22020kb
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:
20062804 2237408489 2634689540 2669867524 4905597214 5435178726 10172124744 9761130512 10692972542 16958984764 14897575076 17765072802 18370455858 17317889318 22333588206 27240986130 31726891035 36629367873 38408277148 39195127108 40092089004 41807339340 43666091748 43711591770 44803525074 457490182...
result:
wrong answer 1st numbers differ - expected: '0', found: '20062804'
Test #19:
score: 0
Wrong Answer
time: 108ms
memory: 21932kb
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:
105651040 2924465412 5343306985 6206172755 8065978858 12085762403 12048631253 15225629109 15834106209 16425712509 18109642893 18345777099 19945580514 20329415328 22857661386 26963402310 27791927816 35473203681 36110209963 40329129199 41497913589 42218240994 45143162694 47750338842 49187987560 494923...
result:
wrong answer 1st numbers differ - expected: '0', found: '105651040'
Test #20:
score: 0
Wrong Answer
time: 98ms
memory: 21956kb
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:
1382919216 2068930178 2114137268 1921189496 4819242138 6529217818 6770229268 7310312339 9223848251 10045626376 13013792028 13299118607 20171906347 21498797341 26960001187 26023296282 26890084833 27666627243 32950294480 37040440930 38068437696 39863489497 44088227937 47569187037 48303473551 538089922...
result:
wrong answer 1st numbers differ - expected: '0', found: '1382919216'