960D - Full Binary Tree Queries

思路:

用move1[i]记录第i层第1种操作移动的个数(对这一层的个数取模)

用move2[i]记录第i层第2种操作移动的个数(对这一层的个数取模)

查询方法:

记当前值为 x,当前层为 i,则经过操作后,他到了 x + move1[i] + move2[i] 的位置,记 x = x + move1[i] + move2[i]

则这个位置上一层的位置是 x >> 1,记 x = x >> 1

那我们只要知道 x 这个位置的值是多好就好了,因为第二种操作对值并没有影响,所以 x 这个位置的值是 x - move1[i-1],记 x = x - move1[i-1]

这样循环操作就能输出路径了

代码:

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define mem(a, b) memset(a, b, sizeof(a)) LL move1[], move2[];
int main() {
int q, t;
LL x, k;
scanf("%d", &q);
while (q--) {
scanf("%d", &t);
if (t == ) {
scanf("%lld%lld", &x, &k);
int dep = ;
while (x) {
x >>= ;
dep++;
}
LL mod = 1LL << (dep - );
move1[dep] = ((move1[dep] + k ) % mod + mod) % mod;
}
else if (t == ) {
scanf("%lld%lld", &x, &k);
int dep = ;
while (x) {
x >>= ;
dep++;
}
LL mod = 1LL << (dep - );
move2[dep] = ((move2[dep] + k) % mod + mod) % mod;
}
else {
scanf("%lld", &x);
LL tx = x;
int dep = ;
while (tx) {
dep++;
tx >>= ;
}
bool f = true;
while (x) {
printf("%lld ", x);
LL mod = (1LL << dep - );
LL mv = (move1[dep] + move2[dep]) % mod;
LL L = 1LL << (dep - ), R = (L << ) - ;
if(x + mv > R) x = x + mv - mod;
else x = x + mv;
x >>= ;
dep --;
mod = (1LL << dep - );
L = 1LL << (dep - ), R = (L << ) - ;
if(x - move1[dep] < L) x = x + mod - move1[dep];
else x = x - move1[dep];
}
printf("\n");
}
}
return ;
}

Codeforces 960D - Full Binary Tree Queries的更多相关文章

  1. Codeforces 960D Full Binary Tree Queries ( 思维 && 模拟 )

    题意 : 给出一颗无限层的满二叉树,然后每个值为 X (根的 X 等于 1 ) 左孩子的值是 2*X,右孩子的值是 2*X+1 ,现在有两种操作,(1, x,k) 表示将 x 所在层的所有节点整体向右 ...

  2. Codeforces 250 E. The Child and Binary Tree [多项式开根 生成函数]

    CF Round250 E. The Child and Binary Tree 题意:n种权值集合C, 求点权值和为1...m的二叉树的个数, 形态不同的二叉树不同. 也就是说:不带标号,孩子有序 ...

  3. 九章算法系列(#3 Binary Tree & Divide Conquer)-课堂笔记

    前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode ...

  4. Full Binary Tree(二叉树找规律)

    Description In computer science, a binary tree is a tree data structure in which each node has at mo ...

  5. Full Binary Tree(sdut 2882)

    Problem Description: In computer science, a binary tree is a tree data structure in which each node ...

  6. 2014年山东省第五届ACM大学生程序设计竞赛F题:Full Binary Tree

    题目描述 In computer science, a binary tree is a tree data structure in which each node has at most two ...

  7. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  8. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  9. Leetcode, construct binary tree from inorder and post order traversal

    Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...

随机推荐

  1. Centos下搭建golang环境

    一.下载安装包 先查看一下我的Centos版本,这里是6.4. # cat /etc/redhat-release CentOS release 6.4 (Final) 去go语言中文社区下载想要下载 ...

  2. Html5之Web存储

    localStorage 方法存储的数据没有时间限制.第二天.第二周或下一年之后,数据依然可用. sessionStorage 方法针对一个session 进行数据存储.当用户关闭浏览器窗口后,数据会 ...

  3. 20180307-Xen、KVM、VMware、hyper-v等虚拟化技术的比较

    xen和kvm,是开源免费的虚拟化软件. vmware是付费的虚拟化软件. hyper-v比较特别,是微软windows 2008 R2附带的虚拟化组件,如果你买了足够的授权,hyper-v(包括hy ...

  4. centos7之安装wordpress

    wordpress安装教程如下: mysql安装可以参考我的博客园Centos构建Java环境:https://www.cnblogs.com/youcong/p/9118753.html 1.安装a ...

  5. SVM学习笔记2-拉格朗日对偶

    下面我们抛开1中的问题.介绍拉格朗日对偶.这一篇中的东西都是一些结论,没有证明. 假设我们有这样的问题:$min_{w}$ $f(w)$,使得满足:(1)$g_{i}(w)\leq 0,1\leq i ...

  6. Linux电源管理(五)thermal【转】

    本文转载自:https://blog.csdn.net/zhouhuacai/article/details/78172267 版权声明:本文为博主原创文章,未经博主允许不得转载.    https: ...

  7. CentOS 使用 Docker 安装 Sentry

    官网介绍:Sentry是一个实时事件日志记录和汇集的日志平台,其专注于错误监控,以及提取一切事后处理所需的信息.他基于Django开发,目的在于帮助开发人员从散落在多个不同服务器上的日志文件里提取发掘 ...

  8. Almost Union-Find (并查集+删除元素)题解

    思路: 当时只有暴力的思想,1.3都很好达到,只要加一个sum[ ]和num[ ]就可以,但是2比较麻烦,不知道谁以p为根,就想直接扫描然后一个个和p脱离关系,果然超时emmm.通常的想法是我们先用一 ...

  9. oracle 之 定时任务,存储过程和游标等语法案例

    --定时任务 declare job20 number; begin sys.dbms_job.submit(job20,'test1;',sysdate,'sysdate+1'); end; --存 ...

  10. p3396 哈希冲突(暴力)

    想了好久,没想到优秀的解法,结果是个暴力大吃一静.jpg 分类讨论,预处理\(p\le \sqrt{n}\) 的情况,其他直接暴力,复杂度\(O(n \sqrt{n} )\) #include < ...