fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. int Q;
  8. cin >> Q;
  9.  
  10. while (Q--) {
  11. int x, y;
  12. cin >> x >> y;
  13.  
  14. // Determine the level of the triangle
  15. int level = 0;
  16. while (x > 1) {
  17. x /= 2;
  18. level++;
  19. }
  20.  
  21. // Calculate the number of points in the previous level
  22. int prevLevelPoints = pow(2, level - 1) - 1;
  23.  
  24. // Determine the position of the point within the level, starting from 0
  25. int position = y - 1;
  26.  
  27. // Determine the color based on the position within the level
  28. if (level == 1) {
  29. cout << 1 << endl; // Base case: top point is always red
  30. } else if (position < prevLevelPoints) {
  31. cout << 1 << endl; // Red
  32. } else {
  33. // Check if the position is in the inverted triangle or the bottom triangle
  34. int invertedTrianglePoints = prevLevelPoints;
  35. if (position < invertedTrianglePoints + prevLevelPoints + 1) {
  36. cout << 0 << endl; // Blue
  37. } else {
  38. // Recursively check the color of the corresponding point in the previous level
  39. x /= 2;
  40. y = (position - invertedTrianglePoints - 1) / 2 + 1;
  41. level--;
  42. }
  43. }
  44. }
  45.  
  46. return 0;
  47. }
Success #stdin #stdout 0.01s 5296KB
stdin
6
1 1
5 3
8 2
8 6
5 4
6 4
stdout
0
0
1
0