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)