fork download
  1. def get_color(x, y):
  2. # Base case: Step 1
  3. if x == 1:
  4. return 1 # Red point
  5.  
  6. # Recursive descent through the structure
  7. # Determine the size of the current triangle
  8. size = 1
  9. while size < x:
  10. size = size * 2 + 1 # Each size grows as 2 * previous size + 1
  11.  
  12. # Now we need to traverse back up the triangle
  13. while x > 1:
  14. # Check if we are in the blue triangle
  15. # The blue triangle's rows will have (x-1) total points and y will fall into that range
  16. if y <= (x - 1) // 2:
  17. # In left triangle
  18. x -= 1
  19. elif y > (x - 1) // 2 + (x - 1) // 2 + 1:
  20. # In right triangle
  21. x -= 1
  22. y -= (x - 1) // 2 + 1 # Adjust y to the right triangle's y-space
  23. else:
  24. # We're in the blue triangle area (blue triangle rows start from (x-1))
  25. return 0 # Blue point
  26.  
  27. return 1 # We reached back to step 1, thus it's red
  28.  
  29. def main():
  30. import sys
  31. input = sys.stdin.read
  32. data = input().strip().splitlines()
  33.  
  34. Q = int(data[0])
  35. results = []
  36.  
  37. for i in range(1, Q + 1):
  38. x, y = map(int, data[i].split())
  39. color = get_color(x, y)
  40. results.append(color)
  41.  
  42. print("\n".join(map(str, results)))
  43.  
  44. if __name__ == "__main__":
  45. main()
  46.  
Success #stdin #stdout 0.02s 9884KB
stdin
6
1 1
5 3
8 2
8 6
5 4
6 4
stdout
1
0
0
0
0
0