LRIP UVALive - 7148 (点分治)

大意: 给定树, 每个点有点权, 求最长非减树链, 满足树链上最大值与最小值之差不超过D

 

 

点分治, 线段树维护最小值为$x$时的最长非增和非减树链即可.

实现时有技巧是翻转一下儿子区间, 这样可以只维护一种树链, 时间复杂度$O(nlog^2n)$, 空间复杂度$O(n)$.

要注意使用线段树要注意每次清空, 不清空的话内存会卡到$O(nlog^2n)$

看其他dala0的题解好像说可以用set来维护, 但不是很懂要怎么用set区间更新最值.

 

#include <iostream>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <string.h>
#include <queue>
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define pb push_back
#define mid ((l+r)>>1)
#define lc tr[o].l
#define rc tr[o].r
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
using namespace std;

const int N = 1e5+10, INF = 0x3f3f3f3f;
int n, rt, sum, ans, D, tot, T;
int a[N], vis[N];
int mx[N], sz[N];
vector<int> g[N];
struct {
	int l,r,mx;
} tr[N<<2];

void getrt(int x, int fa) {
	mx[x]=0, sz[x]=1;
	for (int y:g[x]) if (!vis[y]&&y!=fa) {
		getrt(y,x);sz[x]+=sz[y];
		mx[x]=max(mx[x],sz[y]);
	}
	mx[x]=max(mx[x],sum-sz[x]);
	if (mx[rt]>mx[x]) rt=x;
}

void query(int o, int l, int r, int ql, int qr, int c) {
	if (!o) return;
	if (ql<=l&&r<=qr) return ans = max(ans, tr[o].mx+c),void();
	if (mid>=ql) query(ls,ql,qr,c);
	if (mid<qr) query(rs,ql,qr,c);
}
void update(int &o, int l, int r, int x, int v) {
	if (!o) o=++tot;
	tr[o].mx = max(tr[o].mx, v);
	if (l==r) return;
	if (mid>=x) update(ls,x,v);
	else update(rs,x,v);
}

void dfs_lis(int x, int fa, int d) {
	if (a[x]<a[fa]) return;
	query(T,1,N,a[x]-D,a[x],1+d);
	for (int y:g[x]) if (!vis[y]&&y!=fa) {
		dfs_lis(y,x,d+1);
	}
}
void dfs_lds(int x, int fa, int d) {
	if (a[x]>a[fa]) return;
	update(T,1,N,a[x],d);
	for (int y:g[x]) if (!vis[y]&&y!=fa) {
		dfs_lds(y,x,d+1);
	}
}
void calc(int x, vector<int> &g) {
	while (tot) tr[tot].l=tr[tot].r=tr[tot].mx=0,--tot;
	T = 0;
	update(T,1,N,a[x],0);
	//因为题目要求非减, 一条链可能既是LIS又是LDS
	//这里先用LIS更新答案, 再更新LDS的值, 防止一条链同时处理两次
	//同时为了防止全部都为LDS的情况, 最后再用LDS更新一次答案
	//但是实际上本题数据比较水, 很多情况不考虑也能过
	for (int y:g) {
		dfs_lis(y,x,1);
		dfs_lds(y,x,1);
	}
	query(T,1,N,a[x]-D,a[x],1);
}
void solve(int x) {
	vis[x] = 1;
	vector<int> son;
	for (int y:g[x]) son.pb(y);
	calc(x,son);
	reverse(son.begin(),son.end());
	calc(x,son);
	for (int y:g[x]) if (!vis[y]) {
		mx[rt=0]=n,sum=sz[y];
		getrt(y,0),solve(rt);
	}
}

void work() {
	scanf("%d%d", &n, &D);
	REP(i,1,n) scanf("%d", a+i);
	REP(i,1,n) g[i].clear(),vis[i]=0;
	REP(i,2,n) {
		int u, v;
		scanf("%d%d", &u, &v);
		g[u].pb(v),g[v].pb(u);
	}
	sum=mx[0]=n, getrt(1,0), solve(rt);
}
int main() {
	int t;
	scanf("%d", &t);
	REP(i,1,t) {
		ans = 1;
		work();
		printf("Case #%d: %d\n", i, ans);
	}
}

 

转载于:https://www.cnblogs.com/uid001/p/10446067.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值