Skip to content

Commit d6eeff7

Browse files
Add files via upload
1 parent 69eb9cb commit d6eeff7

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
n, k, q = map(int, input().split())
2+
3+
arr = [list(map(int, input().split())) for _ in range(n)]
4+
5+
for r in range(k):
6+
cur = 0
7+
for c in range(n):
8+
cur |= arr[c][r]
9+
arr[c][r] = cur
10+
11+
for _ in range(q):
12+
m = int(input())
13+
mini = 0
14+
maxi = n-1
15+
16+
for _ in range(m):
17+
region, o, val = input().split()
18+
region = int(region) - 1
19+
val = int(val)
20+
21+
if o == '>':
22+
# find smallest row that has column region with value greater than val
23+
lo = mini
24+
hi = n
25+
26+
while lo < hi:
27+
mid = (lo+hi)//2
28+
if arr[mid][region] > val:
29+
hi = mid
30+
else:
31+
lo = mid+1
32+
mini = lo
33+
34+
else:
35+
# find largest row that has column region with value less than val
36+
lo = -1
37+
hi = maxi
38+
while lo < hi:
39+
mid = (lo+hi+1)//2
40+
if arr[mid][region] < val:
41+
lo = mid
42+
else:
43+
hi = mid-1
44+
maxi = lo
45+
46+
if mini <= maxi:
47+
print(mini + 1)
48+
else:
49+
print(-1)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
t = int(input())
2+
from math import log2
3+
4+
for _ in range(t):
5+
l, r, i, k = map(int, input().split())
6+
7+
def prefix(x): # XOR of 1..x
8+
ans = 0
9+
for j in range(int(log2(x + 2)) + 1):
10+
if j == 0:
11+
if x % 4 == 1 or x % 4 == 2:
12+
ans = 1
13+
else:
14+
ans = 0
15+
continue
16+
17+
if (x >> j) % 2 == 1:
18+
# one block
19+
numOnes = x % (1 << j) + 1
20+
if numOnes % 2 == 1:
21+
ans |= 1 << j
22+
return ans
23+
24+
def xorUpto(x): # XOR of all numbers up to x that are k mod 2^i
25+
y = x // (1 << i)
26+
if x % (1 << i) >= k:
27+
y += 1
28+
29+
return (prefix(y - 1) << i) + (k if y % 2 == 1 else 0)
30+
31+
print(prefix(r) ^ prefix(l - 1) ^ xorUpto(r) ^ xorUpto(l - 1))

0 commit comments

Comments
 (0)