链接: 新一与基德的身高大战
def solve():
n, = RI()
a = sorted(RILST(), key=lambda x: x & 1)
b = sorted(RILST(), key=lambda x: x & 1)
print(sum((x + y) // 2 for x, y in zip(a, b)))
链接: 肖恩的投球游戏加强版
class BinTree2DIUPQ:
"""二维树状数组"""
def __init__(self, m, n):
self.n = n
self.m = m
self.tree = [[0] * (n + 1) for _ in range(m + 1)]
def lowbit(self, x):
return x & (-x)
def _update_point(self, x, y, val):
m, n, tree = self.m, self.n, self.tree
while x <= m:
y1 = y
while y1 <= n:
tree[x][y1] += val
y1 += y1 & -y1
x += x & -x
def _sum_prefix(self, x, y):
res = 0
tree = self.tree
while x > 0:
y1 = y
while y1 > 0:
res += tree[x][y1]
y1 &= y1 - 1
x &= x - 1
return res
def add_interval(self, x1, y1, x2, y2, v):
self._update_point(x1, y1, v)
self._update_point(x2 + 1, y1, -v)
self._update_point(x1, y2 + 1, -v)
self._update_point(x2 + 1, y2 + 1, v)
def query_point(self, x, y):
return self._sum_prefix(x, y)
# ms
def solve():
n, m, q = RI()
tree = BinTree2DIUPQ(n, m)
for i in range(1, n + 1):
row = RILST()
for j, v in enumerate(row, start=1):
tree.add_interval(i, j, i, j, v)
for _ in range(q):
x1, y1, x2, y2, c = RI()
tree.add_interval(x1, y1, x2, y2, c)
for i in range(1, n + 1):
ans = []
for j in range(1, m + 1):
ans.append(tree.query_point(i, j))
print(*ans)
链接: 体育健将
def lower_bound(lo: int, hi: int, key):
"""由于3.10才能用key参数,因此自己实现一个。
:param lo: 二分的左边界(闭区间)
:param hi: 二分的右边界(闭区间)
:param key: key(mid)判断当前枚举的mid是否应该划分到右半部分。
:return: 右半部分第一个位置。若不存在True则返回hi+1。
虽然实现是开区间写法,但为了思考简单,接口以[左闭,右闭]方式放出。
"""
lo -= 1 # 开区间(lo,hi)
hi += 1
while lo + 1 < hi: # 区间不为空
mid = (lo + hi) >> 1 # py不担心溢出,实测py自己不会优化除2,手动写右移
if key(mid): # is_right则右边界向里移动,目标区间剩余(lo,mid)
hi = mid
else: # is_left则左边界向里移动,剩余(mid,hi)
lo = mid
return hi
class BinIndexTree:
""" PURQ的最经典树状数组,每个基础操作的复杂度都是logn;如果需要查询每个位置的元素,可以打开self.a """
def __init__(self, size_or_nums): # 树状数组,下标需要从1开始
# 如果size 是数字,那就设置size和空数据;如果size是数组,那就是a
if isinstance(size_or_nums, int):
self.size = size_or_nums
self.c = [0 for _ in range(self.size + 5)]
# self.a = [0 for _ in range(self.size + 5)]
else:
self.size = len(size_or_nums)
# self.a = [0 for _ in range(self.size + 5)]
self.c = [0 for _ in range(self.size + 5)]
for i, v in enumerate(size_or_nums):
self.add_point(i + 1, v)
def add_point(self, i, v): # 单点增加,下标从1开始
# self.a[i] += v
while i <= self.size:
self.c[i] += v
i += i & -i
def sum_prefix(self, i): # 前缀求和,下标从1开始
s = 0
while i >= 1:
s += self.c[i]
# i -= i&-i
i &= i - 1
return s
def lowbit(self, x):
return x & -x
# ms
def solve():
n, k = RI()
a = RILST()
b = RILST()
ans = 0
s = BinIndexTree(n)
ab = sorted(zip(a, b), key=lambda x: x[0] + x[1])
for i, (x, y) in enumerate(ab, start=1):
s.add_point(i, x + y)
for i, (x, y) in enumerate(ab, start=1):
s.add_point(i, -(x + y))
p = lower_bound(1, n, lambda y:s.sum_prefix(y) > k-x) - 1
ans = max(ans, 1 + p - int(p >= i))
s.add_point(i, x + y)
print(ans)
链接: 小桥的奇异旋律
def solve():
n, = RI()
a = RILST()
p = list(accumulate(a))
ans = 0
def get(z):
d =ans= 0
for i,v in enumerate(p):
v += d
if i %2==z: # 需求正数
if v <= 0:
x = 1 - v
ans += x
d += x
else: # 需求负数
if v >= 0:
x = v+1
ans += x
d -= x
return ans
print(min(get(1),get(0)))
链接: 区间or划分
赛中看错题了,以为是异或,其实是或,那就好想一点。
def solve():
n, = RI()
a = RILST()
m = reduce(ior,a)
p = [0]+list(accumulate(a,ior))
s = 0
ans = 0
for i in range(n-1,-1,-1):
s |= a[i]
if s & p[i] == 0:
ans += 1
print(m,ans)