Full Binary Tree Queries
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.
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 ≤ 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).
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 Copy
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 Copy
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:
題意:無限長度的二叉樹,每次操作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];
}
int main()
{
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)//子樹旋轉次數
{
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];//逆向求哪個數到了這個位置
printf("%lld ",c[deep]+tmp);
deep--;
pos = pos/2;
}
printf("\n");
}
}
return 0;
}