QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#354920 | #5094. 小 H 分糖果 | ANIG | 40 | 5418ms | 872676kb | C++23 | 6.8kb | 2024-03-16 08:36:31 | 2024-04-09 18:19:47 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll __int128
const int N=1e5+5,inf=1e18;
struct gj{
signed a;int b;
friend gj operator+(gj a,gj b){
gj c={a.a+b.a,a.b+b.b};
while(c.b>inf)c.a++,c.b-=inf;
while(c.b<0)c.a--,c.b+=inf;
return c;
}
friend gj operator-(gj a,gj b){
gj c={a.a-b.a,a.b-b.b};
while(c.b>inf)c.a++,c.b-=inf;
while(c.b<0)c.a--,c.b+=inf;
return c;
}
ll gets(){
return (ll)a*inf+b;
}
}emps;
struct msg{
signed sm;
int rs;
gj pf;
friend msg operator+(msg a,msg b){
return {a.sm+b.sm,a.rs+b.rs,a.pf+b.pf};
}
friend msg operator-(msg a,msg b){
return {a.sm-b.sm,a.rs-b.rs,a.pf-b.pf};
}
msg rev(){
return {-sm,-rs,emps-pf};
}
}emp;
namespace trr{
struct node{
signed son[2],val;
char fl;
msg sm;
}p[N*150];
int bk[150*N],idx;
int nw(){
int y=bk[idx--];
p[y].fl=30;
return y;
}
void add(int x,int d,msg sm){
if(p[x].fl<0){
p[x].sm=p[x].sm+sm;
return;
}
int c=d>>p[x].fl&1;
// cout<<d<<"-"<<(int)p[x].fl<<" "<<c<<" "<<p[x].son[c]<<endl;
if(!p[x].son[c])p[x].son[c]=nw(),p[p[x].son[c]].val=d,p[p[x].son[c]].fl=-1;
else{
// cout<<d<<" "<<(int)p[p[x].son[c]].fl<<" "<<p[p[x].son[c]].val<<endl;
if((d>>p[p[x].son[c]].fl+1)!=p[p[x].son[c]].val){
for(int i=p[x].fl-1;;i--){
if((d>>i+1)!=p[p[x].son[c]].val>>i-p[p[x].son[c]].fl){
i++;
int t=p[x].son[c];
// cout<<x<<" "<<i<<" "<<d<<" "<<p[x].son[c]<<" "<<p[p[x].son[c]].val<<" "<<(int)p[p[x].son[c]].fl<<" "<<(p[t].val>>p[x].fl-p[t].fl-1&1)<<endl;
p[x].son[c]=nw();
p[p[x].son[c]].val=d>>i+1;
p[p[x].son[c]].fl=i;
p[p[x].son[c]].son[p[t].val>>i-p[t].fl-1&1]=t;
break;
}
}
}
}
add(p[x].son[c],d,sm);
p[x].sm=p[p[x].son[0]].sm+p[p[x].son[1]].sm;
}
msg gets(int x,int d){
if(!x)return emp;
if(p[x].fl<0)return p[x].sm;
int c=d>>p[x].fl&1;
// cout<<x<<" "<<(int)p[x].fl<<" "<<p[x].val<<" "<<p[x].son[0]<<" "<<p[x].son[1]<<" "<<(int)p[p[x].son[0]].fl<<" "<<p[p[x].son[0]].val<<" "<<(int)p[p[x].son[1]].fl<<" "<<p[p[x].son[1]].val<<" "<<c<<endl;
if((d>>p[x].fl+1)>p[x].val)return p[x].sm;
msg res=emp;
if(c)res=res+p[p[x].son[0]].sm;
if(p[p[x].son[c]].val<=(d>>p[p[x].son[c]].fl+1))res=res+gets(p[x].son[c],d);
return res;
}
void add(int x,int l,int r,msg sm){
add(x,l,sm);add(x,r+1,sm.rev());
}
}
namespace tr{
struct node{
signed lson,rson,rt;
}p[100*N];
int idx;
void init(int x,int op){
if(!p[x].lson&&op==0)p[x].lson=++idx,p[idx].rt=trr::nw();
if(!p[x].rson&&op==1)p[x].rson=++idx,p[idx].rt=trr::nw();
}
void add(int x,int d,int l,int r,int sm,int nl=0,int nr=1e9){
if(sm>0)trr::add(p[x].rt,l,r,{sm,sm*d,{0,sm*d*d}});
else trr::add(p[x].rt,l,r,{sm,sm*d,{-1,inf+sm*d*d}});
// cout<<x<<" "<<nl<<"-"<<nr<<" "<<p[x].rt<<" "<<p[x].lson<<endl;
if(nl==nr)return;
int mid=nl+nr>>1;
if(d<=mid)init(x,0);
else init(x,1);
if(d<=mid)add(p[x].lson,d,l,r,sm,nl,mid);
else add(p[x].rson,d,l,r,sm,mid+1,nr);
}
msg gets(int x,int l,int r,int d,int nl=0,int nr=1e9){
if(!x||!d)return emp;
// cout<<"ok"<<endl;
if(l<=nl&&r>=nr)return trr::gets(p[x].rt,d);
int mid=nl+nr>>1;
if(r<=mid)return gets(p[x].lson,l,r,d,nl,mid);
if(l>mid)return gets(p[x].rson,l,r,d,mid+1,nr);
return gets(p[x].lson,l,r,d,nl,mid)+gets(p[x].rson,l,r,d,mid+1,nr);
}
int solve(int x,int x1,int x2,int x3,int x4,int k,int he,int sz,int nl=0,int nr=1e9){
if(nl==nr)return nl;
int mid=nl+nr>>1;
msg c=trr::gets(p[p[x].rson].rt,x1)+trr::gets(p[p[x].rson].rt,x2)-trr::gets(p[p[x].rson].rt,x3)-trr::gets(p[p[x].rson].rt,x4);
int ans=c.rs+he-mid*(c.sm+sz);
// cout<<nl<<" "<<nr<<" "<<mid<<" "<<c.sm<<" "<<c.rs<<" "<<ans<<" "<<trr::gets(p[p[x].rson].rt,x2).rs<<endl;
if(ans<=k)return solve(p[x].lson,x1,x2,x3,x4,k,he+c.rs,sz+c.sm,nl,mid);
return solve(p[x].rson,x1,x2,x3,x4,k,he,sz,mid+1,nr);
}
}
int n,m,w[N],fa[N],dep[N],mk[N],dfn[N],eds[N],idx,fs[N][20];
ll al;
vector<int>p[N];
void dfs(int x){
mk[x]=1;dfn[x]=++idx;
for(int i=1;i<=18;i++)fs[x][i]=fs[fs[x][i-1]][i-1];
for(auto c:p[x]){
if(mk[c])continue;
dep[c]=dep[x]+1;
fs[c][0]=x;
dfs(c);
fa[c]=x;
}
mk[x]=0;eds[x]=idx;
}
int up(int x,int k){
while(k){
x=fs[x][__lg(k&-k)];
k^=k&-k;
}
return x;
}
int lca(int a,int b){
if(dep[a]>dep[b])swap(a,b);
b=up(b,dep[b]-dep[a]);
if(a==b)return a;
for(int i=18;i>=0;i--){
if(fs[a][i]!=fs[b][i])a=fs[a][i],b=fs[b][i];
}
return fs[a][0];
}
msg gets(int x,int l=0,int r=1e9){
return tr::gets(1,l,r,dfn[x]);
}
void print(ll x){
if(x>9)print(x/10);
putchar(x%10+'0');
}
signed main(){
for(int i=1;i<150*N;i++)trr::bk[++trr::idx]=i;
//fin();fout();
cin>>n>>m;
for(int i=1;i<n;i++){
int x=i,y=i+1;
cin>>x>>y;
p[x].push_back(y);
p[y].push_back(x);
}
tr::idx=1;
tr::p[1].rt=trr::nw();
dfs(1);
for(int i=1;i<=n;i++)cin>>w[i],tr::add(1,w[i],dfn[i],eds[i],1),al+=w[i]*w[i];
while(m--){
int op=1,x=rand()%n+1,y=rand()%n+1,k;
cin>>op>>x>>y;
if(op==1){
tr::add(1,w[x],dfn[x],eds[x],-1);
al-=w[x]*w[x];
w[x]=y;
al+=y*y;
tr::add(1,y,dfn[x],eds[x],1);
}else{
cin>>k;
int z=lca(x,y);
ll res=0,ans=0;
msg cc=gets(x)+gets(y)-gets(z)-gets(fa[z]);
res=al-cc.pf.gets();
// cout<<cc.sm<<" "<<cc.rs<<" "<<x<<" "<<y<<" "<<z<<endl;
if(cc.rs<=k){
print(res);
puts("");
continue;
}
int t=tr::solve(1,dfn[x],dfn[y],dfn[z],dfn[fa[z]],k,0,0);
cc=gets(x,0,t)+gets(y,0,t)-gets(z,0,t)-gets(fa[z],0,t);
res+=cc.pf.gets();
cc=gets(x,t+1,1e9)+gets(y,t+1,1e9)-gets(z,t+1,1e9)-gets(fa[z],t+1,1e9);
ans=cc.rs-t*cc.sm;res+=(ll)t*t*cc.sm;
res-=(k-ans)*(2*t-1);
print(res);
puts("");
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 24ms
memory: 131900kb
input:
866 841 1 864 4 153 9 559 10 410 11 336 12 417 14 666 18 241 21 184 22 849 23 40 25 783 26 189 28 329 29 216 31 864 34 581 40 131 42 625 45 744 47 723 50 633 51 447 52 454 53 88 55 619 60 259 62 680 67 126 72 371 73 742 80 196 81 536 82 647 85 254 87 172 88 489 93 708 94 227 95 340 96 7 7 91 97 594 ...
output:
285125508 285374449 285871392 285072359 284419704 284843737 284692039 284936099 285944374 285174668 285019779 284651455 287282253 287175619 284878507 285369672 284880507 285404741 284913527 286053317 288622563 286960150 287194443 288326074 286937403 287883097 288535226 288195055 288643208 288632989 ...
result:
ok 419 lines
Test #2:
score: 0
Accepted
time: 27ms
memory: 132116kb
input:
951 896 6 886 8 809 9 392 12 590 13 643 16 322 17 613 37 605 42 354 44 594 47 905 49 623 53 240 56 546 58 751 60 919 62 74 63 303 64 105 68 578 69 41 41 110 71 402 74 528 75 616 76 83 77 230 83 289 84 354 88 340 89 65 91 785 92 831 93 286 96 101 97 841 99 556 100 700 101 574 102 652 103 534 105 107 ...
output:
323762325 323477550 323340853 323950611 323726768 322803839 322828150 323927499 323452292 322435378 323945420 323767611 322557229 323137116 322736140 322900873 322078759 322286746 322383436 322825503 321513631 320299707 320738958 320752861 319979668 320430841 319960294 319658294 317899682 318822085 ...
result:
ok 443 lines
Subtask #2:
score: 15
Accepted
Dependency #1:
100%
Accepted
Test #3:
score: 15
Accepted
time: 8ms
memory: 131908kb
input:
903 809 2 4 5 604 6 396 10 637 12 593 22 855 26 586 29 170 34 64 37 559 42 413 44 789 46 584 49 83 52 642 55 688 59 899 61 614 68 572 74 707 79 278 80 32 83 315 86 779 87 323 89 901 91 720 92 463 93 592 98 348 99 644 100 465 102 377 103 835 104 479 107 757 111 207 112 458 116 43 43 623 121 538 124 1...
output:
276805063584166248522 273874231351955459944 277417224646898680385 276526352730593552950 276097972273805693659 277000626431768466948 275360041448269408386 277555271820331288115 275027390500562660091 275359996505750169694 273988679638576452218 277452768032389764786 279717343951191633999 27734436115806...
result:
ok 399 lines
Test #4:
score: 0
Accepted
time: 28ms
memory: 132032kb
input:
971 875 1 259 4 861 6 917 7 397 8 943 10 11 19 105 20 319 23 330 31 665 32 25 34 247 38 794 39 356 44 169 46 552 51 776 52 74 54 491 55 657 56 132 62 94 63 534 65 867 66 398 68 628 69 909 71 791 75 769 80 98 83 540 84 553 86 404 89 28 91 929 95 732 96 82 82 121 98 460 100 565 102 640 108 849 109 582...
output:
314553601458474340784 315008648334632896915 309593803308875213443 314381387084951628984 314980104154667100244 315415916743855708963 311283117410815259761 310699439139678733298 315534307669378140861 310218082581673983393 313516726064555225372 312528429437654618149 311275276237153741600 31488347816476...
result:
ok 423 lines
Test #5:
score: 0
Accepted
time: 19ms
memory: 136240kb
input:
863 950 4 210 7 429 10 484 12 11 13 524 14 856 17 369 21 616 22 608 24 348 26 312 30 676 32 235 33 217 36 659 40 51 42 410 45 415 54 142 64 294 66 332 70 129 71 530 75 635 76 741 77 749 86 731 88 170 90 107 93 523 94 503 95 493 96 202 99 700 100 23 103 51 51 387 109 495 113 823 114 41 116 91 91 492 ...
output:
270446496979315412554 271870043667332946529 272456686591499140527 269284545566217062767 270990125394886733462 267756299196219401987 271960154369666524587 269381518805814804923 269742070489494972424 271327427174666570120 269616849392122791048 271332644790428015294 270212908721540092053 26902859752821...
result:
ok 485 lines
Subtask #3:
score: 15
Accepted
Test #6:
score: 15
Accepted
time: 4050ms
memory: 740192kb
input:
87080 98363 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52...
output:
27217464773998101198216 27222683135365131711066 27215685950441383375941 27221607244120669838311 27219047117137492446677 27222635053035794978138 27218848172360084265818 27217641965048032442370 27217075857038185043354 27219505943263517662069 27219987830714690994915 27216425553487126261338 272156845500...
result:
ok 49026 lines
Test #7:
score: 0
Accepted
time: 3874ms
memory: 722316kb
input:
84870 94829 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52...
output:
26588824183614252611956 26590972957572618361173 26588072360121209836363 26591857847070561616545 26589232564845670209408 26592122980539987339833 26587754880131177015120 26589297513918827289821 26587809270957143483620 26589079923893149835091 26592765548568891098479 26589309295038049830001 265880100172...
result:
ok 47183 lines
Test #8:
score: 0
Accepted
time: 3496ms
memory: 744228kb
input:
96634 80387 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52...
output:
30077825779968347451702 30074759816356090849186 30079514925011418879668 30076799133737016237726 30078990001739930674611 30080891961908067423095 30078486689897323991187 30075141829412423915885 30080001764736455079465 30073677391611280633894 30079391586644084223862 30080833751860317298636 300797099430...
result:
ok 40329 lines
Subtask #4:
score: 0
Wrong Answer
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Test #9:
score: 0
Wrong Answer
time: 5418ms
memory: 872676kb
input:
94940 96559 2 48868 6 41587 9 60037 10 4957 15 63113 24 2993 25 43059 26 23799 27 58856 31 92716 33 50875 34 864 37 46030 40 78345 42 75973 44 68250 45 3555 46 730 49 13522 50 26971 51 30585 52 29379 58 10570 60 66269 64 55951 66 89776 69 93660 70 28512 71 16310 75 91744 78 5146 81 8033 89 14642 90 ...
output:
29617178670453413164069 29614639318060085437952 29618450865028118043190 29619985987394160724901 29615742988245945792595 29614901462561753867573 29620072586799466410363 29617293585334506103852 29613760321829811476882 29613323124665517085193 29616445678681741866923 29616375117753770090809 296200415481...
result:
wrong answer 35237th lines differ - expected: '29727185948300565986599', found: '29727176247566599708219'