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最长长度,线段树合并时更 ...
随机推荐
- windows server2008R2 64位 配置 mysql-8.0.15-winx64
下载mysql: 1 https://dev.mysql.com/downloads/mysql/ 如图: 然后在解压的文件夹里面新建 my.ini文件,内容如下 按 Ctrl+C 复制代码 按 Ct ...
- COMP9021--6.13
1. break语句和continue语句都可以在循环中使用,且常与选择结构结合使用,以达到在特定条件满足时跳出循环的作用.break语句被执行,可以使整个循环提前结束.而continue语句的作用是 ...
- LeetCode(279)Perfect Squares
题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9 ...
- Hard problem CodeForces - 706C
Time limit1000 ms Memory limit262144 kB 题目: Vasiliy is fond of solving different tasks. Today he fou ...
- CodeForce--Benches
A. Benches There are nn benches in the Berland Central park. It is known that aiai people are curr ...
- 数据挖掘算法——Apriori
在上一篇数据挖掘入门算法整理中提到,Apriori算法是关联规则算法中使用最为广泛的算法,这次我们就来学习下该算法的基本知识. 一.算法概述 Apriori 算法是一种最有影响力的挖掘布尔关联规则的频 ...
- 用virtualbox+模拟串口+CDT调试linux内核 TCP/IP协议栈-起步
经常有人问一台机器如何将hello经网络发送给另一台机器,我确实是不知道,只能看代码了. 说明:本人对内核的研究学习也是刚刚起步,有很多不了解的,所以文中可能会有一些"一本正经的胡扯&quo ...
- TOJ1698: Balanced Lineup
Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same ...
- Invalid regular expression flags 错误
找到写正则表达式的地方,检查是不是写了一个非法的正则表达式. Invalid regular expression flags
- 开发者选择短视频SDK,为何青睐七牛云?
从文字到图片再到视频的互联网内容媒介发展途径,随着 5g 技术的逐渐落地愈发清晰.短视频市场中的角力也随着诸多资本和创业者的涌入,进入到白热化阶段.这样的情况下,选择合适的短视频SDK产品就显得尤为重 ...