QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#42835#2305. 历史qiaozh100 ✓958ms124212kbC++114.2kb2022-08-04 10:52:032022-08-04 10:52:05

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-08-04 10:52:05]
  • 评测
  • 测评结果:100
  • 用时:958ms
  • 内存:124212kb
  • [2022-08-04 10:52:03]
  • 提交

answer

#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
inline int read()
{
    char ch=getchar();int s=0,w=1;
    while(ch<48||ch>57){if(ch=='-')w=-1;ch=getchar();}
    while(ch>=48&&ch<=57){s=(s<<1)+(s<<3)+ch-48;ch=getchar();}
    return s*w;
}
inline void write(ll x)
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+48);
}
ll n,m;
vector<ll> G[400010];
struct info {
	ll top,siz,fa,asiz,rak,dep,dfn,son,h,inp,ans;
} f[400010];
ll dfn_ind=0,Ans=0;
class CMP { public:bool operator()(ll ot1,ll ot2) {
	return ot1>ot2;
}};
set<ll,CMP> tre[400010];
void dfs1(ll node,ll last) {
	f[node].siz=1;f[node].fa=last;f[node].son=0;f[node].dep=f[last].dep+1;
	f[node].asiz=f[node].inp;
	for(int i=0;i<G[node].size();i++) {
		if(G[node][i]==last) continue;
		dfs1(G[node][i],node);
		f[node].siz+=f[G[node][i]].siz;f[node].asiz+=f[G[node][i]].asiz;
		if(f[G[node][i]].siz>f[f[node].son].siz) f[node].son=G[node][i];
	}
}
void dfs2(ll node,ll last,ll ctop) {
	f[node].dfn=++dfn_ind;f[node].top=ctop;f[f[node].dfn].rak=node;
	ll mx=f[node].inp,lind=0;
	if(f[node].son!=0) {
		dfs2(f[node].son,node,ctop);
		if(f[f[node].son].asiz>mx) mx=f[f[node].son].asiz,lind=f[node].son;
	}
	for(int i=0;i<G[node].size();i++) {
		if(G[node][i]==last||G[node][i]==f[node].son) continue;
		dfs2(G[node][i],node,G[node][i]);tre[G[node][i]].insert(0);
		if(f[G[node][i]].asiz>mx) mx=f[G[node][i]].asiz,lind=G[node][i];
	}
	if(f[node].asiz*2<=f[f[node].fa].asiz&&node!=1) tre[f[node].top].insert(f[node].dfn);
	if(mx*2>f[node].asiz) {
		Ans+=(f[node].ans=2*(f[node].asiz-mx));
		if(mx==f[node].inp) f[node].h=node;
		else f[node].h=lind;
	} else Ans+=(f[node].ans=f[node].asiz-1);
}
struct BIT {
	ll va[400010],col[400010],ti[400010],la[400010];
	void init() {
		for(int i=1;i<=n;i++) va[i]=0;
		for(int i=1;i<=n;i++) col[i]=f[f[i].rak].top;
		ti[1]=1;
		for(int i=2;i<=n;i++) {
			if(col[i]==col[i-1]) ti[i]=ti[i-1];
			else ti[i]=i;
		}
		la[n]=n;
		for(int i=n-1;i>=1;i--) {
			if(col[i]==col[i+1]) la[i]=la[i+1];
			else la[i]=i;
		}
		col[0]=-1;
	}
	inline void cun(ll ind,ll k) { 
		ll tu=ti[ind];
		for(int i=ind-tu+1;i>=1;i-=(i&-i)) va[i+tu-1]+=k;
	}
	inline ll cha(ll ind) {
		ll ret=0,tu=ti[ind],lu=la[ind];
		for(int i=ind-tu+1;i+tu-1<=lu;i+=(i&-i)) ret+=va[i+tu-1];
		return ret;
	}
} btre;
inline void prealter(ll node,ll k) {
	f[node].inp+=k;
	while(node!=0) {
		btre.cun(f[node].dfn,k);
//		if(node) btre.cun(f[f[node].top].dfn-1,-k);
		node=f[f[node].top].fa;
	}
}
inline void update(ll node,ll ch,ll k,ll jinp) {
	Ans-=f[node].ans;
	ll A=btre.cha(f[node].dfn),B=btre.cha(f[f[node].h].dfn),C=btre.cha(f[ch].dfn);
	if(f[node].h!=0&&f[node].h!=node) {
		if(B*2<=A) tre[f[f[node].h].top].insert(f[f[node].h].dfn),f[node].h=0;
	} else if(f[node].h==node) {
		if(f[node].inp*2<=A) f[node].h=0;
	}
	if(C*2>A) {
		if(f[node].h!=ch) tre[f[ch].top].erase(f[ch].dfn),f[node].h=ch,B=btre.cha(f[f[node].h].dfn);
	} else if(f[node].inp*2>A) f[node].h=node;
	if(f[node].h!=0&&f[node].h!=node) {
		Ans+=(f[node].ans=2*(A-B));
	} else if(f[node].h==node) {
		Ans+=(f[node].ans=2*(A-f[node].inp));
	} else Ans+=(f[node].ans=A-1);
}
void alter(ll node,ll k) {
	ll fn=f[node].top,cur,fath;
	if(f[node].son) update(node,f[node].son,k,k);
	while(node!=0) {
		if(f[node].fa&&f[f[node].fa].h!=node) update(f[node].fa,node,k,0);
		cur=f[node].dfn;
		cur=(*tre[fn].upper_bound(cur));
		while(cur>=f[fn].dfn) {
			node=f[cur].rak;fath=f[node].fa;
			update(fath,node,k,0);
			cur=(*tre[fn].upper_bound(cur));
		}
		node=f[fn].fa;fn=f[node].top;
	}
}
int main() {
//	freopen("ex_history2.in","r",stdin);
//	freopen("pu.out","w",stdout);
	n=read();m=read();
	for(int i=1;i<=n;i++) f[i].inp=read();
	ll u,v;
	for(int i=1;i<=n-1;i++) {
		u=read();v=read();
		G[u].push_back(v);G[v].push_back(u);
	}
	dfs1(1,0);dfs2(1,0,1);btre.init();tre[1].insert(0);
	for(int i=1;i<=n;i++) {
		btre.cun(f[i].dfn,f[i].asiz);
		if(i!=f[i].top) btre.cun(f[i].dfn-1,-f[i].asiz);
	}
	write(Ans);putchar('\n');
	for(int i=1;i<=m;i++) {
		u=read();v=read();prealter(u,v);
		alter(u,v);write(Ans);putchar('\n');
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 10
Accepted
time: 2ms
memory: 37924kb

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: 112ms
memory: 69188kb

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: 115ms
memory: 68252kb

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: 98ms
memory: 69260kb

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: 115ms
memory: 69328kb

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: 228ms
memory: 66816kb

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: 207ms
memory: 70512kb

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: 239ms
memory: 66924kb

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: 873ms
memory: 122532kb

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: 958ms
memory: 124212kb

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