Codeforces 960F 线段树
题意: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 线段树的更多相关文章
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
- 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 ...
- Codeforces 938G 线段树分治 线性基 可撤销并查集
Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问 ...
- codeforces 1136E 线段树
codeforces 1136E: 题意:给你一个长度为n的序列a和长度为n-1的序列k,序列a在任何时候都满足如下性质,a[i+1]>=ai+ki,如果更新后a[i+1]<ai+ki了, ...
- Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset
Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...
- D - The Bakery CodeForces - 834D 线段树优化dp···
D - The Bakery CodeForces - 834D 这个题目好难啊,我理解了好久,都没有怎么理解好, 这种线段树优化dp,感觉还是很难的. 直接说思路吧,说不清楚就看代码吧. 这个题目转 ...
- B - Legacy CodeForces - 787D 线段树优化建图+dij最短路 基本套路
B - Legacy CodeForces - 787D 这个题目开始看过去还是很简单的,就是一个最短路,但是这个最短路的建图没有那么简单,因为直接的普通建图边太多了,肯定会超时的,所以要用线段树来优 ...
- CodeForces 343D 线段树维护dfs序
给定一棵树,初始时树为空 操作1,往某个结点注水,那么该结点的子树都注满了水 操作2,将某个结点的水放空,那么该结点的父亲的水也就放空了 操作3,询问某个点是否有水 我们将树进行dfs, 生成in[u ...
- Linear Kingdom Races CodeForces - 115E (线段树优化dp)
大意: n条赛道, 初始全坏, 修复第$i$条花费$a_i$, m场比赛, 第$i$场比赛需要占用$[l_i,r_i]$的所有赛道, 收益为$w_i$, 求一个比赛方案使得收益最大. 设$dp[i]$ ...
随机推荐
- Java 反射机制应用实践
反射基础 p.s: 本文需要读者对反射机制的API有一定程度的了解,如果之前没有接触过的话,建议先看一下官方文档的Quick Start(https://docs.oracle.com/javase/ ...
- 【Prism】MEF版Commanding
引言 接下来的是Commanding Demo的改造. DelegateCommand WPF本身提供了一个RoutedCommand,然而没什么卵用.在Prism框架中提供了个更人性化的ICo ...
- golang的项目结构 相关知识
### 项目结构 ``` ├── bin │ ├── login │ └── main ├── pkg │ └── darwin_amd64 │ └── login │ └── a ...
- boost bind function用法说明
目录(?)[+] 1 bind/function 引 (1)头文件 bind函数#include <boost/bind.hpp> function使用头文件#include <bo ...
- CANopenNode drvTemplate/CO_driver.h hacking
/************************************************************************ * CANopenNode drvTemplate/ ...
- 数据清洗记录,pandas
pandas数据清洗:http://www.it165.net/pro/html/201405/14269.html data=pd.Series([1,2,3,4]) data.replace([1 ...
- JvisualVm添加远程监控
一.Weblogic远程监控 1.首先需要在远程的weblogic的域下面,找到/bin/ setDomainEnv.sh ,需要在此文件下加入如下内容: -Dcom.sun.management.j ...
- 软件架构设计 ADMEMS方法体系
ADMEMS是Architecture Design Method has been Extended to Method System的简称,是由CSAI顾问团架构设计专家组于2009年11月在第六 ...
- Gradle的快速入门
1.基础知识: Gradle提供了:构建项目的框架.但是其中起作用的是Plugin. Gradle在默认情况下提供了很多常用的Plugin.例如:构建Java的Plugin.还有war.Ear等. G ...
- Day2-Python基础2---浅copy、深copy的差别
浅copy 首先我们来看下面一段代码: 1 >>> names = ["maqing"," peilin"," xiaoming&q ...