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 2·x and its right child has value 2·x + 1.
The value of the root is 1.
You need to answer Q queries.
There are 3 types of queries:
- 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).
- 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).
- 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.
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 ≤ 2; 1 ≤ X ≤ 1018; 0 ≤ |K| ≤ 1018), where T is type of the query.
- Queries of type 3 have the following format: 3 X (1 ≤ X ≤ 1018).
For each query of type 3, print the values of all nodes encountered in descending order.
5 3 12 1 2 1 3 12 2 4 -1 3 8
12 6 3 1 12 6 2 1 8 4 2 1
5 3 14 1 5 -3 3 14 1 3 1 3 14
14 7 3 1 14 6 3 1 14 6 2 1
Following are the images of the first 4 levels of the tree in the first test case:
题意:无限长度的二叉树,每次操作1把包含数x的那一层整体移动k个位置,正往右,负往左,操作2把包含数x的那一层带着子树移动k个位置.操作3打印从x到根沿线的所有数.
思路:对于每一层我们可以记录旋转了多少次,因为最多有60+层.对于操作2它的子树就分别移动k^2次,k^4次...,对于查询操作,我们可以先找到x的位置,然后依次除以2就是上层的对应位置,根据旋转次数打印相应数字即可.
代码:
#include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define mod 1000000007 using namespace std; typedef long long ll; const int maxn = 1e6+5; const double esp = 1e-7; const int ff = 0x3f3f3f3f; map<int,int>::iterator it; ll d[110]; ll c[110]; void init() { c[1] = 1; for(int i = 2;i< 63;i++) c[i] = c[i-1]*2; } ll get_deep(ll x) { ll tmp = 1,cnt = 0; while(tmp<= x) tmp*= 2,cnt++; return cnt; } ll get_pos(ll x)//找x的当前位置 { ll tmp = 1,cnt = 0; while(tmp<= x) tmp*= 2,cnt++; ll pos = x-tmp/2; return (pos+d[cnt])%c[cnt]; } intmain() { init(); int t; cin>>t; while(t--) { ll o,x,k,deep,pos; scanf("%lld",&o); if(o == 1) { scanf("%lld %lld",&x,&k); deep = get_deep(x); k%= c[deep]; d[deep] = (d[deep]+k+c[deep])%c[deep]; } else if(o == 2) { scanf("%lld %lld",&x,&k); deep = get_deep(x); while(deep<= 62)//Number of subtree rotations { k%= c[deep]; d[deep] = (d[deep]+k+c[deep])%c[deep]; k*= 2; deep++; } } else { scanf("%lld",&x); deep = get_deep(x); pos = get_pos(x); while(deep>= 1) { ll tmp = (pos+c[deep]-d[deep])%c[deep];//Reverse search which number reaches this position printf("%lld ",c[deep]+tmp); deep--; pos = pos/2; } printf("\n"); } } return 0; }