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

标签: 二叉树 思维乱搞
27人阅读 评论(0) 收藏 举报
分类:

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:


题意:无限长度的二叉树,每次操作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;
}


查看评论

Python算法实战视频课程--二叉树

-
  • 1970年01月01日 08:00

Paths in a Complete Binary Tree codeforces

D. Paths in a Complete Binary Tree time limit per test 3 seconds memory limit per test 256 megab...
  • Courage_kn
  • Courage_kn
  • 2017-04-04 23:19:02
  • 341

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

D. Full Binary Tree Queries time limit per test 4 seconds memory limit per test 256 mega...
  • Haipai1998
  • Haipai1998
  • 2018-04-08 23:33:17
  • 38

2014年山东省第五届ACM--Full Binary Tree (满二叉树)

Full Binary Tree Time Limit: 2000MS Memory limit: 65536K 题目描述 In computer science, a binary t...
  • kavu1
  • kavu1
  • 2016-04-10 15:58:38
  • 282

Full Binary Tree

题目描述 In computer science, a binary tree is a tree data structure in which each node has at most...
  • HYNU_zhizuzhe
  • HYNU_zhizuzhe
  • 2014-08-29 11:04:58
  • 600

二叉树(Binary Tree)

二叉树(Binary Tree)是n个结点的有限集合,该集合或者为空集(称为空二叉树),或者由一个根结点和两棵互不相交的、分别称为根结点的左子树和右子树的二叉树组成。 二叉树特点: 每个结点最多有两棵...
  • dengpei187
  • dengpei187
  • 2016-07-12 20:46:02
  • 6229

D. Paths in a Complete Binary Tree

T is a complete binary tree consisting of n vertices. It means that exactly one vertex is a root, ...
  • qq_33362864
  • qq_33362864
  • 2017-03-28 22:01:08
  • 228

leetcode 226 Invert Binary Tree 翻转二叉树

大牛没有能做出来的题,我们要好好做一做     Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to ...
  • wangyaninglm
  • wangyaninglm
  • 2015-06-23 23:53:58
  • 3060

D. Paths in a Complete Binary Tree CodeForces - 792D

T is a complete binary tree consisting of n vertices. It means that exactly one vertex is a root, an...
  • qq_36346262
  • qq_36346262
  • 2017-04-25 19:03:57
  • 169

Leetcode 226: Invert Binary Tree(二叉树反转 递归、非递归实现)

nvert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...
  • sunao2002002
  • sunao2002002
  • 2015-06-13 14:41:43
  • 14949
    个人资料
    持之以恒
    等级:
    访问量: 1万+
    积分: 1326
    排名: 3万+
    最新评论