UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#204754#3620. AOccDreamer10014699ms125860kbC++114.9kb2024-06-09 09:34:542024-06-09 13:11:29

answer

//OccDreamer
#include<bits/stdc++.h>

#define vc vector
#define db double
#define fi first
#define se second
#define ll long long
#define mk make_pair
#define pb push_back
#define RI register int
#define PI pair<int,int>
#define ull unsigned long long
#define err cerr << "   -_-   " << endl
#define debug cerr << " ------------------- " << endl

#define input(x) freopen(#x".in","r",stdin)
#define output(x) freopen(#x".out","w",stdout)

#define NO puts("No")
#define YES puts("Yes")

//#define OccDreamer
//#define int long long

using namespace std;

namespace IO{
	inline int read(){
		int X=0, W=0; char ch=getchar();
		while(!isdigit(ch)) W|=ch=='-', ch=getchar();
		while(isdigit(ch)) X=(X<<1)+(X<<3)+(ch^48), ch=getchar();
		return W?-X:X;
	}
	inline void write(int x){
		if(x<0) x=-x, putchar('-');
		if(x>9) write(x/10);
		putchar(x%10+'0');
	}
	inline void sprint(int x){write(x), putchar(32);}
	inline void eprint(int x){write(x), putchar(10);}
}using namespace IO;

const int MAXN = 5e5+5;
const int mod = 998244353;

int anc[20][MAXN];
int mark[MAXN], son[MAXN], id[MAXN], tot;
int n, m, de[MAXN], topf[MAXN], siz[MAXN];
int head[MAXN], ne[MAXN<<1], to[MAXN<<1], fa[MAXN], cnt;

ll tag[MAXN<<2], tree[MAXN<<2];

vc<int> fail;
set<int> node[MAXN];

struct SGT{
	int minn[MAXN<<2], pos[MAXN<<2], tag[MAXN<<2]; 	
	inline void pushup(int p){
		minn[p]=min(minn[p<<1],minn[p<<1|1]);
		pos[p]=(minn[p]==minn[p<<1|1]?pos[p<<1|1]:pos[p<<1]);
		return ;
	}
	inline void pushdown(int p){
		tag[p<<1]+=tag[p];
		tag[p<<1|1]+=tag[p];
		minn[p<<1]+=tag[p];
		minn[p<<1|1]+=tag[p];
		return tag[p]=0, void();	
	}
	inline void build(int p, int l, int r){
		if(l==r) return minn[p]=de[mark[l]], pos[p]=l, void();
		int mid=(l+r)>>1;
		build(p<<1,l,mid); build(p<<1|1,mid+1,r);
		return pushup(p);	
	}
	inline void add(int p, int l, int r, int L, int R, int v){
		if(L<=l && r<=R) return minn[p]+=v, tag[p]+=v, void();
		int mid=(l+r)>>1; pushdown(p);
		if(L<=mid) add(p<<1,l,mid,L,R,v);
		if(R>mid) add(p<<1|1,mid+1,r,L,R,v);
		return pushup(p);
	}
}T;

struct BIT{
	ll tree[MAXN];
	inline int lowbit(int x){return x & -x;}
	inline void add(int x, ll v){while(x<=n) tree[x]+=v, x+=lowbit(x);}
	inline ll ask(int x){ll s=0; while(x) s+=tree[x], x^=lowbit(x); return s;}	
}t0, t1;

inline void add(int x, int y){++cnt;to[cnt]=y;ne[cnt]=head[x];head[x]=cnt;}

inline ll query(int x){return 1ll*(x+1)*t0.ask(x)-t1.ask(x);}

inline void add(int l, int r, int v){
	t0.add(l,v); t0.add(r+1,-v);
	t1.add(l,1ll*l*v); t1.add(r+1,-1ll*(r+1)*v);
	return ;
}

inline ll ask(int l, int r){return query(r)-query(l-1);}

inline void dfs(int x, int f){
	anc[0][x]=f; siz[x]=1;
	fa[x]=f; de[x]=de[f]+1; son[x]=0;
	for(int i=1;(1<<i)<=de[x];++i) anc[i][x]=anc[i-1][anc[i-1][x]];
	for(int i=head[x];i;i=ne[i]){
		if(to[i]==f) continue;
		dfs(to[i],x); siz[x]+=siz[to[i]];
		if(siz[to[i]]>siz[son[x]]) son[x]=to[i];
	}
	return ;
}

inline void dfs2(int x, int t){
	id[x]=++tot; topf[x]=t; mark[tot]=x;
	if(!son[x]) return ;
	dfs2(son[x],t);	
	for(int i=head[x];i;i=ne[i])
		if(to[i]!=fa[x] && to[i]!=son[x]) dfs2(to[i],to[i]);
	return ;
}

inline ll subsum(int x){return x==0?0:ask(id[x],id[x]+siz[x]-1);}

inline void upd(int x){
	int t=fa[x], y=son[t];
	if(subsum(x)>subsum(y)){
		son[t]=x; 
		T.add(1,1,n,id[y],id[y]+siz[y]-1,1);
		T.add(1,1,n,id[x],id[x]+siz[x]-1,-1);
		node[topf[y]].insert(id[y]);
		return ;
	}
	fail.pb(id[x]);
	return ;
}

inline void adjust(int x){
	while(x){
		int o=topf[x]; fail.clear();
		while(node[o].size() && *node[o].begin()<=id[x]) upd(mark[*node[o].begin()]), node[o].erase(node[o].begin());
		for(auto i:fail) node[o].insert(i); x=fa[o];
	}
	return ;
}

inline void Solve(){
	int p=mark[T.pos[1]];
	for(int i=19;i>=0;--i){
		if(anc[i][p]==0) continue;
		int o=anc[i][p];
		if(subsum(son[o])<=subsum(1)-subsum(o)) p=o;	
	}
	pair<ll,int> ans=mk(max(subsum(son[p]),subsum(1)-subsum(p)),id[p]);
	if(p!=1){
		p=fa[p];
		ans=min(ans,mk(max(subsum(son[p]),subsum(1)-subsum(p)),id[p]));	
	}
	eprint(mark[ans.se]);
}

signed main(){
	n=read(), m=read();
	for(int i=2;i<=n;++i){
		int x, y;
		x=read(), y=read();
		add(x,y), add(y,x);	
	}
	int op, x, y, z, a, b;
	dfs(1,0); dfs2(1,1); T.build(1,1,n);
	for(int i=2;i<=n;++i){
		if(son[fa[i]]!=i) node[topf[i]].insert(id[i]);
		else T.add(1,1,n,id[i],id[i]+siz[i]-1,-1);	
	}
	while(m--){
		op=read();
		if(op==1) x=read(), z=read(), add(id[x],id[x]+siz[x]-1,z), adjust(x);
		else{
			a=x=read(), b=y=read(), z=read();	
			while(topf[x]!=topf[y]){
				if(de[topf[x]]<de[topf[y]]) swap(x,y);
				add(id[topf[x]],id[x],z);
				x=fa[topf[x]];	
			}
			if(id[x]>id[y]) swap(x,y);
			add(id[x],id[y],z); adjust(a); adjust(b);
		}
		Solve();
	}
	return 0;
}



















































详细

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 6ms
memory: 25188kb

input:

1631 1695
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...

output:

1303
721
688
701
682
737
588
628
713
769
774
780
862
866
871
886
909
929
937
943
941
946
945
954
949...

result:

ok 1695 lines

Test #2:

score: 0
Accepted
time: 12ms
memory: 25540kb

input:

1724 1881
2 1
3 2
4 2
5 2
6 1
7 6
8 2
9 8
10 6
11 10
12 1
13 1
14 12
15 7
16 3
17 14
18 13
19 4
20 8...

output:

910
910
910
910
910
910
910
910
910
910
910
910
826
826
826
826
826
826
1006
1006
1006
1006
1006
100...

result:

ok 1881 lines

Test #3:

score: 0
Accepted
time: 4ms
memory: 24984kb

input:

1935 1603
2 1
3 2
4 3
5 1
6 1
7 2
8 6
9 1
10 1
11 8
12 11
13 10
14 4
15 2
16 9
17 8
18 14
19 14
20 7...

output:

14
2
10
1
1
1
1
1
1
1
1
1
1
1
1
2
2
2
2
1
1
1
1
1
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
1
1
1
1
1
1
1
...

result:

ok 1603 lines

Test #4:

score: 0
Accepted
time: 0ms
memory: 24980kb

input:

1782 1942
2 1
3 1
4 2
5 4
6 1
7 1
8 1
9 7
10 4
11 5
12 10
13 4
14 7
15 14
16 15
17 3
18 5
19 16
20 1...

output:

14
7
7
7
4
4
4
4
4
1
1
7
1
4
4
4
4
4
4
2
1
1
4
2
2
2
2
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1...

result:

ok 1942 lines

Subtask #2:

score: 20
Accepted

Test #5:

score: 20
Accepted
time: 1253ms
memory: 121836kb

input:

281495 252939
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
1...

output:

213578
210748
135169
135026
144651
132400
134957
124141
118451
118781
137612
154321
170613
166113
17...

result:

ok 252939 lines

Test #6:

score: 0
Accepted
time: 760ms
memory: 119444kb

input:

273746 287587
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
1...

output:

189163
189918
190785
185592
183371
184857
188261
186057
186098
184608
193692
178813
178667
179002
17...

result:

ok 287587 lines

Test #7:

score: 0
Accepted
time: 801ms
memory: 105312kb

input:

247712 269003
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
1...

output:

176247
185709
162271
169277
173592
171721
153484
155588
162688
163154
161122
160221
154947
154853
14...

result:

ok 269003 lines

Test #8:

score: 0
Accepted
time: 915ms
memory: 125860kb

input:

294554 246011
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
1...

output:

225125
191067
199412
203257
197830
189818
184048
184192
183424
180181
180167
185337
187207
185668
18...

result:

ok 246011 lines

Subtask #3:

score: 30
Accepted

Test #9:

score: 30
Accepted
time: 1022ms
memory: 123420kb

input:

286640 284270
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
1...

output:

257488
195036
211947
235498
241668
232198
232766
228649
228553
208960
201281
202100
202317
203208
20...

result:

ok 284270 lines

Test #10:

score: 0
Accepted
time: 1305ms
memory: 61156kb

input:

260280 269761
2 1
3 1
4 2
5 4
6 5
7 2
8 3
9 6
10 7
11 1
12 7
13 3
14 9
15 3
16 11
17 13
18 2
19 14
2...

output:

180457
180457
171997
177266
159775
159174
159174
159174
232429
232429
146084
146084
146084
146084
14...

result:

ok 269761 lines

Test #11:

score: 0
Accepted
time: 1312ms
memory: 68676kb

input:

299939 243207
2 1
3 1
4 2
5 1
6 5
7 5
8 1
9 3
10 8
11 1
12 9
13 8
14 11
15 5
16 13
17 11
18 14
19 16...

output:

289736
289736
8
5
1
1
1
1
1
1
9190
9190
9190
9190
9190
9190
9190
9190
9190
9190
9190
9190
9190
9190
...

result:

ok 243207 lines

Test #12:

score: 0
Accepted
time: 938ms
memory: 58368kb

input:

260657 248813
2 1
3 1
4 1
5 4
6 5
7 1
8 5
9 5
10 5
11 1
12 9
13 11
14 6
15 12
16 10
17 12
18 16
19 1...

output:

192653
115832
172896
11
5
190178
32264
32264
9
9
9
9
12
12
12
12
3992
3992
3992
3992
3992
3992
3992
...

result:

ok 248813 lines

Subtask #4:

score: 40
Accepted

Test #13:

score: 40
Accepted
time: 784ms
memory: 103360kb

input:

241289 290298
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
1...

output:

198749
185755
175529
192379
192636
183414
183546
173058
171195
154762
156345
166238
164292
163728
15...

result:

ok 290298 lines

Test #14:

score: 0
Accepted
time: 1850ms
memory: 71512kb

input:

295796 290103
2 1
3 1
4 2
5 1
6 1
7 6
8 4
9 8
10 2
11 5
12 4
13 4
14 3
15 12
16 2
17 10
18 1
19 14
2...

output:

70561
125326
125326
133092
125326
125326
125326
125326
125326
125326
125326
133092
133092
133092
125...

result:

ok 290103 lines

Test #15:

score: 0
Accepted
time: 2107ms
memory: 65352kb

input:

268550 291961
2 1
3 2
4 1
5 1
6 3
7 3
8 3
9 3
10 2
11 4
12 6
13 12
14 12
15 3
16 1
17 14
18 6
19 1
2...

output:

223475
1
2
2
2
4435
4435
4435
4435
2716
2716
2716
2716
2716
2716
2716
2716
2716
2716
2716
2716
2716
...

result:

ok 291961 lines

Test #16:

score: 0
Accepted
time: 1630ms
memory: 67304kb

input:

287030 275087
2 1
3 1
4 1
5 4
6 3
7 6
8 6
9 3
10 5
11 9
12 8
13 1
14 4
15 10
16 1
17 5
18 11
19 12
2...

output:

18
9
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
712...

result:

ok 275087 lines

Extra Test:

score: 0
Extra Test Passed