codeforces 792D - Paths in a Complete Binary Tree
2017-04-24 21:27
344 查看
#include<cstdio> #include<iostream> #define lowbit(x) x&(-x) typedef long long ll; using namespace std; ll n,q,num,root;string s; int main() { scanf("%I64d%I64d",&n,&q); root=(n+1)/2; for(ll q_i=1;q_i<=q;q_i++) { cin>>num>>s; for(int step=0;step<s.size();step++) { ll lowbit_num=lowbit(num);//假设num这个节点是左子节点 if(s[step]=='U' && num!=root) { ll num_u=num+lowbit_num;//求出在假设情况下的num的父节点num_u ll lowbit_num_u=lowbit(num_u); if(num_u - lowbit_num_u/2 == num) num=num_u;//如果根据父节点求出来的左孩子就是num,那么num确实是左子节点 else num=num-lowbit_num;//否则num就是右子节点 } if(s[step]=='L') num-=lowbit_num/2; if(s[step]=='R') num+=lowbit_num/2; } printf("%I64d\n",num); } }
思路来自http://blog.csdn.net/Courage_kn/article/details/69218592
用#define比定义一个lowbit函数快……不过好像很多时候不能像函数那样随便用,容易出问题……
这是分别用
long long lowbit(long long x){return x&(-x);}
和
#define lowbit(x) x&(-x)
情况下的耗时……
相关文章推荐
- 【codeforces 792D】Paths in a Complete Binary Tree
- codeforces 792D——Paths in a Complete Binary Tree(二叉树,数学)
- CF 792D - Paths in a Complete Binary Tree lowbit,模拟
- Codeforces 792 D. Paths in a Complete Binary Tree
- [CF792D] Paths in a Complete Binary Tree (规律, 位运算, lowbit)
- D. Paths in a Complete Binary Tree
- Educational Codeforces Round 18 -- D. Paths in a Complete Binary Tree(二叉树模拟)
- Educational Codeforces Round 18 D. Paths in a Complete Binary Tree
- CF792D:Paths in a Complete Binary Tree(数学 & 规律)
- No. 04 - Paths with Specified Sum in Binary Tree
- [Google] print the outline of a complete binary tree in anti-clockwise direction
- Groupon面经:Find paths in a binary tree summing to a target value
- CodeForces 750G. New Year and Binary Tree Paths
- 4.在二元树中找出和为某一值的所有路径[FindPathsInBinaryTree]
- Binary Tree Inorder Traversal ---LeetCode
- Search Range in Binary Search Tree
- leetcode_105题——Construct Binary Tree from Preorder and Inorder Traversal(树,递归)
- Construct Binary Tree from Preorder and Inorder Traversal
- 94. Binary Tree Inorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversa