题意:https://blog.csdn.net/qq_39809664/article/details/79871282

思路:我们考虑LIS的状态转移,对于这个题,假设现在扫描到的边是(u, v, w),那么需要找以u为结尾的,并且值小于w的dp值。我们可以对每个节点建一颗权值线段树,询问的时候找对应节点的线段树中小于w的最大值。因为只有单点操作的过程,只要我们动态开点, 空间复杂度就是O(nlogn)的,可以开的下。

代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
struct SegementTree {
int lson, rson;
int val;
};
SegementTree tr[maxn * 200];
int tot, root[maxn];
void pushup(int now) {
tr[now].val = max(tr[tr[now].lson].val, tr[tr[now].rson].val);
}
void insert(int now, int l, int r, int pos, int val) {
if(l == r) {
tr[now].val = max(tr[now].val, val);
return;
}
int mid = (l + r) >> 1;
if(pos <= mid) {
if(!tr[now].lson) tr[now].lson = ++tot;
insert(tr[now].lson, l, mid, pos, val);
} else {
if(!tr[now].rson) tr[now].rson = ++tot;
insert(tr[now].rson, mid + 1, r, pos, val);
}
pushup(now);
}
int query(int now, int l, int r, int ql, int qr) {
if(l >= ql && r <= qr) {
return tr[now].val;
}
int mid = (l + r) >> 1, ans = 0;
if(ql <= mid && tr[now].lson) ans = max(ans, query(tr[now].lson, l, mid, ql, qr));
if(qr > mid && tr[now].rson) ans = max(ans, query(tr[now].rson, mid + 1, r, ql, qr));
return ans;
}
int main() {
int n, m, x, y, z;
scanf("%d%d", &n, &m);
int tmp = 0, ans = 0;
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &x, &y, &z);
tmp = 0;
if(root[x]) tmp = query(root[x], 0, 100000, 0, z - 1);
if(!root[y]) root[y] = ++tot;
insert(root[y], 0, 100000, z, tmp + 1);
}
for (int i = 1; i <= n; i++) {
ans = max(ans, tr[root[i]].val);
}
printf("%d\n", ans);
}

  

Codeforces 960F 线段树的更多相关文章

  1. Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论

    Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...

  2. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 831E) - 线段树 - 树状数组

    Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this int ...

  3. Codeforces 938G 线段树分治 线性基 可撤销并查集

    Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问 ...

  4. codeforces 1136E 线段树

    codeforces 1136E: 题意:给你一个长度为n的序列a和长度为n-1的序列k,序列a在任何时候都满足如下性质,a[i+1]>=ai+ki,如果更新后a[i+1]<ai+ki了, ...

  5. Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset

    Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...

  6. D - The Bakery CodeForces - 834D 线段树优化dp···

    D - The Bakery CodeForces - 834D 这个题目好难啊,我理解了好久,都没有怎么理解好, 这种线段树优化dp,感觉还是很难的. 直接说思路吧,说不清楚就看代码吧. 这个题目转 ...

  7. B - Legacy CodeForces - 787D 线段树优化建图+dij最短路 基本套路

    B - Legacy CodeForces - 787D 这个题目开始看过去还是很简单的,就是一个最短路,但是这个最短路的建图没有那么简单,因为直接的普通建图边太多了,肯定会超时的,所以要用线段树来优 ...

  8. CodeForces 343D 线段树维护dfs序

    给定一棵树,初始时树为空 操作1,往某个结点注水,那么该结点的子树都注满了水 操作2,将某个结点的水放空,那么该结点的父亲的水也就放空了 操作3,询问某个点是否有水 我们将树进行dfs, 生成in[u ...

  9. Linear Kingdom Races CodeForces - 115E (线段树优化dp)

    大意: n条赛道, 初始全坏, 修复第$i$条花费$a_i$, m场比赛, 第$i$场比赛需要占用$[l_i,r_i]$的所有赛道, 收益为$w_i$, 求一个比赛方案使得收益最大. 设$dp[i]$ ...

随机推荐

  1. 解决:Elipse配置Jython Interpreters时报错Error: Python stdlib source files not found

    今天学习lynnLi的博客monkeyrunner之eclipse中运行monkeyrunner脚本之环境搭建(四)时,遇到了一个问题,即: lynnLi给出的解决办法是:将Python下的Lib拷贝 ...

  2. 缓存淘汰算法--LRU算法(转)

    (转自:http://flychao88.iteye.com/blog/1977653) 1. LRU1.1. 原理 LRU(Least recently used,最近最少使用)算法根据数据的历史访 ...

  3. NodeJs学习之API篇

    学习nodeJS的API在对于使用nodeJS来进行编程的是十分重要的,所以首先就要去学习看看,相关的node的模块,来看一看相关的内容和可用性. 正文篇: nodeJS的API学习之路.(这里我们将 ...

  4. java快捷键大全

    存盘 Ctrl+s(肯定知道) 注释代码 Ctrl+/ 取消注释 Ctrl+(Eclipse3已经都合并到Ctrl+/了) 代码辅助 Alt+/ 快速修复 Ctrl+1 代码格式化 Ctrl+Shif ...

  5. Django 基础 ORM系统

    Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据 ...

  6. 一直想写的关于tarjan算法的理解——向struct edge大佬低头

    tarjan的算法精髓就是dfn[]和low[]数组 dfn[i]表示在该节点被搜索的次序(时间戳) low[i]表示i或i的子树可以追溯到的最早的栈中节点 判断有强连通分量的条件就是 dfn[i]= ...

  7. python_Notepad++编码集的说明

    window环境 [以ANSI格式编码]:简对应python中的"gbk"编码 [以UTF-8无BOM格式编码]:对应python中的"utf-8"编码 [以U ...

  8. awk指令的使用

    awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大 awk工作流程是这样的:读入有'\n'换行符分割的一条记录,然后将记录按指定的域分隔 ...

  9. Effective Python之编写高质量Python代码的59个有效方法

                                                         这个周末断断续续的阅读完了<Effective Python之编写高质量Python代码 ...

  10. 1110. Complete Binary Tree (25)

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...