2016微软探星夏令营在线技术笔试-#1342 : Full Binary Tree Picture

http://hihocoder.com/problemset/problem/1342

题意理解:找规律,N=1,2是特殊的,之后的就有规律了:y坐标最下一层是2,4,2,4的差,最后就是差6, 12, 24.。。。x左边最下差2, 之后是3,6,12.。。。 一行一行的算,算某一行的时候,写出来最左边为第一项的等差数列通项,然后用f(y2) - f(y1 - 1)来算

急转弯:有点麻烦

算法:无

数据结构:无

from __future__ import print_function
#
#

'guilv'

__author__ = 'hjkruclion'

import sys
import math

def read_int():
    return list(map(int, sys.stdin.readline().split()))

maxn = 50
x = [[] for i in range(maxn)]
y = [0 for i in range(maxn)]
delt_y = [0 for i in range(maxn)]

N, M = read_int()

t = 3
if N >= 2:
    delt_y[N - 2] = 2
for i in reversed(range(0, N - 2)):
    delt_y[i] = t
    t *= 2
y[0] = 0
for i in range(1, N):
    y[i] = y[i - 1] + delt_y[i - 1]

for _ in range(M):
    x1, y1, x2, y2 = read_int()
    x1, y1 = y1, x1
    x2, y2 = y2, x2

    if N == 1:
        if x1 <= 0 and y1 <= 0 and x2 >= 0 and y2 >= 0:
            print(1)
        else:
            print(0)
    elif N == 2:
        ans = 0
        if x1 <= 0 and y1 <= 0 and x2 >= 0 and y2 >= 0:
            ans += 1
        if y1 <= 2 and y2 >= 2:
            t1 = min(2, max(0, ((x1 - 1) + 6) // 4))
            t2 = min(2, max(0, (x2 + 6) // 4))
            ans += t2 - t1
        print(ans)
    else:
        ans = 0
        if y1 <= y[N - 1] and y2 >= y[N - 1]:
            t1 = min(2 ** (N - 2), max(0, ((x1 - 1) + 5 + 3 * (2 ** (N - 2))) // 6))
            t2 = min(2 ** (N - 2), max(0, (x2 + 5 + 3 * (2 ** (N - 2))) // 6))
            ans += t2 - t1
            t1 = min(2 ** (N - 2), max(0, ((x1 - 1) + 1 + 3 * (2 ** (N - 2))) // 6))
            t2 = min(2 ** (N - 2), max(0, (x2 + 1 + 3 * (2 ** (N - 2))) // 6))
            ans += t2 - t1
        # print(ans)
        t = 6
        for i in reversed(range(1, N - 1)):
            t1 = min(2 ** i, max(0, ((x1 - 1) + t // 2 + 2 ** (i - 1) * t) // t))
            t2 = min(2 ** i, max(0, (x2 + t // 2 + 2 ** (i - 1) * t) // t))
            if y1 <= y[i] and y2 >= y[i]:
                ans += t2 - t1
            t *= 2
        if x1 <= 0 and y1 <= 0 and x2 >= 0 and y2 >= 0:
            ans += 1
        # for i in range(0, N):
        #     print(i, y[i])
        print(ans)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值