Codeforces 960D - Full Binary Tree Queries
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的更多相关文章
- Codeforces 960D Full Binary Tree Queries ( 思维 && 模拟 )
题意 : 给出一颗无限层的满二叉树,然后每个值为 X (根的 X 等于 1 ) 左孩子的值是 2*X,右孩子的值是 2*X+1 ,现在有两种操作,(1, x,k) 表示将 x 所在层的所有节点整体向右 ...
- Codeforces 250 E. The Child and Binary Tree [多项式开根 生成函数]
CF Round250 E. The Child and Binary Tree 题意:n种权值集合C, 求点权值和为1...m的二叉树的个数, 形态不同的二叉树不同. 也就是说:不带标号,孩子有序 ...
- 九章算法系列(#3 Binary Tree & Divide Conquer)-课堂笔记
前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode ...
- Full Binary Tree(二叉树找规律)
Description In computer science, a binary tree is a tree data structure in which each node has at mo ...
- Full Binary Tree(sdut 2882)
Problem Description: In computer science, a binary tree is a tree data structure in which each node ...
- 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 ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- 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 ...
随机推荐
- 搭建一个简单的svn服务器(旧)
cenos 6.5,svnserver 1.6.11 默认可能已经安装,没有的话就: yum install svn -ysvnserver --version 创建一个svn仓库: svnadmin ...
- nginx动静态分离以及配置https(安全组强行切换以及导致的问题解决)
公司原来的网络采用http/https同时支持的方式,http并不会强制自动跳转到https,最近要求强制切换,导致了一系列问题.趁今天测试完成了,整理如下: 1.要求HTTP自动跳转到HTTPS: ...
- 08: MySQL慢查询
1.1 寻找慢查询 定义:我们将超过指定时间的SQL语句查询称为“慢查询”. 1.在mysql日志中开启慢查询日志 1. 修改配置文件 在 my.ini 增加几行: 主要是慢查询的定义时间(超 ...
- oracle RAC如何正确地删除ASM磁盘组
1.登录到命令行 切换到grid用户 [grid@swnode1 ~]$ sqlplus / as sysasm SQL*Plus: Release Production on Wed May :: ...
- linux 中部署 rsync 实现文件远程备份及 同步
客户端:数据源:服务端:数据接收方 rsync官方文档:https://www.samba.org/ftp/rsync/rsync.html 手动测试用“通过远程外壳访问"里的语法: 参考1 ...
- 集合框架-Map集合
* Map集合和Collection集合的区别? * Map集合存储元素是成对出现的,Map集合的键是唯一的,值是可重复的.可以把这个理解为:夫妻对 * Collection集合存储元素是单独出现的, ...
- 前后台分离开发--文件上传与下载,cookie,session
一.前后台分离开发的概念 ''' 1. 前台页面运行在前台服务器上,负责页面的渲染(静态文件的加载)与转跳 2. 后台代码运行在后台服务器上,负责数据的处理(提供数据请求的接口) ''' #如果没有前 ...
- topcoder srm 694 div1 -3
1.给出$n$个数字,将其分成三个非空的组,每组的权值为该组所有数字的抑或.选择一种分法使得三组的权值和最大? 思路:记录前两组的权值且三组有没有数字时第三组的值.(当前两组的值知道时第三组的权值是确 ...
- 流程控制if,while,for
if语句 什么是if语句 判断一个条件如果成立则做...不成立则做....为何要有if语句 让计算机能够像人一样具有判断的能力 如何用if语句 语法1: if 条件1: code1 code2 cod ...
- Derek解读Bytom源码-protobuf生成比原核心代码
作者:Derek 简介 Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom ...