题意 : 给出一颗无限层的满二叉树,然后每个值为 X (根的 X 等于 1 ) 左孩子的值是 2*X,右孩子的值是 2*X+1 ,现在有两种操作,(1, x,k) 表示将 x 所在层的所有节点整体向右循环地移动 k 个单位、(2,x,k)表示将 x 所在的层的所有节点及其子树向右循环地移动 k 个单位、(3,x)输出从 x 到根的路径

分析 :

第一个操作不难进行模拟

只要给每个层记录其偏移量,最后查询的时候依据偏移量来输出具体的值即可

关键是第二个操作

仔细一想,会发现一个规律

如果第一层移动 k 个单位

那么对于第二种操作,其孩子分别移动了 k*2^1、k*2^2、k*2^3……

所以其本质就是进行第一种操作,偏移量不一样了而已

查看了题目数据量之后发现每一次只进行到第 60 层

所以模拟到第 60 层就是回答所有问询了

#include<bits/stdc++.h>
#define LL long long
using namespace std;
;
LL Move1[maxn];///正移动偏移量,方便知道原节点去了哪个位置
LL Move2[maxn];///反移动偏移量,方便知道原节点现在是什么值

inline void op1(int Lay, LL move, LL x, LL times)
{
    ) Lay = log2(x) + ;///获取 x 在第几层
    LL Base = 1LL<<(Lay-);///这一层有多少个节点

    ) move %= Base;///如果非第二种操作
    else move = ( (move % Base) * (times % Base) ) % Base;///第二种操作

    Move1[Lay] = (( Move1[Lay] + move) + Base ) % Base;///正偏移量直接移动即可
    Move2[Lay] = (( Move2[Lay] - move) + Base ) % Base;///反偏移量要反向移动
}

inline void op2(LL move, LL x)
{
    ;
    ; i<; i++,j++)///最多到60层
        op1(i, move, , 1LL<<j);
}

inline void op3(LL x)
{
    if(x == 1LL){
        puts(");
        return;
    }
    printf("%I64d", x);
    ;
    LL Base = 1LL<<(Lay-);
    LL pos = (( (x - Base) + Move1[Lay] ) + Base) % Base + Base;
    while(!(pos == 3LL || pos == 2LL)){

        ) pos = (pos - )>>;///找到当前节点的父亲节点
        ;

        Lay = log2(pos) + ;
        Base = 1LL<<(Lay-);

        printf(" %I64d", ( (pos - Base + Move2[Lay]) + Base ) % Base + Base);///通过反向偏移量来获取在这个节点位置的值
    }puts(");
}

int main(void)
{
    int Q;
    scanf("%d", &Q);
    while(Q--){
        int command;
        LL X, K;
        scanf("%d", &command);
        ){
            scanf("%I64d %I64d", &X, &K);
            op1(-, K, X, -);
        }){
            scanf("%I64d %I64d", &X, &K);
            op2(K, X);
        }else{
            scanf("%I64d", &X);
            op3(X);
        }
    }
    ;
}

Codeforces 960D Full Binary Tree Queries ( 思维 && 模拟 )的更多相关文章

  1. Codeforces 960D - Full Binary Tree Queries

    960D - Full Binary Tree Queries 思路: 用move1[i]记录第i层第1种操作移动的个数(对这一层的个数取模) 用move2[i]记录第i层第2种操作移动的个数(对这一 ...

  2. hdu 6161--Big binary tree(思维--压缩空间)

    题目链接 Problem Description You are given a complete binary tree with n nodes. The root node is numbere ...

  3. Codeforces 758C:Unfair Poll(思维+模拟)

    http://codeforces.com/problemset/problem/758/C 题意:教室里有n列m排,老师上课点名从第一列第一排开始往后点,直到点到第一列第m排,就从第二列第一排开始点 ...

  4. codeforces C. Vasya And The Mushrooms (思维+模拟)

    题意:给定一个2*n的矩形方格,每个格子有一个权值,从(0,0)开始出发,要求遍历完整个网格(不能重复走一个格子),求最大权值和,(权值和是按照step*w累加,step步数从0开始). 转载: 题解 ...

  5. codeforce 1311E. Construct the Binary Tree (构造,就是个模拟)

    ACM思维题训练集合 You are given two integers n and d. You need to construct a rooted binary tree consisting ...

  6. Binary Tree(二叉树+思维)

    Binary Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

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

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

  8. Binary Tree HDU - 5573 (思维)

    题目链接: B - Binary Tree  HDU - 5573 题目大意: 给定一颗二叉树,根结点权值为1,左孩子权值是父节点的两倍,右孩子是两倍+1: 给定 n 和 k,让你找一条从根结点走到第 ...

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

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

随机推荐

  1. 3、APK file does not exist on disk

    参考:https://blog.csdn.net/ouyang_peng/article/details/51243441 亲测可以解决

  2. Springboot与springcloud

    1.什么是Spring Boot? 它简化了搭建Spring项目,自动配置Spring,简化maven配置,自带tomcat无需部署war包,创建独立的spring引用程序main方法运行: 2.Sp ...

  3. 数位dp(二进制01问题)

    http://poj.org/problem?id=3252 题意:给你一个区间,求区间有多少个满足条件的数.条件是:把该数转为二进制后,如果0的数量大于等于1的数量,则为满足条件的数量. 题解:数位 ...

  4. opencv学习之等待按键事件-waitKey函数

    文章来源: https://mangoroom.cn/opencv/opencv-learning-waitKey.html 序 waitKey函数属于opencv函数里既常用又非常基础的函数,无论是 ...

  5. java中常见的异常种类

    Java常见的异常种类 ------------------------------------------------------------------------------- java Exc ...

  6. Action获取请求参数的3中方式

    方式一:Action本身作为Model对象,通过属性注入(Setter)方法讲参数数据封装到Action中 具体为:在Action中,提供和参数名相同的几个属性,并为其提供set方法,那么,该参数会被 ...

  7. React-native 底部导航栏(二)

    1.组件安装:npm install react-native-router-flux --save 2.定义菜单图片和文字: import React, { Component } from 're ...

  8. git 本地tag和远程tag对应不上 vscode里pull不下代码

    vscode拉取代码是用  git pull --tags origin saas-xxx > git pull --tags origin saas-base From 172.16.0.xx ...

  9. CSS3 @keyframes 实现匀速旋转魔方(搬运工)

    原博文 * https://www.cnblogs.com/shihao905/p/6208324.html * html <div id="wrap"> <di ...

  10. 几个常用I/O函数用法(printf,fprintf等)

    一 格式化输出 1.printf 定义:int printf(const char *format,[argument]); 功能:产生格式化输出的函数(定义在 stdio.h 中) 参数说明:for ...