首页 > 编程语言 >codeforces.ml/contest/932/problem/D 树上找最长上升子序列长度 倍增算法

codeforces.ml/contest/932/problem/D 树上找最长上升子序列长度 倍增算法

时间:2022-09-05 16:46:37浏览次数:80  
标签:node cnt contest int ml la codeforces query 节点

D. Tree time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output

You are given a node of the tree with index 1 and with weight 0. Let cnt be the number of nodes in the tree at any instant (initially, cnt is set to 1). Support Q queries of following two types:

  •  Add a new node (index cnt + 1) with weight W and add edge between node R and this node.
  •  Output the maximum length of sequence of nodes which
    1. starts with R.
    2. Every node in the sequence is an ancestor of its predecessor.
    3. Sum of weight of nodes in sequence does not exceed X.
    4. For some nodes i, j that are consecutive in the sequence if i is an ancestor of j then w[i] ≥ w[j] and there should not exist a node k on simple path from i to j such that w[k] ≥ w[j]

The tree is rooted at node 1 at any instant.

Note that the queries are given in a modified way.

Input

First line containing the number of queries Q (1 ≤ Q ≤ 400000).

Let last be the answer for previous query of type 2 (initially last equals 0).

Each of the next Q lines contains a query of following form:

  • 1 p q (1 ≤ p, q ≤ 1018): This is query of first type where  and . It is guaranteed that 1 ≤ R ≤ cnt and 0 ≤ W ≤ 109.
  • 2 p q (1 ≤ p, q ≤ 1018): This is query of second type where  and . It is guaranteed that 1 ≤ R ≤ cnt and 0 ≤ X ≤ 1015.

 denotes bitwise XOR of a and b.

It is guaranteed that at least one query of type 2 exists.

Output

Output the answer to each query of second type in separate line.

 

题意:

初始有个1号点,权值为0

操作1,将编号为cnt +1的点加到R号点后面,权值为W

操作2,查询编号为 R 的点,往祖宗节点上走,最大上升子序列的长度

 

分析:

树上操作,数据范围是1e18,考虑倍增。2^18 = 1e18

操作1:假设当前点是cnt,接在 R 后面,权值为 W,可以考虑维护 w[N][20] ,w[cnt][0] 表示 比 cnt大的第一个节点,就可以倍增去找比当前节点大的第一个节点,并接在它后面,它后面的节点都是比它大的。

操作2:维护 g[N][20] 表示,这个节点往上 2^j 个节点的权值总和,可以倍增找到 最大的上升子序列长度

#define int ll
const int N = 4e5+10;
int n,m,q;

int V[N],g[N][20],f[N][20],fa[N],dep[N];

void add(int cnt,int r,ll w) {
    fa[cnt] = r;f[cnt][0] = r;g[cnt][0] = w;
    V[cnt] = w;dep[cnt] = dep[r] + 1;
    for(int j = 1;j <= 19;j ++ ) {
        f[cnt][j] = f[f[cnt][j-1]][j-1];
        g[cnt][j] = g[cnt][j-1] + g[f[cnt][j-1]][j-1];
    }
}

int find(int r,ll w) {
    return !r || V[r] >= w ? r : find(fa[r],w);
}

void solve()
{
//    cin>>n>>m;
    cin>>q;
    int la = 0,cnt = 1;
    dep[1] = 1;
    while(q -- ) {
        int op;cin>>op;;
        if(op == 1) {
            int r,w;cin>>r>>w;
            r = r ^ la, w = w ^ la;
            add(++ cnt,find(r,w),w);
        } else {
            ll ans = 0;
            int r,x;cin>>r>>x;
            r = r ^ la,x = x ^ la;
            for(int j = 19;j >= 0 ;j -- ) {
                if(g[r][j] <= x) {
                    x -= g[r][j];
                    ans += min(1ll * (1<<j),dep[r]);
                    r = f[r][j];
                }
            }
            cout<<(la = ans)<<endl;
        }
    }
}

 

 

 

标签:node,cnt,contest,int,ml,la,codeforces,query,节点
From: https://www.cnblogs.com/er007/p/16658687.html

相关文章

  • 【UML分析、建模与设计】我在工作时遇到UML
    一、前言UML分析、建模与设计来自现实世界中的概念的抽象描述方法(摘取自《UML面向对象分析、建模与设计(第2版)》)就我对UML分析与建模技术的认知,最早可追溯至2019年时的......
  • 干货 | 一改测试步骤代码就全写?为什么不试试用 Yaml实现数据驱动?
    ⬇️点击“下方链接”,提升测试核心竞争力!>>更多技术文章分享和免费资料领取理念与同“UI自动化测试框架”中的“测试步骤的数据驱动”相同,接口中的测试步骤的数据驱动就......
  • UML图示详解
    UML图示详解前言UML俗称统一建模语言。我们可以简单理解成他是一套符号语言。不同的符号对应不同的含义。在之前设计模式章节中我们文章中用到的就是UML类图,UML除了类图......
  • HTML编辑器如何能实现直接粘贴把图片上传到服务器中
    ​ 当前功能基于PHP,其它语言流程大致相同 1.新增上传wordjson配置在ueditor\php\config.json中新增如下配置:     /* 上传word配置 */    "wordAction......
  • 利用response的getWriter().write()方法向浏览器传输一个html标签的时候,浏览器原封不
    问题:  解决:加入这一行代码  成功! ......
  • Atcoder Regular Contest 147
     AtcoderRegularContest147这是本蒟蒻第3次打的\(ARC\),赛时的时候\(B\)题调代码调崩了,\(wa\)了15发。所以来简略的写一下题解A:题目链接题目大意:略题目分析......
  • vue项目里地图组件截图快照的实现方法---html2Canvas
    一、前言最近项目里要求要把当前地图截图展示在小窗里,之前没接触这种请求,于是我就百度了一下,发现有这么一块插件html2Canvas,它能够将dom元素转换成canvas进行截图保存,而......
  • [HTML+CSS] 20.媒体查询,响应式布局
    笔记来源:尚硅谷Web前端HTML5&CSS3初学者零基础入门全套完整版目录媒体查询响应式布局媒体查询媒体查询语法媒体类型媒体特性断点媒体查询响应式布局网页可以根据不......
  • [HTML+CSS] 笔记总结
    目录笔记:几种水平垂直双方向居中的方式对比绝对定位的方式table-cell的方式/*transform变形平移的方式*/flex居中多余显示省略号:笔记:几种水平垂直双方向居中的方式对比......
  • [HTML+CSS] 17.less 简介
    笔记来源:尚硅谷Web前端HTML5&CSS3初学者零基础入门全套完整版目录less简介1、安装插件2、编写less3、less语法less注释父子关系嵌套变量其他4、混合函数其他5、......