fork download
  1. def get_color(x, y):
  2. while x > 1:
  3. # Calculate the size of the triangle at step x
  4. size = (1 << (x - 1)) - 1 # size = 2^(x-1) - 1
  5.  
  6. # Check if (x, y) is in the inverted triangle
  7. if y > size:
  8. return 0 # Blue
  9.  
  10. # Determine if we are in the left or right triangle
  11. if y == size:
  12. return 1 # Red (the point at the tip of the inverted triangle)
  13.  
  14. # Move to the previous triangle
  15. x -= 1
  16.  
  17. # Adjust y for the next layer
  18. if y > size // 2:
  19. y -= (size // 2 + 1) # Move to the right triangle
  20. # If y <= size // 2, we stay in the left triangle
  21.  
  22. return 1 # If we reach step 1, it is always red
  23.  
  24. import sys
  25.  
  26. # Read input
  27. input = sys.stdin.read
  28. data = list(map(int, input().split()))
  29. Q = data[0]
  30. results = []
  31.  
  32. # Process each query
  33. for i in range(1, 2 * Q, 2):
  34. x = data[i]
  35. y = data[i + 1]
  36. results.append(get_color(x, y))
  37.  
  38. # Print results
  39. print(" ".join(map(str, results)))
Success #stdin #stdout 0.04s 9784KB
stdin
6
1 1
5 3
8 2
8 6
5 4
6 4
stdout
1 1 1 1 1 1