题目链接 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. 如何用 CSS 和 D3 创作一个无尽的六边形空间

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/NBvrWL 可交互视频 此视频是可 ...

  2. leetcode-20-Dynamic Programming

    303. Range Sum Query - Immutable 解题思路: Note里说sumRange会被调用很多次..所以简直强烈暗示要做cache啊...所以刚开始,虽然用每次都去遍历数组求和 ...

  3. 彻底卸载gedit

    $ sudo  apt-get  purge gedit gedit-plugins$ sudo apt-get autoremove

  4. linux学习-用户的特殊 shell 与 PAM 模块

    特殊的 shell, /sbin/nologin 『无法登入』指的是:『这个使用者无法使用 bash 或其他 shell 来登入系统』而已, 并不是说这个账号就无法使用其他的系统资源! 让某个具有 / ...

  5. TSS (任务状态段)的作用及结构

    1.什么是TSS TSS全称Task State Segment ,是操作系统在进行进程切换时保存进程现场信息的段 2.TSS什么时候用,有什么用 TSS在任务(进程)切换时起着重要的作用,通过它保存 ...

  6. MongoDB学习-->命令行增删改查&JAVA驱动操作Mongodb

    MongoDB 是一个基于分布式文件存储的数据库. 由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关 ...

  7. loj2000 「SDOI2017」数字表格

    there #include <iostream> #include <cstring> #include <cstdio> using namespace std ...

  8. 常见的Linux目录及其含义

    /                                   系统根目录,通常不会在这里存放文件 . /bin                              二进制目录,存放许多 ...

  9. day01_07.逻辑与字符串运算符

    &&(并且)====>发现&符号总是打错,记忆口令:&7(暗器),在数字7上面,在python中是and ||(或者)====>在python中是or . ...

  10. cobbler常用目录/命令(三)

    常用目录: /var/www/cobbler/ks_mirror/                cobbler distro文件目录 /var/lib/tftpboot/pxelinux.cfg/d ...