UVALive 7148 LRIP
LRIP
Time Limit: 10000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

解题:树分治
参考了Oyking大神的解法
我们用map<int,int>维护上升序列,first表示value,second表示长度,按first由小到大,second由大到小排列,因为在val相同的时候,当然是越长越好,但是,Oyking大神说过的冗余上升序列,意思就是在你的值比他小,长度也比它小,那么在拼接那个下降的序列的时候,就会导致极差大,所以可以删除这些没用的。
我们在搜索下降序列的时候,可以对map进行lower_bound,找出极差范围内最大的长度,进行合并即可
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct arc{
int to,next;
arc(int x = ,int y = -){
to = x;
next = y;
}
}e[maxn<<];
bool vis[maxn];
int head[maxn],val[maxn],D,ret,tot;
int sz[maxn],maxson[maxn];
map<int,int>up;
void add(int u,int v){
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
void dfs(int u,int fa){
sz[u] = ;
maxson[u] = ;
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].to == fa || vis[e[i].to]) continue;
dfs(e[i].to,u);
sz[u] += sz[e[i].to];
maxson[u] = max(maxson[u],sz[e[i].to]);
}
}
int FindRoot(int sum,int u,int fa){
int ret = u;
maxson[u] = max(maxson[u],sum - sz[u]);
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].to == fa || vis[e[i].to]) continue;
int x = FindRoot(sum,e[i].to,u);
if(maxson[x] < maxson[ret]) ret = x;
}
return ret;
}
void dfs_down(int u,int fa,int len){
auto it = up.lower_bound(val[u] - D);
if(it != up.end()) ret = max(ret,it->second + + len);
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].to == fa || vis[e[i].to] || val[e[i].to] < val[u]) continue;
dfs_down(e[i].to,u,len + );
}
}
void insert(int val,int len){
auto x = up.lower_bound(val);
if(x != up.end() && x->second >= len) return;
auto ed = up.upper_bound(val);
auto it = map<int,int>::reverse_iterator(ed);
while(it != up.rend() && it->second <= len) ++it;
up.erase(it.base(),ed);
up[val] = len;
}
void dfs_up(int u,int fa,int len){
insert(val[u],len);
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].to == fa || vis[e[i].to] || val[e[i].to] > val[u]) continue;
dfs_up(e[i].to,u,len + );
}
}
void work(int u,vector<int>&son){
up.clear();
up[val[u]] = ;
for(int v:son){
if(val[v] >= val[u]) dfs_down(v,,);
if(val[v] <= val[u]) dfs_up(v,,);
}
}
void solve(int u){
dfs(u,);
int root = FindRoot(sz[u],u,);
vis[root] = true;
vector<int>son;
for(int i = head[root]; ~i; i = e[i].next)
if(!vis[e[i].to]) son.push_back(e[i].to);
work(root,son);
reverse(son.begin(),son.end());
work(root,son);
for(int i = head[root]; ~i; i = e[i].next)
if(!vis[e[i].to]) solve(e[i].to);
}
int main(){
int kase,n,u,v,cs = ;
scanf("%d",&kase);
while(kase--){
scanf("%d %d",&n,&D);
memset(head,-,sizeof head);
memset(vis,false,sizeof vis);
tot = ;
for(int i = ; i <= n; ++i)
scanf("%d",val + i);
for(int i = ret = ; i < n; ++i){
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
solve();
printf("Case #%d: %d\n",cs++,ret);
}
return ;
}
UVALive 7148 LRIP的更多相关文章
- UVALive 7148 LRIP(树的分治+STL)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive 7148 LRIP【树分治+线段树】
题意就是要求一棵树上的最长不下降序列,同时不下降序列的最小值与最大值不超过D. 做法是树分治+线段树,假设树根是x,y是其当前需要处理的子树,对于子树y,需要处理出两个数组MN,MX,MN[i]表示以 ...
- UVALive 7148 LRIP 14年上海区域赛K题 树分治
题意 n个点组成一棵树, 带有点权. 求最长不降的路径的长度, 且路径上最大值最小值之差不超过D. 显然是树分治, 但是分治之后如何维护答案呢. 假设当前重心为g, 分别记录g出发不降路径的长度,以及 ...
- LRIP UVALive - 7148 (点分治)
大意: 给定树, 每个点有点权, 求最长非减树链, 满足树链上最大值与最小值之差不超过D 点分治, 线段树维护最小值为$x$时的最长非增和非减树链即可. 实现时有技巧是翻转一下儿子区间, 这样可以只维 ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
随机推荐
- 转 python 将一个文件中内容添加到另一个文件指定位置
http://blog.csdn.net/huguangshanse00/article/details/14624601
- JavaScript入门2
5.document对象:Document对象是window对象的一个对象属性,代表浏览器窗口中装载的整个HTML文档.文档中的每个HTML元素对应着JavaScript对象. 因为document代 ...
- IO 优化
转自 BlackJack_ #define fastcall __attribute__((optimize("-O3"))) #define IL __inline__ __at ...
- 使用vbScript 链接SQLserver数据库和基础操作
使用vbs链接SQLserver数据库 数据库的创建.设计使用 management studio完成 1.本地链接数据库 set oCon = server.createObject("a ...
- Android手机app耗电量测试工具 - Gsam Battery Monitor
这段时间需要测试一个Android手机app的耗电量,在网上找了一个工具,Gsam Battery Monitor,觉得挺好用,和大家分享一下. 安装app后打开,可以看到主界面是这样的 点击一下上图 ...
- PMP项目管理学习笔记(6)——整合管理之制订项目管理计划
制订项目管理计划 输入:项目章程.组织过程资产.企业环境要素.计划过程的输出(): 工具:专家判断 输出:项目管理计划 项目管理计划使你在问题发生之前做出规划 你要在计划过程组中明确如何完成项目——因 ...
- [Java 8] (9) Lambda表达式对递归的优化(下) - 使用备忘录模式(Memoization Pattern) .
使用备忘录模式(Memoization Pattern)提高性能 这个模式说白了,就是将需要进行大量计算的结果缓存起来,然后在下次需要的时候直接取得就好了.因此,底层只需要使用一个Map就够了. 但是 ...
- qt 设置阴影 不显示黑色边框
this->setAttribute(Qt::WA_TranslucentBackground);
- Android(java)学习笔记174:服务(service)之混合方式开启服务
1. 前面我们已经讲过可以使用两种方式开启服务 startService----stopService: oncreate() ---> onstartCommand() ---& ...
- HashSet LinkedHashSet TreeSet 分析
1.HashSet分析 hashset 底层是hash表,就是hashMap,是无序的,唯一的.也就是说,它的底层其实就是一个HashMap key 值的组成值.所以具有唯一性. public Ha ...