QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#53571 | #2305. 历史 | not_so_organic | 100 ✓ | 395ms | 32916kb | C++11 | 2.9kb | 2022-10-05 13:31:02 | 2022-10-05 13:31:05 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=4e5+10;
int n,m,first[N],cnt,fa[N];
ll a[N],s[N],vs[N],ans;
struct node{
int v,nxt;
}e[N<<1];
inline void add(int u,int v){e[++cnt].v=v;e[cnt].nxt=first[u];first[u]=cnt;}
int ch[N][2];
inline bool isroot(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
inline bool which(int x){return ch[fa[x]][1]==x;}
inline void pushup(int u){s[u]=vs[u]+s[ch[u][0]]+s[ch[u][1]]+a[u];}
inline void Rotate(int x){
int y=fa[x],z=fa[y],wx=which(x),wy=which(y);
fa[x]=z;if(!isroot(y)) ch[z][wy]=x;
ch[y][wx]=ch[x][wx^1];fa[ch[x][wx^1]]=y;
ch[x][wx^1]=y;fa[y]=x;
pushup(y);pushup(x);
}
inline void Splay(int x){
while(!isroot(x)){
int y=fa[x];
if(!isroot(y)) which(x)==which(y)?Rotate(y):Rotate(x);
Rotate(x);
}
pushup(x);
}
inline void solve(int u){
s[u]=a[u];
ll mx=a[u],pos=0;
for(int i=first[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==fa[u]) continue;
fa[v]=u;solve(v);
s[u]+=s[v];
if(s[v]>mx) mx=s[v],pos=v;
}
ans+=min(s[u]-1,(s[u]-mx)<<1);
if((mx<<1)>=s[u]+1) ch[u][1]=pos;
vs[u]=s[u]-a[u]-s[ch[u][1]];
}
inline ll calc(int u,ll sum,ll mx){
return mx?(sum-mx)<<1:((a[u]<<1)>=sum+1)?(sum-a[u])<<1:sum-1;
}
inline void access(int x,ll w){
Splay(x);
ll sum=s[x]-s[ch[x][0]],mx=s[ch[x][1]];
ans-=calc(x,sum,mx);
a[x]+=w;sum+=w;
if((mx<<1)<sum+1) ch[x][1]=0,vs[x]+=mx,mx=0;
pushup(x);ans+=calc(x,sum,mx);
int last=x;x=fa[x];
for(;x;last=x,x=fa[x]){
Splay(x);
ll sum=s[x]-s[ch[x][0]],mx=s[ch[x][1]];
ll rec=ans;
ans-=calc(x,sum,mx);
s[x]+=w;sum+=w;vs[x]+=w;
if((mx<<1)<sum+1) ch[x][1]=0,vs[x]+=mx,mx=0;
if((s[last]<<1)>=sum+1) ch[x][1]=last,vs[x]-=s[last],mx=s[last];
pushup(x);ans+=calc(x,sum,mx);
}
}
namespace iobuff{
const int LEN=1000000;
char in[LEN+5],out[LEN+5];
char *pin=in,*pout=out,*ed=in,*eout=out+LEN;
inline char gc(void){
#ifdef LOCAL
return getchar();
#endif
return pin==ed&&(ed=(pin=in)+fread(in,1,LEN,stdin),ed==in)?EOF:*pin++;
}
inline void pc(char c){
pout==eout&&(fwrite(out,1,LEN,stdout),pout=out);
(*pout++)=c;
}
inline void flush(){fwrite(out,1,pout-out,stdout),pout=out;}
template<typename T> inline void read(T &x){
static int f;
static char c;
c=gc(),f=1,x=0;
while(c<'0'||c>'9') f=(c=='-'?-1:1),c=gc();
while(c>='0'&&c<='9') x=10*x+c-'0',c=gc();
x*=f;
}
template<typename T> inline void putint(T x,char div){
static char s[15];
static int top;
top=0;
x<0?pc('-'),x=-x:0;
while(x) s[top++]=x%10,x/=10;
!top?pc('0'),0:0;
while(top--) pc(s[top]+'0');
pc(div);
}
}
using namespace iobuff;
int main(){
// freopen("b4.in","r",stdin);
read(n);read(m);
for(int i=1;i<=n;++i) read(a[i]);
for(int i=1,u,v;i<n;++i) read(u),read(v),add(u,v),add(v,u);
solve(1);
putint(ans,'\n');ll w;
for(int i=1,x;i<=m;++i){
read(x);read(w);
access(x,w);
putint(ans,'\n');
}
flush();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 10
Accepted
time: 0ms
memory: 3728kb
input:
10 0 1 1 1 1 1 1 1 1 1 1 10 5 8 5 1 2 1 5 3 4 4 5 9 2 9 6 1 7
output:
17
result:
ok single line: '17'
Test #2:
score: 10
Accepted
time: 71ms
memory: 20192kb
input:
150000 149910 1 2399920 1199960 599980 299990 149995 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
9599667 9599669 9599672 9599674 9599686 9599692 9599698 9599703 9599705 9599706 9599707 9599716 9599721 9599727 9599759 9599767 9599785 9599791 9599795 9599799 9599809 9599855 9599856 9599864 9600002 9600009 9600037 9600037 9600044 9600049 9600093 9600181 9600189 9600191 9600197 9600201 9600205 9600...
result:
ok 149911 lines
Test #3:
score: 10
Accepted
time: 82ms
memory: 20280kb
input:
150000 149954 1 2399920 1199960 599980 299990 149995 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
9599667 9599669 9599672 9599684 9599689 9599693 9599697 9599701 9599721 9599741 9599803 9599929 9599939 9599943 9599949 9599995 9600001 9600005 9600009 9600065 9600071 9600075 9600079 9600101 9600201 9600209 9600213 9600251 9600297 9600391 9600391 9600402 9600403 9600625 9600629 9600635 9601201 9601...
result:
ok 149955 lines
Test #4:
score: 10
Accepted
time: 24ms
memory: 15204kb
input:
150000 0 7724084 1808747 1 760841 5821258 2593314 3096183 5819780 8510371 4964943 1 1 399341 1 5184723 1072997 1 1 7215202 1 1 1 1 147960 7548634 2116190 7989065 3014630 8555028 294090 8051056 1814668 1 5799041 8609845 1 7288854 1 7656015 1 1 1 19666 6030783 1 1785189 1 8276572 1 1364761 4548065 857...
output:
3612613958284
result:
ok single line: '3612613958284'
Test #5:
score: 10
Accepted
time: 15ms
memory: 15116kb
input:
150000 0 9788766 1 4145086 2318648 3043955 6915352 1 1 1768719 1 806124 1 6784541 1 1 1 6406310 1 8135975 1862577 2489088 7715594 7376664 1 7066686 3394921 8476524 9017539 1 7285379 6035186 810507 1 1 1 9489441 1 1 1 4713716 1 9376990 681627 5159702 6566128 6926821 6942190 8597998 513112 362505 1 1 ...
output:
3629642717476
result:
ok single line: '3629642717476'
Test #6:
score: 10
Accepted
time: 53ms
memory: 16088kb
input:
150000 149959 1 1 1 1 1 1 1 1 6303720 1 1 1 6076031 1 9616222 3053858 1 1 1 8166453 1 1 1 7769956 1 1 1 1 1 1 1 1 1 1 1 1 1 1794304 1 1 1616597 3340058 1 1 1 1 7585636 5398598 1 1 1 1 1 1 1 1 1 1 6253940 1 1 1 1 1 1 1 1 1 1 1 1 1 8300863 1 5483661 9976196 1 1 7335509 1 1 1 1 9812313 1 1021798 420367...
output:
992152333550 992152333565 992152333580 992152333595 992152333610 992152333625 992152333640 992152333655 992152333670 992152333685 992152333700 992152333715 992152333730 992152333745 992152333760 992152333775 992152333790 992152333805 992152333820 992152333835 992152333850 992152333865 992152333880 9...
result:
ok 149960 lines
Test #7:
score: 10
Accepted
time: 66ms
memory: 16204kb
input:
150000 149923 1 4085457 1 1 118307 1 1 9893419 1 1 1 5136532 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 7876691 1 1 1 2518549 8095334 1 1 1 1 1 1 1 5121 1 1 7952364 1043408 1 1 1 2186656 1 1 1 1 1 1 1 2974238 1 1 1 1 1 1 7276666 1 1 1 1030015 1 1 1 1 1582849 1 8822747 1 1 6129389 1 1 1904750 2456056 ...
output:
986544341135 986544341150 986544341165 986544341180 986544341195 986544341210 986544341225 986544341240 986544341255 986544341270 986544341285 986544341300 986544341315 986544341330 986544341345 986544341360 986544341375 986544341390 986544341405 986544341420 986544341435 986544341450 986544341465 9...
result:
ok 149924 lines
Test #8:
score: 10
Accepted
time: 62ms
memory: 16208kb
input:
150000 149904 1 8208626 9166467 6512934 1 1 1283801 1 4863877 1 1 1 1 1 1 1 1 1 9353663 1 1861928 1 1511684 4565491 1 1 3657609 1 1 7260181 1 1 4621835 3285788 4900022 5320878 1 1 3237440 1 1 4134312 1 795034 6450215 1 1 1 1 1 8157896 1 1 1 1 1 1 6941973 1 522637 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4243177 ...
output:
946598576429 946598576444 946598576459 946598576474 946598576489 946598576504 946598576519 946598576534 946598576549 946598576564 946598576579 946598576594 946598576609 946598576624 946598576639 946598576654 946598576669 946598576684 946598576699 946598576714 946598576729 946598576744 946598576759 9...
result:
ok 149905 lines
Test #9:
score: 10
Accepted
time: 395ms
memory: 32916kb
input:
400000 399985 1 1 1 1 5518357 4106342 5714179 1 1 2881037 1 4350107 1 1 1 1 7958497 3573501 3416321 1681813 5246366 1 7376701 1 7166968 2786231 9952289 1 1 1 1 8736818 1 9850728 1 1 1 5320308 1 1 1 9552215 1 1 1 1 1 9788159 1252012 4398753 1 8320223 9733966 8658585 1772284 1 3317462 1996853 6481028 ...
output:
7828326731477 7828326731492 7828326731507 7828326731522 7828326731537 7828326731552 7828326731567 7828326731582 7828326731597 7828326731612 7828326731627 7828326731642 7828326731657 7828326731672 7828326731687 7828326731702 7828326731717 7828326731732 7828326731747 7828326731762 7828326731777 782832...
result:
ok 399986 lines
Test #10:
score: 10
Accepted
time: 349ms
memory: 32908kb
input:
400000 399905 1 7342875 1 1 3254459 1 1 1 1 1 1 1 816776 1 1 1 8814282 3203585 1 1 1 1 1 1 1107137 1 1 1 1 1 1 1 1 1 67752 1 1 1 1 6286151 1 1 1 3059588 1 1 1 1 1 1 1 9094635 1 1 1 1 1 1 1 1 1 2334603 1 9136202 1 1 1 4834053 1 1 1067444 1 1 1 1 1 1 4835140 1 1 1 1 1 1 1 1 1 195207 8295104 1 1 1 1 1 ...
output:
3178975585248 3178975585265 3178975585282 3178975585299 3178975585316 3178975585333 3178975585350 3178975585367 3178975585384 3178975585401 3178975585418 3178975585435 3178975585452 3178975585469 3178975585486 3178975585503 3178975585520 3178975585537 3178975585554 3178975585571 3178975585588 317897...
result:
ok 399906 lines
Extra Test:
score: 0
Extra Test Passed