样例2过不了求条
查看原帖
样例2过不了求条
999274
CNS_5t0_0r2楼主2024/7/29 08:20
#include<bits/stdc++.h>
#define mid ((l + r) >> 1)
#define lchild (root << 1)
#define rchild ((root << 1) + 1)
using namespace std;
const int N = 5e4 + 9,INF = 2e9 + 9;

int n,q;
int a[N];

struct edge{
	int to,nex;
} e[N << 1];
int head[N],ecnt;
void addedge(int u,int v){
	ecnt++;
	e[ecnt] = (edge){v,head[u]};
	head[u] = ecnt;
}

int fa[N],dep[N];
int siz[N],w_ch[N];
void dfs1(int u,int father){
    fa[u] = father;
    siz[u] = 1;
    dep[u] = dep[father] + 1;
    for(int i = head[u];i;i = e[i].nex){
        int v = e[i].to;
        if(v != father){
            dfs1(v,u);
            siz[u] += siz[v];
            if(siz[v] > siz[w_ch[u]])
                w_ch[u] = v;
        }
    }
}
int top[N];
int dfn[N],rdfn[N],id;
void dfs2(int u,int link_top){
	id++;
	dfn[u] = id;
	rdfn[id] = u;
    top[u] = link_top;
    if(w_ch[u]){
        dfs2(w_ch[u],link_top);
        for(int i = head[u];i;i = e[i].nex){
            int v = e[i].to;
            if(v != fa[u] && v != w_ch[u])
                dfs2(v,v);
        }
    }
}

//rec[0] 是 u 到 LCA 的链,rec[1] 是 LCA 到 v 的链 
vector<pair<int,int> > rec[2];
void split(int u,int v){//把操作的链提取出来 
	rec[0].clear();
	rec[1].clear();
	int type = 0;
	while(top[u] != top[v]) {
		if(dep[top[u]] < dep[top[v]]){
            swap(u,v);
            type ^= 1;//保证是与u/v对应的 
		}
		rec[type].push_back(make_pair(dfn[top[u]],dfn[u]));
        u = fa[top[u]];
	}
	if(dep[u] > dep[v])
		swap(u,v);
	else
		type ^= 1;
	rec[type].push_back(make_pair(dfn[u],dfn[v]));
	reverse(rec[1].begin(),rec[1].end());
}

struct node{
	int val[2];
	int Min,Max;
	int tag;
} t[N << 2];

bool in_range(int l,int r,int L,int R){//[l,r]是否在修改/查询区间[L,R]中 
	return L <= l && r <= R;
}
bool out_range(int l,int r,int L,int R){//[l,r]是否与修改/查询区间[L,R]无交集 
	return l > R || r < L;
}


void push_up(int root){
	t[root].val[0] = max(max(t[lchild].val[0],t[rchild].val[0]),t[lchild].Max - t[rchild].Min);
	t[root].val[1] = max(max(t[lchild].val[1],t[rchild].val[1]),t[rchild].Max - t[lchild].Min);
	t[root].Min = min(t[lchild].Min,t[rchild].Max);
	t[root].Max = max(t[lchild].Max,t[rchild].Max);
}

void make_tag(int root,int v){
	t[root].tag += v;
	t[root].Min += v;
	t[root].Max += v;
}
void push_down(int root,int l,int r){
	make_tag(lchild,t[root].tag);
	make_tag(rchild,t[root].tag);
	t[root].tag = 0;
}

void build(int root,int l,int r){
	if(l == r){//到达叶节点
		//t[root].val[0] = t[root].val[1] = 0;
		t[root].Min = t[root].Max = a[rdfn[l]];
		return;
	}
	build(lchild,l,mid);
	build(rchild,mid + 1,r);
	push_up(root);
}

//将区间 [L,R] 内的所有数加 v 
void update(int root,int l,int r,int L,int R,int v){//root是当前节点,l、r是当前节点表示的区间,L、R是操作区间,v是区间加的值(下同)。
	if(out_range(l,r,L,R))
		return;
	if(in_range(l,r,L,R)){
		make_tag(root,v);
		return;
	}
	push_down(root,l,r);
	update(lchild,l,mid,L,R,v);
	update(rchild,mid + 1,r,L,R,v);
	push_up(root);
}

//查询区间 [L,R] 中所有数的最小值 
int query_min(int root,int l,int r,int L,int R){
	if(out_range(l,r,L,R))
		return 0;
	if(in_range(l,r,L,R))
		return t[root].Min;
	push_down(root,l,r);
	return min(query_min(lchild,l,mid,L,R),query_min(rchild,mid + 1,r,L,R));
}
int query_max(int root,int l,int r,int L,int R){
	if(out_range(l,r,L,R))
		return 0;
	if(in_range(l,r,L,R))
		return t[root].Max;
	push_down(root,l,r);
	return max(query_max(lchild,l,mid,L,R),query_max(rchild,mid + 1,r,L,R));
}
node query_ans(int root,int l,int r,int L,int R,int type){
	node ret;
	if(out_range(l,r,L,R)){
		ret.val[type] = -INF;
		ret.Min = INF;
		ret.Max = -INF;
		return ret;
	}
	else if(in_range(l,r,L,R)){
		ret = t[root];
		return ret;
	}
	push_down(root,l,r);
	node lc = query_ans(lchild,l,mid,L,R,type),rc = query_ans(rchild,mid + 1,r,L,R,type);
	ret.val[type] = max(max(lc.val[type],rc.val[type]),
					(type == 0 ? lc.Max - rc.Min : rc.Max - lc.Min));
	ret.Min = min(lc.Min,rc.Min);
	ret.Max = max(lc.Max,rc.Max);
	return ret;
}

int query(int u,int v){
	split(u,v);
	int premax = 0,ret = 0;//前缀最大值,返回值 
	for(int i = 0;i <= 1;i++){
		for(int j = 0;j < rec[i].size();j++){
			int l = rec[i][j].first,r = rec[i][j].second;//链对应的区间
			if(l > r)
				swap(l,r);
			ret = max(ret,max(premax - query_min(1,1,n,l,r),query_ans(1,1,n,l,r,i).val[i]));
			premax = query_max(1,1,n,l,r);
		}
	}
	return ret;
}
void modify(int w){
	for(int i = 0;i <= 1;i++){
		for(int j = 0;j < rec[i].size();j++){
			int l = rec[i][j].first,r = rec[i][j].second;//链对应的区间
			if(l > r)
				swap(l,r);
			update(1,1,n,l,r,w);
		}
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n;
	for(int i = 1;i <= n;i++)
		cin >> a[i];
	for(int i = 1;i < n;i++){
		int u,v;
		cin >> u >> v;
		addedge(u,v);
		addedge(v,u);
	}
	dfs1(1,0);
	dfs2(1,1);
	build(1,1,n);
	cin >> q;
	while(q--){
		int u,v,w;
		cin >> u >> v >> w;
		cout << query(u,v) << '\n';
		modify(w);
	}
	return 0;
}
2024/7/29 08:20
加载中...