题目链接 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. 这五本Python急速入门必读的书,送给正在学习Python的你!

    书籍是人类进步的阶梯,这句话从古至今都是适用的.为什么会这么说呢?书籍,它记录了人们实践的经验,这些经验有助于我们快速的学习,对于编程学习来说也不例外,今天就给大家带来了以下的书籍干货,希望能够帮助到 ...

  2. 七周成为数据分析师06_MySQL

    关于 MySQL 的知识,主要也是一些实操和练习. 因为个人之前已经专门练习过 MySQL 操作,这里就不做笔记,之后另写一篇博文记录 MySQL 知识. 同时附上本课程对应的文字教程: 如何七周成为 ...

  3. STM32CUBEMX入门学习笔记2:关于STM32芯片使用内部flash

    找到正点原子的官网,下载他的HAL库:http://www.openedv.com/thread-109778-1-1.html 找到此例程,并打开其工程文件. 找到此文件,复制到自己工程里 复制到自 ...

  4. 使用nohup+& 踩到的坑

    首先分清楚nohup与&: &是指在后台运行一般在执行命令后,都会显式的在前台执行,当Ctrl+C后进程回宕掉,但是 在命令后加&,即使Ctrl+C,程序还在进行,但是,当关闭 ...

  5. Python中re(正则表达式)模块使用方法

    Python中常用的正则表达式处理函数: re.match re.match 尝试从字符串的开始匹配一个模式,如:下面的例子匹配第一个单词. import re text = "JGood ...

  6. poj2823 Sliding Window luogu1886 滑动窗口 单调队列

    模板题 #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...

  7. MySQL中的DDL(Data Definition Language,数据定义语言)

    create(创建表) 标准的建表语句: create table [模式名.]表名 ( #可以有多个列定义 columnName1 dataType [default expr(这是默认值)], . ...

  8. [POJ 1003] Hangover C++解题

        Hangover Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 95164   Accepted: 46128 De ...

  9. 大数据学习——spark安装

    一主多从 1 上传压缩包 2 解压 -bin-hadoop2..tgz 删除安装包 -bin-hadoop2..tgz 重命名 mv spark-1.6.2-bin-hadoop2.6/ spark  ...

  10. 聊聊、Nginx 初始化日志文件

    我们接着上一篇文章继续来看看 ngx_regex_init()函数.搜索 ngx_regex_init 得到位置为src/core/ngx_regex.c:ngx_regex_init(void). ...