import sys
sys.setrecursionlimit(2**31-1)
def i2s(): return sys.stdin.readline().rstrip()
def i2ss(): return s2ss(i2s())
def i2n(): return int(i2s())
def i2nn(): return s2nn(i2s())
L1,R1,L2,R2 = i2nn()
print(max(min(R1,R2) - max(L1,L2),0))
WL or LW or DD
であるかを確かめます。itertools.combinations
を利用しています。import sys
from itertools import *
def i2s(): return sys.stdin.readline().rstrip()
def i2n(): return int(i2s())
N=i2n()
A=[i2s() for _ in range(N)]
def main():
for i,j in combinations(range(N),2):
ab = A[i][j]+A[j][i]
if ab not in ['WL', 'LW', 'DD']:
return print('incorrect')
return print('correct')
main()
collections.Counter
でを数えるだけです。Counter便利。import sys
from collections import *
def i2s(): return sys.stdin.readline().rstrip()
def i2n(): return int(i2s())
N=i2n()
def main():
cnt = Counter()
for i in range(N):
s = i2s()
if s not in cnt:
print(s)
else:
print(f'{s}({cnt[s]})')
cnt[s] += 1
return
main()
import sys
sys.setrecursionlimit(2**31-1)
def s2ss(s): return s.split()
def s2nn(s): return list(map(int, s2ss(s)))
def i2s(): return sys.stdin.readline().rstrip()
def i2ss(): return s2ss(i2s())
def i2n(): return int(i2s())
def i2nn(): return s2nn(i2s())
N,M=i2nn()
X=i2nn()
B=[0]*(N+1)
for _ in range(M):
c,y = i2nn()
B[c] = y
def main():
dp = [[0]*(N+1) for _ in range(N+2)]
for i in range(1,N+1):
dp[i+1][0] = dp[i][0]
for j in range(1,i+1):
dp[i][j] = dp[i-1][j-1] + X[i-1] + B[j]
dp[i+1][0] = max(dp[i+1][0], dp[i][j])
print(dp[-1][0])
return
main()
opr[i]
にi番目のbitに対する操作を定義し、随時更新していっています。
具体的には0 ↔ 1
と -1 ↔ -2
で切り替えることができ、例えばANDで0にセットされたあとにXORが適用されれば1になり、-1で操作なしであったbitならば-2で反転になり、逆に-2で反転のときはそれを打ち消して操作なしに戻すことができます。import sys
def s2ss(s): return s.split()
def s2nn(s): return list(map(int, s2ss(s)))
def i2s(): return sys.stdin.readline().rstrip()
def i2ss(): return s2ss(i2s())
def i2n(): return int(i2s())
def i2nn(): return s2nn(i2s())
N,C=i2nn()
def main():
p = C
opr = [-1 for _ in range(30)]
for i in range(N):
t, x = i2nn()
r = 0
for i in range(30):
if t == 1:
if not x & 1 << i:
opr[i] = 0
elif t == 2:
if x & 1 << i:
opr[i] = 1
elif t == 3:
if x & 1 << i:
opr[i] ^= 1
if mask[i] == 1:
r += 1 << i
elif opr[i] < 0:
r += (p if opr[i] == -1 else ~p) & 1 << i
print(r)
p = r
return
main()