# CodeForces960DFull Binary Tree Queries(二叉树的操作)

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 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
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:

#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;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120