Codeforces Round #474 D. Full Binary Tree Queries【思维】

D. Full Binary Tree Queries
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You have a full binary tree having infinite levels.

Each node has an initial value. If a node has value x, then its left child has value x and its right child has value x + 1.

The value of the root is 1.

You need to answer Q queries.

There are 3 types of queries:

  1. Cyclically shift the values of all nodes on the same level as node with value X by K units. (The values/nodes of any other level are not affected).
  2. Cyclically shift the nodes on the same level as node with value X by K units. (The subtrees of these nodes will move along with them).
  3. Print the value of every node encountered on the simple path from the node with value X to the root.

Positive K implies right cyclic shift and negative K implies left cyclic shift.

It is guaranteed that atleast one type 3 query is present.

Input

The first line contains a single integer Q (1 ≤ Q ≤ 105).

Then Q queries follow, one per line:

  • Queries of type 1 and 2 have the following format: T X K (1 ≤ T ≤ 21 ≤ X ≤ 10180 ≤ |K| ≤ 1018), where T is type of the query.
  • Queries of type 3 have the following format: 3 X (1 ≤ X ≤ 1018).
Output

For each query of type 3, print the values of all nodes encountered in descending order.

Examples
input
Copy
5
3 12
1 2 1
3 12
2 4 -1
3 8
output
12 6 3 1 
12 6 2 1 
8 4 2 1 
input
Copy
5
3 14
1 5 -3
3 14
1 3 1
3 14
output
14 7 3 1 
14 6 3 1 
14 6 2 1 
Note

Following are the images of the first 4 levels of the tree in the first test case:

Original:

After query 1 2 1:

After query 2 4 -1:


题意:三种操作

1.把值为x的那一层节点全部往右移k,子树不动

2.把值为x的那一层节点全部往右移k,子树一起走

3.问从节点编号为12的节点到根节点1的路径


思路:

核心:记录下每个深度往右移动多少次

模拟题意即可

注意位运算1LL

#include<bits/stdc++.h>
#define PI acos(-1.0)
using namespace std;
typedef long long ll;

const int N=1e5+5;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;

int level(__int64 x){//获得值为x节点的深度
//    cout <<"x="<<x<<endl;
    int cnt=0;
    while(x){
        x/=2;
        cnt++;
    }
    return cnt;
}
__int64 sum[70];
__int64 shift[70];
int main(void){
    ll q,x,k,op;

    for(int i=1;i<=64;i++)  sum[i]=1ll<<(i-1);
//    cout<<sum[60]<<endl;
    cin >>q;
    for(ll i=1;i<=q;i++){
        scanf("%I64d",&op);
        if(op==1){
            scanf("%I64d%I64d",&x,&k);
            int dep=level(x);
            if(k>0) shift[dep]+=k;
            else    shift[dep]+= sum[dep] +k;//左移k等价右移sum[dep]+k
            shift[dep]=(shift[dep]%sum[dep]+sum[dep])%sum[dep];
        }
        else if(op==2){
            scanf("%I64d%I64d",&x,&k);
            int dep=level(x);
            ll tot=1;
            for(;dep<64;dep++){//相对dep的深度决定要移动的个数
                if(k>0) shift[dep]+=k%sum[dep]*tot%sum[dep];
                else    shift[dep]+= (sum[dep]+k)%sum[dep]*tot%sum[dep];
                shift[dep]=(shift[dep]%sum[dep]+sum[dep])%sum[dep];
                tot*=2;
            }
        }
        else{
           scanf("%I64d",&x);
           int dep=level(x);
           for(int i=dep;i>=1;i--){
                printf("%I64d ",x);
                x+=shift[i];//先求出当前x移动到的位置
                if(x>= (1ll<<i))  x-=(1ll<<(i-1));
                x/=2;//需要的x
                x+=(sum[i-1]-shift[i-1]);//哪一个编号移动到需要的x
                if(x>= (1ll<<(i-1)))  x-=(1ll<<(i-2));
           }
            puts("");
        }
    }


    return 0;
}


猜你喜欢

转载自blog.csdn.net/haipai1998/article/details/79859881