[Hihocoder week177]Full Binary Tree Picture
来源:互联网 发布:迅捷路由器绑定mac干嘛 编辑:程序博客网 时间:2024/06/05 07:35
Hihocoder Week 177 — Full Binary Tree Picture (O(N)复杂度)
Problem
题目链接
Idea
Some solutions gives by others need to traverse the whole full binary tree and check each nodes to get the answer. Here we put forward a solution whose time complexity is
The key idea of the solution is to get the count of nodes in the range of [y1, y2]
in each layer. If the [x1, x2]
, we will add the count to the answer.
Note that we can mark nodes in a full binary tree with numbers from 1 to snode
and enode
denote the start node ant the end node of the satisfied nodes sequence.
In the solution, we use two variables to capture the indexes of snode
and enode
, i.e., y_min
and y_max
. Their indexes are stored in y_min_idx
and y_max_idx
. At the start of the algorithm, we initialize their values as y_min = y_max = 0
and y_min_idx = y_max_idx = 0
, for we start to go from the root.
Our algorithm can be divided into two procedures.
In the first procedure,
y_min==y_max
. And we need to check whethersnode
andenode
can go two side (left child and right child). We can see that only ify_min/y_max
is in the range of[y1, y2]
, we can bi-pathsnode
andenode
to left child and right child. This is because the range between them potentially have an intersection with [y1, y2]. Otherwisey_min/y_max
is in the outside of[y1, y2]
. This time,y_min/y_max
need to go close toy1
ory2
.In the second procedure,
y_min!=y_max
. And we need to checky_min
andy_max
to make sure whether they are in the range of[y1, y2]
. If not, fory_min
,snode
will go to its right child; fory_max
,enode
will go to its left child. We have mentioned that[y_min, y_max]
may have an intersection with[y1, y2]
. Ify_min
is out of the range, we set tempsnode
to its right sibling node. Ify_max
is out of the range, we set tempenode
to its left sibling node. If thex coordinate of the current layer is in the range of[x1, x2]
, we add count of nodes betweensnode
andenode
.
The step (height difference) between two adjcent layer is easy to compute, and we omit here.
Code
// time: 0ms, momory: 0MB#include <string.h>#include <stdio.h>int n, m;int x1[110];int y1[110];int x2[110];int y2[110];int height[20];void cal(int a1, int b1, int a2, int b2){ int i; int ans = 0; int x = 0; int y_max = 0; int y_min = 0; int y_max_idx = 1; int y_min_idx = 1; int tmp_max, tmp_min; if(a1 > a2 || b1 > b2){ printf("0\n"); return; } for(i = 1; i <= n; i++){ if(x > a2) break; if(y_min == y_max){ // check whether in the range if(y_min >= b1 && y_min <= b2){ //good, go two sides if(x >= a1){ ans++; } y_min -= (height[i] + 1); y_min_idx = y_min_idx * 2; y_max += (height[i] + 1); y_max_idx = y_max_idx * 2 + 1; } else { // bad , both go one side if(y_min < b1) { // both go right y_min += (height[i] + 1); y_min_idx = y_min_idx * 2 + 1; y_max += (height[i] + 1); y_max_idx = y_max_idx * 2 + 1; } else { // both go left y_min -= (height[i] + 1); y_min_idx = y_min_idx * 2; y_max -= (height[i] + 1); y_max_idx = y_max_idx * 2; } } } else { // has go two side tmp_max = (y_max > b2) ? (y_max_idx - 1) : y_max_idx; tmp_min = (y_min < b1) ? (y_min_idx + 1) : y_min_idx; // if(tmp_max >= tmp_min) { if(x >= a1){ ans += (tmp_max - tmp_min + 1); } // } if(y_max > b2){ // go left y_max -= (height[i] + 1); y_max_idx = y_max_idx * 2; } else { // continue go right y_max += (height[i] + 1); y_max_idx = y_max_idx * 2 + 1; } if(y_min < b1) { // go right y_min += (height[i] + 1); y_min_idx = y_min_idx * 2 + 1; } else { // continue go left y_min -= (height[i] + 1); y_min_idx = y_min_idx * 2; } } x += (height[i] + 1); } printf("%d\n", ans);}void solve(){ int i; memset(height, 0, sizeof(height)); // calculate layer step if(n > 1) height[n-1] = 1; if(n > 2) height[n-2] = 2; for(i = n-3; i >= 1; i--){ // for(int j = n-1; j > i; j--){ // height[i] += (height[j] + 1); // } height[i] = height[i+1] * 2 + 1; } for(int i = 0; i < m; i++){ cal(x1[i], y1[i], x2[i], y2[i]); }}int main(int argc, char const *argv[]){ int i;#ifndef ONLINE_JUDGE freopen("out", "r", stdin);#endif scanf("%d %d", &n, &m); for(i = 0; i < m; i++){ // cin >> x1[i] >> y1[i] >> x2[i] >> y2[i]; scanf("%d %d %d %d", &x1[i], &y1[i], &x2[i], &y2[i]); } solve(); return 0;}
- [Hihocoder week177]Full Binary Tree Picture
- [HihoCoder]#1342 : Full Binary Tree Picture
- Full Binary Tree Picture——hihoCoder177
- Full Binary Tree Picture hiho一下第 177 周
- 2016微软探星夏令营在线技术笔试-#1342 : Full Binary Tree Picture
- Full Binary Tree
- Difference between Full Binary Tree and Complete Binary Tree
- full binary tree != 满二叉树
- Full Binary Tree(GCJ2014 round1A PB)
- 山东省第五届省赛回顾 Full Binary Tree
- 第五届山东省ACM Full Binary Tree
- SDUT 2882 Full Binary Tree【思维】
- 【hihocoder】94. Binary Tree Inorder Traversal
- Construct Full Binary Tree from given preorder and postorder traversals
- 2014山东省第五届ACM省赛 Full Binary Tree
- 山东省第五届ACM大学生程序设计竞赛 Full Binary Tree
- 第五届ACM大学生程序设计竞赛:Full Binary Tree
- 2014山东省第五届ACM省赛 Full Binary Tree
- 我的学习记录45
- 逻辑、条件、逗号表达式操作符
- Codeforces Educational Codeforces Round 33 C 893CRumor(并查集)
- java选择判断语句(switch)
- 下标引用、函数调用、结构体操作符
- [Hihocoder week177]Full Binary Tree Picture
- ubuntu软件包系统已损坏
- 本地安装nexus私服步骤以及配置到本地maven插件中
- spark 两个rdd求交集,差集,并集
- java list集合去重复
- Java EE从零开始-0001 Hello World !
- Linux学习内容总结2017/11/19(linux文本编辑器及基本进程管理)
- 我喜欢上你时的内心活动
- 使用MATLAB中的HDL coder工具箱,对一个线性调频信号进行FIR滤波,最终在FPGA上实现