开o2全re,不开o2ac????
查看原帖
开o2全re,不开o2ac????
966353
super_zzr楼主2023/10/28 20:55

题目传送门

我不理解,下面是代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<cstdio>
#define bug cout<<"-bug-"<<endl
#define endl "\n"
#define mem(a,b) memset(a,b,sizeof a)
using namespace std;
void close(){
	 ios::sync_with_stdio(false);
	 cin.tie(0);cout.tie(0);
}
int maxx=-1e9,df=0,n,m,a[100005],top[100005],fa[100005],son[100005],sz[100005],deep[100005],dfn[100005],tot=0,head[100005];
struct node{
	int to,next,sum;
}tu[100005];
struct nnode{
	int l,r,sum=0,lazy;
}tree[400005];
void add(int x,int y){
	tu[++tot].to=y;
	tu[tot].next=head[x];
	head[x]=tot;
}
void dfs1(int x,int f){
	fa[x]=f;
	sz[x]=1;
	deep[x]=deep[f]+1;
	int bigson=-1e9;
	for(int i=head[x];i;i=tu[i].next){
		int v=tu[i].to;
		if(f==v) continue;
		dfs1(v,x);
		sz[x]+=sz[v];
		if(sz[v]>bigson){
			bigson=sz[v];
			son[x]=v;
		}
	}
}
void dfs2(int x,int bg){
	dfn[x]=++df;
	top[x]=bg;
	if(son[x]==0) return ;
	dfs2(son[x],bg);
	for(int i=head[x];i;i=tu[i].next){
		int v=tu[i].to;
		if(v==fa[x]||v==son[x]) continue;
		dfs2(v,v);
	}
}
void build(int root,int l,int r){
	tree[root].l=l;
	tree[root].r=r;
	if(l==r) return ;
	int mid=(l+r)>>1;
	build(root*2,l,mid);
	build(root*2+1,mid+1,r);
}
void spread(int root){
	if(tree[root].lazy){
		int l=tree[root].lazy;
		tree[root*2].sum+=(tree[root*2].r-tree[root*2].l+1)*l;
		tree[root*2].lazy+=l;
		tree[root*2+1].sum+=(tree[root*2+1].r-tree[root*2+1].l+1)*l;
		tree[root*2+1].lazy+=l;
		tree[root].lazy=0; 
	}
}
void pushup(int root){
	tree[root].sum=tree[root*2].sum+tree[root*2+1].sum;
}
void update(int root,int l,int r){
	if(l<=tree[root].l&&r>=tree[root].r){
		tree[root].sum+=(tree[root].r-tree[root].l+1);
		tree[root].lazy+=1;
		return ; 
	}
	spread(root);
	int mid=(tree[root].l+tree[root].r)>>1;
	if(l<=mid) update(root*2,l,r);
	if(r>mid) update(root*2+1,l,r);
	pushup(root);
}
int query(int root,int l,int r){
	if(tree[root].l==l&&tree[root].r==r) return tree[root].sum;
	spread(root);
	int mid=(tree[root].l+tree[root].r)>>1;
	if(l<=mid) query(root*2,l,r);
	else query(root*2+1,l,r);
}
void find_update(int x,int y){
	while(top[x]!=top[y]){
		if(deep[top[x]]<deep[top[y]]) swap(x,y);
		update(1,dfn[x],dfn[top[x]]);
		x=fa[top[x]];
	}
	if(deep[x]<deep[y]) swap(x,y);
	update(1,dfn[y],dfn[x]);
}
int main(){
	close();
	cin>>n>>m;
	for(int i=1;i<n;i++){
		int x,y;
		cin>>x>>y;
		add(x,y);
		add(y,x);
	}
	dfs1(1,0);
	dfs2(1,1);
	build(1,1,n);
	while(m--){
		int x,y;
		cin>>x>>y;
		find_update(x,y);
	}
	for(int i=1;i<=n;i++){
		maxx=max(maxx,query(1,i,i));
	}
	cout<<maxx;
	return 0;
}
2023/10/28 20:55
加载中...