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]$ ...
随机推荐
- 用Find命令查找文件
➜ AFNetworking find . -iname "*.swift" ./Example/tvOS Example/AppDelegate.swift ./Example/ ...
- ural 1039 树dp
http://acm.timus.ru/problem.aspx?space=1&num=1039 1039. Anniversary Party Time limit: 0.5 second ...
- ADO.NET异步操作测试
配置文件: <?xml version="1.0"?> <configuration> <startup> <supportedRunti ...
- IE跨Iframe时Session丢失问题
例如IIS下,可以打开IIS窗口——〉选择一个网站——〉属性——〉http头,增加一个http头 然后输入头名:P3P 输入头内容:CP=CAO PSA OUR 点评:与上一个方法类似,此方法也要求第 ...
- 项目管理理论与实践(6)——利用Excel制作项目文档的设计技巧
这篇是使用的Excel 2007 进行文档设计,Excel的设计也是一门学问,这里主要介绍一些Excel的设计技巧,后面也会陆续更新该文章. 1. 固定某行某列 首先设计这样的任务管理文档: 现在我想 ...
- if (i%4 ==0 ) 逻辑的魅力 在于 这里
<div class="course_height"> </div> <script src="js/jquery.min.js" ...
- filter()和sort()这两个方法一块学习,案例中。
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Linux运维工程师中级面试题
1.解释top命令和vmstat命令 2.请写出iptables语句 3.mysql高可用方案有哪些?mysql备份方案有哪些?有什么缺点? 4.写出Apache 2.x的两种工作模式,以及各自的工作 ...
- HDU - 5126 stars (CDQ分治)
题目链接 题目大意:一共有Q(1<=Q<=50000)组操作,操作分为两种: 1.在x,y,z处添加一颗星星 2.询问以(x1,y1,z1)与(x2,y2,z2)为左上和右下顶点的矩形之间 ...
- 微信小程序 写音乐播放器 slider组件 将value设置为0 真机测试滑块不能回到起点
最近在用微信小程序写一个音频播放页面,做时间进度的时候用到了slider插件,但是在自然播放完成,或者上/下切换的时候,将slider的value属性值设为0,开发工具上滑块会回到起点,有效.但是真机 ...