题目链接 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(离散化 + 线段树合并)的更多相关文章

  1. Codeforces ECR47F Dominant Indices(线段树合并)

    一个比较显然的做法:对每棵子树用线段树维护其中的深度,线段树合并即可. 本来想用这个题学一下dsu on tree,结果还是弃疗了. #include<iostream> #include ...

  2. Codeforces 490F Treeland Tour 树形dp

    Treeland Tour 离散化之后, 每个节点维护上升链和下降链, 感觉复杂度有点高, 为啥跑这么快.. #include<bits/stdc++.h> #define LL long ...

  3. CodeForces - 1037H: Security(SAM+线段树合并)

    题意:给定字符串S:  Q次询问,每次询问给出(L,R,T),让你在S[L,R]里面找一个字典序最小的子串,其字典序比T大. 没有则输出-1: 思路:比T字典序大,而且要求字典最小,显然就是在T的尾巴 ...

  4. [Codeforces]817F. MEX Queries 离散化+线段树维护

    [Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...

  5. Codeforces 490F. Treeland Tour 暴力+LIS

    枚举根+dfs 它可以活 , 我不知道有什么解决的办法是积极的 ...... F. Treeland Tour time limit per test 5 seconds memory limit p ...

  6. Codeforces 490F Treeland Tour 树上的最长上升子序列

    题目链接:点击打开链接 题意: 给定n个点的树. 以下n个数表示点权. 以下n-1行给出树. 找一条链,然后找出这条链中的点权组成的最长上升子序列. 求:最长上升子序列的长度. 思路: 首先是维护一条 ...

  7. CodeForces 19D Points(离散化+线段树+单点更新)

    题目链接: huangjing 题意:给了三种操作 1:add(x,y)将这个点增加二维坐标系 2:remove(x,y)将这个点从二维坐标系移除. 3:find(x,y)就是找到在(x,y)右上方的 ...

  8. 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数

    题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...

  9. [CF490F]Treeland Tour(线段树合并)

    树上LIS:树上找一条简单路径的子序列使点权严格单增,最大化长度. 原题数据过小,用线段树合并可以做到$O(n\log n)$. 每个点用一棵线段树维护以每个权值为结尾的LIS最长长度,线段树合并时更 ...

随机推荐

  1. 【android】安卓的权限提示及版本相关

    Only dangerous permissions require user agreement. The way Android asks the user to grant dangerous ...

  2. ubuntu 设置定时任务

    crontab -l  #查看详情crontab -e #设置定时任务 * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用*或者 */1表示 第2列表示小时 ...

  3. 微软与百度合作:win10搜索引擎默认百度

    全球最大的中文搜索引擎百度公司与微软公司共同宣布双方展开战略合作.百度并将成为中国市场上Windows 10 Microsoft Edge浏览器的默认主页和搜索引擎.也就是说,将来人们在win10的M ...

  4. Python基础学习总结__Day3

    一.集合 1.特性:无序且天生去重,格式为{} 2.作用: (1)去重 (2)关系测试 3.可调用函数(常见对列表操作) (1)取交集:A.intersection(B) (2)取并集:A.union ...

  5. leetcode-7-hashTable

    解题思路: 这道题需要注意的是s和t长度相等,但都为空的情况.只需要扫描一遍s建立字典(char, count),然后扫描t,如果有 未出现的字母,或者键值小于0,就可以返回false了. bool ...

  6. List删除元素

    在单线程环境下的解决办法 public void remove() { if (lastRet == -1) throw new IllegalStateException(); checkForCo ...

  7. HDU 2242 双连通分量 考研路茫茫——空调教室

    思路就是求边双连通分量,然后缩点,再用树形DP搞一下. 代码和求强连通很类似,有点神奇,=_=,慢慢消化吧 #include <cstdio> #include <cstring&g ...

  8. Install Oracle 11G Release 2 (11.2) on Centos Linux 7

    Install Oracle 11G Release 2 (11.2) on Centos Linux 7 This article presents how to install Oracle 11 ...

  9. 1章 perl入门

    1.标量数据    单变量    数字和字符串两种情况 2.所有数字的内部格式都相同.浮点型   perl中没有应对整数值得运算   . 3.浮点数直接量 数字e表示10的次方标示符 例子:-6.5e ...

  10. thinkphp文件上传以及图片处理

    文件上传 上传表单 在ThinkPHP中使用上传功能无需进行特别处理.例如,下面是一个带有附件上传的表单提交: <form action="__URL__/upload" e ...