Pathwalks CodeForces - 960F(主席树 || 树状数组)
题意:
求树上最长上升路径
解析:
树状数组版: 998ms
edge[u][w] 代表以u为一条路的终点的小于w的最长路径的路的条数
· 那么edge[v][w] = max(edge[u][w-1]) + 1;
因为w最小是0 所以所有的w都+1
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+, INF = 0x7fffffff;
int n, m, maxx = -INF;
map<int, int> edge[maxn];
int lowbit(int x)
{
return x & -x;
}
int qp(int u, int w)
{
int ret = ;
for(int i=w; i>; i-=lowbit(i))
ret = max(ret, edge[u][i]);
return ret;
} int build(int u, int w, int ans)
{
while(w)
{
edge[u][w] = max(edge[u][w], ans);
w += lowbit(w);
} } int main()
{
int u, v, w;
cin >> n >> m;
for(int i=; i<m; i++)
{
cin >> u >> v >> w;
maxx = max(maxx, w);
build(v, +, qp(u, w)+);
}
int max_ret = -INF;
for(int i=; i<=n; i++)
max_ret = max(max_ret, qp(i, ));
cout << max_ret << endl; return ;
}
主席树: 108ms
每棵树都建立100000个结点 每次更新小于w的结点的sum
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1e5 + , INF = 0x7fffffff;
int n, m, cnt, root[maxn], a[maxn], x, y, k;
struct node{int l, r, sum;}T[maxn*];
void update(int l, int r, int& x, int w, int ci)
{
if(!x) x = ++cnt; T[x].sum = max(T[x].sum, ci);
if(l == r) return;
int mid = (l + r) / ;
if(mid >= w) return update(l, mid, T[x].l, w, ci);
else return update(mid+, r, T[x].r, w, ci);
} int query(int l, int r, int x, int k)
{
if(l == r) return T[x].sum;
int mid = (l + r)/;
if(mid >= k) return query(l, mid, T[x].l, k);
else return max(T[T[x].l].sum, query(mid+, r, T[x].r, k));
} int main()
{
int u, v, w, ret = -INF;
rd(n), rd(m);
rep(i, , m)
{
rd(u), rd(v), rd(w);
w++;
int tmp = query(, , root[u], w-) + ;
update(, , root[v], w, tmp);
ret = max(ret, tmp);
}
cout<< ret <<endl; return ;
}
Pathwalks CodeForces - 960F(主席树 || 树状数组)的更多相关文章
- CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)
The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...
- Codeforces 960F Pathwalks ( LIS && 树状数组 )
题意 : 给出若干个边,每条边按照给出的顺序编号,问你找到一条最长的边权以及边的编号同时严格升序的一条路径,要使得这条路径包含的边尽可能多,最后输出边的条数 分析 : 这题和 LIS 很相似,不同的 ...
- Codeforces Round #404 (Div. 2) E. Anton and Permutation(树状数组套主席树 求出指定数的排名)
E. Anton and Permutation time limit per test 4 seconds memory limit per test 512 megabytes input sta ...
- bzoj1901--树状数组套主席树
树状数组套主席树模板题... 题目大意: 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]--a[ ...
- HDU 3333 | Codeforces 703D 树状数组、离散化
HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...
- BZOJ 3196 Tyvj 1730 二逼平衡树 ——树状数组套主席树
[题目分析] 听说是树套树.(雾) 怒写树状数组套主席树,然后就Rank1了.23333 单点修改,区间查询+k大数查询=树状数组套主席树. [代码] #include <cstdio> ...
- BZOJ 1901 Zju2112 Dynamic Rankings ——树状数组套主席树
[题目分析] BZOJ这个题目抄的挺霸气. 主席树是第一时间想到的,但是修改又很麻烦. 看了别人的题解,原来还是可以用均摊的思想,用树状数组套主席树. 学到了新的姿势,2333o(* ̄▽ ̄*)ブ [代 ...
- 【BZOJ 1901】【Zju 2112】 Dynamic Rankings 动态K值 树状数组套主席树模板题
达神题解传送门:http://blog.csdn.net/dad3zz/article/details/50638360 说一下我对这个模板的理解: 看到这个方法很容易不知所措,因为动态K值需要套树状 ...
- 学习笔记--函数式线段树(主席树)(动态维护第K极值(树状数组套主席树))
函数式线段树..资瓷 区间第K极值查询 似乎不过似乎划分树的效率更优于它,但是如果主席树套树状数组后,可以处理动态的第K极值.即资瓷插入删除,划分树则不同- 那么原理也比较易懂: 建造一棵线段树(权值 ...
随机推荐
- uC/OS-III 时钟节拍,时间管理,时间片调度
uC/OS-III 时钟节拍,时间管理,时间片调度 时钟节拍 时钟节拍可谓是 uC/OS 操作系统的心脏,它若不跳动,整个系统都将会瘫痪. 时钟节拍就是操作系统的时基,操作系统要实现时间上的管理, ...
- ZOJ3623:Battle Ships(全然背包)
Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense ...
- 20155226《网络攻防》 Exp5 MSF基础应用
20155226<网络攻防> Exp5 MSF基础应用 基础问题回答 1.用自己的话解释什么是exploit,payload,encode? exploit : Exploit的英文意思就 ...
- # 2017-2018-2 20155319《网络对抗技术》Exp9 :Web安全基础
2017-2018-2 20155319<网络对抗技术>Exp9 :Web安全基础 实践过程 webgoat准备 从GitHub上下载jar包(老师的虚拟机中有 无需下载) 拷贝到本地,并 ...
- 20155333 《网络对抗》Exp4 恶意代码分析
20155333 <网络对抗>Exp4 恶意代码分析 基础问题回答 1.如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监控下系统一天天的到底在干些什么.请设计下你想监控的操作有哪 ...
- Java实现Zip压缩包解压
对zip压缩包的解压是比较常见的应用场景,java代码的实现也很简单.废话不多说,直接上代码吧 一.代码 /** * zip解压 * @param srcFile zip源文件 * @para ...
- ListBox项模板中绑定ListBoxItem属性的方法
原文:ListBox项模板中绑定ListBoxItem属性的方法 <ListBox> <ListBox.ItemTemplate> <DataTemplate> & ...
- 类加载, 静态变量初始化, String不可变, 泛型使用, 内部类
1.java变量类型 java变量类型分:基本数据类型变量和Object数据类型变量,变量也是占用者内存的 例如: int i = 3; i这个变量保存的就是整形3, 占32位 Object a = ...
- python 回溯法 子集树模板 系列 —— 1、8 皇后问题
问题 8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或同一斜线上,问有多少种摆法. 分析 为了简化问题,考虑到8个皇后不同行,则每一行放置一个皇后,每一行的 ...
- Ubuntu16.04LTS +Qt+boost1.66编译错误:consuming_buffers.hpp: parse error in template argument list
升级gcc版本至 6 以上.. 安装gcc-6系列与安装boost (Ubuntu16.04LTS)