[CF792D] Paths in a Complete Binary Tree (规律, 位运算, lowbit)

题目链接:http://codeforces.com/problemset/problem/792/D

画出树,找找规律,画图就好了。不算麻烦。

往下走的时候特判是不是叶子,往上走的时候特判是不是根。其余时候按照规律转移就是。

感觉可以推广到建树上,可以缩小常数是极好的。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 typedef long long LL;
 5 LL lowbit(LL x) { return x & (-x); }
 6 const int maxn = 100010;
 7 LL n, u;
 8 int q;
 9 char s[maxn];
10
11 signed main() {
12     // freopen("in", "r", stdin);
13     LL x, y, rt;
14     while(~scanf("%lld%d",&n,&q)) {
15         rt = 1;
16         while((1LL << rt) < n) rt++;
17         rt = 1LL << rt; rt >>= 1;
18         while(q--) {
19             scanf("%lld%s",&u,s);
20             bool ok = 1;
21             for(int i = 0; s[i]; i++) {
22                 x = u; y = lowbit(x);
23                 if(s[i] == ‘L‘) {
24                     if(u & 1) continue;
25                     x ^= y; x |= (y >> 1);
26                     if(x >= 1) u = x;
27                 }
28                 else if(s[i] == ‘R‘) {
29                     if(u & 1) continue;
30                     x |= (y >> 1);
31                     if(x <= n) u = x;
32                 }
33                 else {
34                     if(x == (x ^ y)) {
35                         if(u != rt) u = (x ^ y);
36                     }
37                     else {
38                         if(u != rt) u = (x ^ y) | (y << 1);
39                     }
40                 }
41             }
42             printf("%lld\n", u);
43         }
44     }
45     return 0;
46 }
时间: 2025-01-02 03:39:16

[CF792D] Paths in a Complete Binary Tree (规律, 位运算, lowbit)的相关文章

PAT1110:Complete Binary Tree

1110. Complete Binary Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each input file contains one test case. For each case, th

PAT 1110 Complete Binary Tree[比较]

1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤20) which is the total nu

PAT甲级——1110 Complete Binary Tree (完全二叉树)

此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830 1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each input file contains one test case. For eac

1110 Complete Binary Tree (25分) 判断一棵二插树是否是完全二叉树

Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤) which is the total number of nodes in the tree -- and he

UVA11127- Triple-Free Binary Strings(DFS+位运算)

题目链接 题意:给出长度为n的字符串,字符串由'1','0','*'组成,其中'*'可以任意替换为'1','0',求不存在连续3个相同子串的字符串的最多个数. 思路:我们可以利用二进制的形式来表示字符串,进行DFS.利用位运算的左移来表示在'*'位置上放置'1',注意在递归的过程中注意判断之否存在3个连续相同的子串. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <alg

leetcode_919. Complete Binary Tree Inserter

https://leetcode.com/problems/complete-binary-tree-inserter/ 设计一个CBTInserter,使用给定完全二叉树初始化.三个功能; CBTInserter(TreeNode root) initializes the data structure on a given tree with head node root; CBTInserter.insert(int v) will insert a TreeNode into the t

PAT甲题题解-1110. Complete Binary Tree (25)-(判断是否为完全二叉树)

题意:判断一个节点为n的二叉树是否为完全二叉树.Yes输出完全二叉树的最后一个节点,No输出根节点. 建树,然后分别将该树与节点树为n的二叉树相比较,统计对应的节点个数,如果为n,则为完全二叉树,否则即不是. #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> using namespace std; const int maxn=22; int two

PAT (Advanced Level) 1110. Complete Binary Tree (25)

判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; int n,root; const int maxn=30; struc

Leetcode-919 Complete Binary Tree Inserter(完全二叉树插入器)

1 vector<TreeNode> ve(16385,0); 2 class CBTInserter 3 { 4 public: 5 queue<TreeNode*> q; 6 int veEnd; 7 8 CBTInserter(TreeNode* root) 9 { 10 TreeNode rubbish(0); 11 veEnd = 0; 12 ve[veEnd++] = (rubbish); 13 q.push(root); 14 Init(); 15 for(int i