树状数组+差分
作者:
成理第一深情
,
2024-05-07 22:27:34
,
所有人可见
,
阅读 13
AcWing 1268
"""
思路:区间修改+单点查询,想到树状数组+差分
时间复杂度mlog(n)
如何判断最后输出是1还是0就看结果是奇数还是偶数,奇数输出1,偶数输出0
"""
import sys
input = sys.stdin.readline
print = sys.stdout.write
def lowbit(x):
return x & -x
def change(x, c):
while x <= n:
tr[x] += c
x += lowbit(x)
def query(x):
t = 0
while x:
t += tr[x]
x -= lowbit(x)
return t
n, m = map(int, input().strip().split())
# 初始化树状数组
tr = [0 for _ in range(n + 1)]
while m:
m -= 1
opt = list(map(int, input().strip().split()))
# 给[l, r]区间内所有数加上1
t = opt[0]
if t == 1:
l, r = opt[1], opt[2]
change(l, 1)
change(r + 1, -1)
elif t == 2:
x = opt[1]
if query(x) % 2 == 0:
print('0\n')
else:
print('1\n')