题意: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. Memcache笔记(1)

    缓存主要分为:页面缓存和数据缓存 Memcache .redis.mongodb都是做数据缓存的 Memcache是什么? 是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的ha ...

  2. 51nod 1449 贪心

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1449 1449 砝码称重 题目来源: CodeForces 基准时间限制 ...

  3. win32下开发hadoop

    转载自:http://my.oschina.net/muou/blog/408543[木偶:Windows下使用Hadoop2.6.0-ecli­p­s­e­-­p­­lugin插件] 对于一些细节地 ...

  4. MongoDB Wiredtiger存储引擎实现原理——Copy on write的方式管理修改操作,Btree cache

    转自:http://www.mongoing.com/archives/2540 传统数据库引擎的数据组织方式,一般存储引擎都是采用 btree 或者 lsm tree 来实现索引,但是索引的最小单位 ...

  5. 手动安装mysql-5.0.45.tar.gz

    Linux下编译安装 安装环境:VMware9(桥接模式) + Linux bogon 2.6.32-642.3.1.el6.x86_64(查看linux版本信息:uname -a) 先给出MySQL ...

  6. Mac 系统安装redis服务

    1.首先去http://www.redis.io/下载最新的redis文件,现在最新的是redis-2.8.19 2.进行解压缩 tar -zxvf redis-2.8.19.tar.gz 3.移动重 ...

  7. windows下安装 redis并开机自启动

    1,redis官方下载地址:https://redis.io/download,redis 64位下载地址:https://github.com/ServiceStack/redis-windows, ...

  8. stl_hash_set.h

    stl_hash_set.h // Filename: stl_hash_set.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: ...

  9. mxnet(gluon)—— 模型、数据集、损失函数、优化子等类、接口大全

    1. 数据集 dataset_train = gluon.data.ArrayDataset(X_train, y_train) data_iter = gluon.data.DataLoader(d ...

  10. fakeroot: preload library `libfakeroot.so' not found, aborting.

    /**************************************************************************** * fakeroot: preload li ...