Codeforces 490F Treeland Tour(离散化 + 线段树合并)
题目链接 Treeland Tour
题目就是让你求树上LIS
先离散化,然后再线段树上操作。一些细节需要注意一下。
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) typedef long long LL; const int N = 200010; int root[N];
int ls[N * 40], rs[N * 40], lis[N * 40], lds[N * 40];
int ncnt, n, ans;
int ret = 0;
vector <int> v[N];
int val[N];
int sx[N];
int icnt = 0; void merge(int &x, int y){
if (!x || !y){
x = x + y;
return;
} lis[x] = max(lis[x], lis[y]);
lds[x] = max(lds[x], lds[y]);
ret = max(ret, max(lis[ls[x]] + lds[rs[y]], lds[rs[x]] + lis[ls[y]]));
merge(ls[x], ls[y]);
merge(rs[x], rs[y]);
} void modify(int &x, int l, int r, int t, int v, int *a){
if (!x) x = ++ncnt;
a[x] = max(a[x], v);
if (l == r) return;
int mid = (l + r) >> 1;
if (t <= mid) modify(ls[x], l, mid, t, v, a);
else modify(rs[x], mid + 1, r, t, v, a);
} int query(int x, int l, int r, int ql, int qr, int *a){
if (l > r) return 0;
if (!x) return 0;
if (ql <= l && r <= qr) return a[x];
int ret = 0, mid = (l + r) >> 1;
if (ql <= mid) ret = max(ret, query(ls[x], l, mid, ql, qr, a));
if (qr > mid) ret = max(ret, query(rs[x], mid + 1, r, ql, qr, a));
return ret;
} void dfs(int x, int fa){
for (auto u : v[x]){
if (u == fa) continue;
dfs(u, x);
} ret = 0;
int nlis = 0, nlds = 0, ilis, ilds;
for (auto u : v[x]){
if (u == fa) continue;
ilis = query(root[u], 1, icnt, 1, val[x] - 1, lis);
ilds = query(root[u], 1, icnt, val[x] + 1, icnt, lds);
merge(root[x], root[u]);
ans = max(ans, ilis + nlds + 1);
ans = max(ans, ilds + nlis + 1);
nlis = max(nlis, ilis);
nlds = max(nlds, ilds);
} ans = max(ans, ret);
modify(root[x], 1, icnt, val[x], nlis + 1, lis);
modify(root[x], 1, icnt, val[x], nlds + 1, lds);
} int main(){ scanf("%d", &n);
rep(i, 1, n){
scanf("%d", val + i);
sx[++icnt] = val[i];
} sort(sx + 1, sx + icnt + 1);
icnt = unique(sx + 1, sx + icnt + 1) - sx - 1;
rep(i, 1, n) val[i] = lower_bound(sx + 1, sx + icnt + 1, val[i]) - sx; rep(i, 2, n){
int x, y;
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
} dfs(1, 0);
printf("%d\n", ans);
return 0;
}
Codeforces 490F Treeland Tour(离散化 + 线段树合并)的更多相关文章
- Codeforces ECR47F Dominant Indices(线段树合并)
一个比较显然的做法:对每棵子树用线段树维护其中的深度,线段树合并即可. 本来想用这个题学一下dsu on tree,结果还是弃疗了. #include<iostream> #include ...
- Codeforces 490F Treeland Tour 树形dp
Treeland Tour 离散化之后, 每个节点维护上升链和下降链, 感觉复杂度有点高, 为啥跑这么快.. #include<bits/stdc++.h> #define LL long ...
- CodeForces - 1037H: Security(SAM+线段树合并)
题意:给定字符串S: Q次询问,每次询问给出(L,R,T),让你在S[L,R]里面找一个字典序最小的子串,其字典序比T大. 没有则输出-1: 思路:比T字典序大,而且要求字典最小,显然就是在T的尾巴 ...
- [Codeforces]817F. MEX Queries 离散化+线段树维护
[Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...
- Codeforces 490F. Treeland Tour 暴力+LIS
枚举根+dfs 它可以活 , 我不知道有什么解决的办法是积极的 ...... F. Treeland Tour time limit per test 5 seconds memory limit p ...
- Codeforces 490F Treeland Tour 树上的最长上升子序列
题目链接:点击打开链接 题意: 给定n个点的树. 以下n个数表示点权. 以下n-1行给出树. 找一条链,然后找出这条链中的点权组成的最长上升子序列. 求:最长上升子序列的长度. 思路: 首先是维护一条 ...
- CodeForces 19D Points(离散化+线段树+单点更新)
题目链接: huangjing 题意:给了三种操作 1:add(x,y)将这个点增加二维坐标系 2:remove(x,y)将这个点从二维坐标系移除. 3:find(x,y)就是找到在(x,y)右上方的 ...
- 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...
- [CF490F]Treeland Tour(线段树合并)
树上LIS:树上找一条简单路径的子序列使点权严格单增,最大化长度. 原题数据过小,用线段树合并可以做到$O(n\log n)$. 每个点用一棵线段树维护以每个权值为结尾的LIS最长长度,线段树合并时更 ...
随机推荐
- Linux远程传输命令scp
指令:scp在不同的linux主机间复制文件带有Security的文件copy,基于ssh登录. 有些linux发行版没有自带scp,因此需要安装scp# yum -y install openssh ...
- golang http 中间件
golang http 中间件 源码链接 golang的http中间件的实现 首先实现一个http的handler接口 type Handler interface { ServeHTTP(Respo ...
- writing a usb driver(在国外的网站上复制下来的)
Writing a Simple USB Driver From Issue #120April 2004 Apr 01, 2004 By Greg Kroah-Hartman in Soft ...
- iOS 中的视图函数 init initwithnib viewDidLoad viewWillAppear的总结
我要总结的函数主要是这几个: UIView *view-如果view还没有被初始化的话,getter方法会先调用[self loadView],如果getter或者setter方法被重写了,子类中的g ...
- POJ:1330-Nearest Common Ancestors(LCA在线、离线、优化算法)
传送门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K ...
- python+selenium面试题
selenium中如何判断元素是否存在? selenium中没有提供原生的方法判断元素是否存在,一般我们可以通过定位元素+异常捕获的方式判断. # 判断元素是否存在 try: dr.find_elem ...
- list里内置程序用法
列表是我们编程工作中经常都会遇到的数据类型.以下是列表里面的一些常用操作,主要分为:增! 删! 改! 查! first 查: 1.索引(下标),其中有切片操作,但要注意下标都是从零开始: 2.查元素出 ...
- 安装python包
开始--cmd pip install pymongo 直到最后给出提示successfull install pymongo-**(版本号,最新的)
- Matlab 二值图像label regions
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/52862719 Matlab提供了现成的 ...
- Concept with HTTP API && RPC
RPC=Remote Produce Call 是一种技术的概念名词. HTTP是一种协议,RPC可以通过HTTP来实现,也可以通过Socket自己实现一套协议来实现.所以楼主可以换一个问法,为何RP ...