hiho week 177

本文介绍了一种使用ASCII字符绘制满二叉树的方法,并通过建立坐标系来精确计算给定矩形区域内的节点数量。文章提供了一个算法实现示例,包括递归绘制树形结构和查找特定区域内节点数的过程。
hiho week 177


Description
Let's draw a picture of full binary tree using ASCII characters. In this picture nodes are represented by '#'. A parent node connects its left child by '/' and its right child by '\'.


For the sake of aesthetic, the nodes of the same height must be painted on the same line. And the nodes must be perfectly connected by '/' and '\'. Intersections or misplacements are not allowed.


For example, this is the full binary tree of height 2:


  #
 / \
#   #
This is the full binary tree of height 3:


     #
    / \
   /   \
  #     #
 / \   / \
#   # #   #
This is the full binary tree of height 4:


           #
          / \
         /   \
        /     \
       /       \
      /         \
     #           #
    / \         / \
   /   \       /   \
  #     #     #     #
 / \   / \   / \   / \
#   # #   # #   # #   #
Now we build a Cartesian coordinate system for the picture. We make the root at the origin (0, 0). The positive direction of x is to the bottom and the positive direction of y is to the right.


The full binary tree of height 2 is illustrated as below.


       0
 +-----+--------> y
 |
0+     #(0,0)
 |    / \
 |   #   #
 |(2,-2) (2,2)
 |
 v
x
Given the height of the tree and a rectangle area of the picture, your task is to find out the amount of nodes in such area. For example, assuming the height of tree is 2, the left-top corner of the rectangle area is at (0, 0) and the right-bottom corner is at (2, 2), then the area contains 2 nodes (the root and its right child) totally. 


Input
The first line contains two integers N and M. N is the height of the full binary tree. M is the number of queries. (1 ≤ N ≤ 20, 1 ≤ M ≤ 100)
Each query consists of 4 integers x1, y1, x2 and y2. (x1, y1) is the left-top corner of the area and (x2,y2) is the right-bottom corner of the area.


Output
For each query output the amount of nodes in the area.


Sample Input
2 3
0 0 0 0
0 0 2 2
0 -2 2 2
Sample Output
1
2



#include <bits/stdc++.h>
using namespace std;
struct node
{
    int x,y;
}b[1001000];
int n,m,cnt;
void dfs(int x,int y,int tmp,int now)
{
    if(tmp==1)tmp=2;
    if(now>=n)return;
        b[cnt].x=x+tmp;
        b[cnt++].y=y-tmp;
         b[cnt].x=x+tmp;
        b[cnt++].y=y+tmp;
        dfs(x+tmp,y-tmp,tmp/2,now+1);
        dfs(x+tmp,y+tmp,tmp/2,now+1);
}
int find1(int x,int y)
{
    int ans=0;
    for(int i=0;i<cnt;i++)
    {


        if(b[i].x<=x&&b[i].y<=y)
        {
           // cout<<b[i].x<<" "<<b[i].y<<" "<<i<<endl;
            ans++;}
    }
    return ans;
}
int main()
{


int a[25];
scanf("%d%d",&n,&m);
a[1]=2;
a[2]=3;
for(int i=3;i<=21;i++)
    a[i]=a[i-1]*2;
    int tmp=a[n];
tmp/=2;
cnt=0;
b[cnt].x=0;
b[cnt++].y=0;
dfs(0,0,tmp,1);
while(m--)
{
    int x1,y1,x2,y2;
    scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
    x1--;
    y1--;
    //cout<<find1(x2,y2)<<endl;
    printf("%d\n",find1(x1,y1)+find1(x2,y2)-find1(x1,y2)-find1(x2,y1));
}
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值