QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#88975 | #2305. 历史 | RainAir | 100 ✓ | 701ms | 49356kb | C++14 | 2.6kb | 2023-03-18 03:14:28 | 2023-03-18 03:14:29 |
Judging History
answer
#include<bits/stdc++.h>
#define fi first
#define se second
#define U unsigned
#define P std::pair<int,int>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
const int MAXN = 5e5 + 5;
int ch[MAXN][2],f[MAXN];
LL sm[MAXN],smi[MAXN],a[MAXN];
#define lc (ch[x][0])
#define rc (ch[x][1])
inline void pushup(int x){sm[x] = sm[lc]+sm[rc]+smi[x]+a[x];}
inline bool nroot(int x){return ch[f[x]][0]==x||ch[f[x]][1]==x;}
inline void rotate(int x){
int y = f[x],z = f[y],k = ch[y][1]==x,w = ch[x][!k];
if(nroot(y)) ch[z][ch[z][1]==y]=x;f[x]=z;
ch[x][!k] = y;f[y] = x;ch[y][k] = w;if(w) f[w] = y;
pushup(y);pushup(x);
}
inline void splay(int x){
while(nroot(x)){
int y = f[x],z = f[y];
if(nroot(y)) rotate((ch[z][1]==y)^(ch[y][1]==x)?x:y);
rotate(x);
}
pushup(x);
}
std::vector<int> G[MAXN];
int tp[MAXN];LL ans;
//0: 子树大 1:自己大 2: 都不大
inline void dfs(int v,int fa=0){
LL mx = a[v];int mp = v;
for(auto x:G[v]){
if(x == fa) continue;
f[x] = v;dfs(x,v);
smi[v] += sm[x];
if(mx < sm[x]) mx = sm[x],mp = x;
}
sm[v] = smi[v]+a[v];
if((mx<<1) > sm[v]){
ans += (sm[v]-mx)<<1;
if(mp == v) tp[v] = 1;
else smi[v] -= sm[mp],ch[v][1] = mp;
}
else tp[v] = 2,ans += sm[v]-1;
}
inline void access(int x,int w){ // add w
for(int y = 0;x;x = f[y = x]){// 这里的y的意思就是这次加法操作是从哪个虚子树加上来的
splay(x);
LL S = sm[x]-sm[lc];//子树和
if(tp[x] == 0) ans -= (S-sm[rc])<<1;//这里rc就不会有lc了
else if(tp[x] == 1) ans -= (S-a[x])<<1;
else ans -= S-1;
S += w;sm[x] += w;
if(y) smi[x] += w;
else a[x] += w;
if(sm[y]<<1 > S) smi[x] += sm[rc]-sm[y],rc = y;
if(sm[rc]<<1 > S) tp[x] = 0,ans += (S-sm[rc])<<1; // 子树过大
else{
if(rc) smi[x] += sm[rc],rc = 0;//实->虚
if(a[x]<<1 > S) tp[x] = 1,ans += (S-a[x])<<1;
else tp[x] = 2,ans += S-1;
}
}
}
int n,m;
int main(){
scanf("%d%d",&n,&m);
FOR(i,1,n) scanf("%lld",a+i);
FOR(i,2,n){
int u,v;scanf("%d%d",&u,&v);G[u].pb(v);G[v].pb(u);
}
dfs(1);printf("%lld\n",ans);
FOR(i,1,m){
int x,w;scanf("%d%d",&x,&w);
access(x,w);printf("%lld\n",ans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 10
Accepted
time: 1ms
memory: 15292kb
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: 124ms
memory: 32528kb
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: 137ms
memory: 32536kb
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: 81ms
memory: 28276kb
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: 73ms
memory: 28480kb
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: 153ms
memory: 28304kb
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: 163ms
memory: 28308kb
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: 161ms
memory: 28332kb
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: 701ms
memory: 49356kb
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: 624ms
memory: 49304kb
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