2016微軟探星夏令營在線技術筆試-#1342 : Full Binary Tree Picture

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

題意理解:找規律,N=1,2是特殊的,以後的就有規律了:y座標最下一層是2,4,2,4的差,最後就是差6, 12, 24.。。。x左邊最下差2, 以後是3,6,12.。。。 一行一行的算,算某一行的時候,寫出來最左邊爲第一項的等差數列通項,而後用f(y2) - f(y1 - 1)來算算法

急轉彎:有點麻煩數據結構

算法:無ui

數據結構:無.net

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)


本文分享 CSDN - ruclion。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。code

相關文章
相關標籤/搜索