题意:

  求树上最长上升路径

解析:

  树状数组版: 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(主席树 || 树状数组)的更多相关文章

  1. CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)

    The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...

  2. Codeforces 960F Pathwalks ( LIS && 树状数组 )

    题意 : 给出若干个边,每条边按照给出的顺序编号,问你找到一条最长的边权以及边的编号同时严格升序的一条路径,要使得这条路径包含的边尽可能多,最后输出边的条数 分析 :  这题和 LIS 很相似,不同的 ...

  3. 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 ...

  4. bzoj1901--树状数组套主席树

    树状数组套主席树模板题... 题目大意: 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]--a[ ...

  5. HDU 3333 | Codeforces 703D 树状数组、离散化

    HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...

  6. BZOJ 3196 Tyvj 1730 二逼平衡树 ——树状数组套主席树

    [题目分析] 听说是树套树.(雾) 怒写树状数组套主席树,然后就Rank1了.23333 单点修改,区间查询+k大数查询=树状数组套主席树. [代码] #include <cstdio> ...

  7. BZOJ 1901 Zju2112 Dynamic Rankings ——树状数组套主席树

    [题目分析] BZOJ这个题目抄的挺霸气. 主席树是第一时间想到的,但是修改又很麻烦. 看了别人的题解,原来还是可以用均摊的思想,用树状数组套主席树. 学到了新的姿势,2333o(* ̄▽ ̄*)ブ [代 ...

  8. 【BZOJ 1901】【Zju 2112】 Dynamic Rankings 动态K值 树状数组套主席树模板题

    达神题解传送门:http://blog.csdn.net/dad3zz/article/details/50638360 说一下我对这个模板的理解: 看到这个方法很容易不知所措,因为动态K值需要套树状 ...

  9. 学习笔记--函数式线段树(主席树)(动态维护第K极值(树状数组套主席树))

    函数式线段树..资瓷 区间第K极值查询 似乎不过似乎划分树的效率更优于它,但是如果主席树套树状数组后,可以处理动态的第K极值.即资瓷插入删除,划分树则不同- 那么原理也比较易懂: 建造一棵线段树(权值 ...

随机推荐

  1. keepalived 做全端口映射

    global_defs {    lvs_id BACKUP }   vrrp_sync_group VGM {     group {        VI_1     } }   vrrp_inst ...

  2. 未能从程序集“System.Transactions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中加载类型“System.Transactions.TransactionScopeAsyncFlowOption”

    项目发布到IIS以后,报以下错误 出现以上问题的原因是,我的项目是在Framework 4.5.2下开发的,而发布程序的服务器FM版本是4.5 .我解决办法是安装Framework 4.6.2 具体办 ...

  3. 多模匹配算法之Aho-Corasick

    除剔除那些含有敏感词的文本,由于有大量的敏感词,所以通过简单的正则表达式和字符串查找的方式效率太低,每次都有遍历一次字符串.而AC算法的核心思想就是避免不必要的回溯使搜索一直沿着向前的方向,最大可能的 ...

  4. docker 端口映射错误解决方法

    今天搞了半天shipyard,在网页上打开时无法显示容器和镜像,最后发现是docker端口映射错误,由于防火墙未关闭: 4月 12 18:51:29 localhost firewalld[757]: ...

  5. HUE配置hadoop

    HDFS配置 参考文档:http://archive.cloudera.com/cdh5/cdh/5/hue-3.9.0-cdh5.5.0/manual.html Hadoop配置文件修改 hdfs- ...

  6. Sign in with the app-specific password you generated. If you forgot the app-specific password or need to create a new one, go to appleid.apple.com

    iOS打包报错信息如下:Sign in with the app-specific password you generated. If you forgot the app-specific pas ...

  7. China Internet Conference(2018.07.12)

    中国互联网大会 时间:2018.07.12地点:北京国家会议中心

  8. WPF编程,使用WindowChrome实现自定义窗口功能的一种方法。

    原文:WPF编程,使用WindowChrome实现自定义窗口功能的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/arti ...

  9. scala学习——(1)scala基础(下)

    (七)定长数组 val array_name = new Array[T](length) val array_name = Array("","") 通过() ...

  10. python 获取文件和文件夹大小

    1.os.path.getsize可以获取文件大小 >>> import os >>> file_name = 'E:\chengd\Cd.db' >> ...