fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. long long int row(long long int node)
  5. {
  6. long long int i=1,r=0;
  7. while(i<=node)
  8. {
  9. i*=2;
  10. r++;
  11. }
  12. return r;
  13. }
  14. int main()
  15. {
  16. int q;
  17. long long int a,b,temp,row_a,row_b,c_a,c_b,total_nodes,N;
  18. char in[5];
  19. scanf("%d\n",&q);
  20. int inode=0; //initial node = 0(black), 1(white)
  21. while(q--)
  22. {
  23. scanf("%s",in);
  24. if(in[1]=='i')
  25. {
  26. if(inode)
  27. inode=0;
  28. else
  29. inode=1;
  30. }
  31. else
  32. {
  33. scanf("%lld %lld",&a,&b);
  34. if(a<b)
  35. {
  36. temp=a;
  37. a=b;
  38. b=temp;
  39. }
  40. row_a=row(a)%2; //if 1, then same color as i_node; if 0, then opposite color of i_node;
  41. row_b=row(b)%2;
  42. c_a=c_b=0;
  43. while(row(a)>row(b))
  44. {
  45. a=a/2;
  46. c_a++;
  47. }
  48. while(a!=b)
  49. {
  50. a=a/2;
  51. c_a++;
  52. b=b/2;
  53. c_b++;
  54. }
  55. total_nodes=c_a+c_b+1;
  56. /* row_a=row(a)%2; //if 1, then same color as i_node; if 0, then opposite color of i_node;
  57.   row_b=row(b)%2;*/
  58. if(in[1]=='b')
  59. {
  60. if(!inode)//black
  61. {
  62. if(row_a)//black
  63. {
  64. N=(total_nodes/2)+(total_nodes%2);
  65. }
  66. else //white
  67. {
  68. N=(total_nodes/2);
  69. }
  70. }
  71. else//i_node is white
  72. {
  73. if(row_a)//white
  74. {
  75. N=(total_nodes/2);
  76. }
  77. else //black
  78. {
  79. N=(total_nodes/2)+(total_nodes%2);
  80. }
  81. }
  82. }
  83. else if(in[1]='r')
  84. {
  85. if(!inode)//black
  86. {
  87. if(row_a)//black
  88. {
  89. N=(total_nodes/2);
  90. }
  91. else //white
  92. {
  93. N=(total_nodes/2)+(total_nodes%2);
  94. }
  95. }
  96. else//i_node is white
  97. {
  98. if(row_a)//white
  99. {
  100. N=(total_nodes/2)+(total_nodes%2);
  101. }
  102. else //black
  103. {
  104. N=(total_nodes/2);
  105. }
  106. }
  107. }
  108. printf("%lld\n\n",N);
  109. }
  110.  
  111. }
  112. return 0;
  113. }
  114.  
Success #stdin #stdout 0s 3300KB
stdin
5
Qb 4 5
Qr 4 5
Qi
Qb 4 5
Qr 4 5
stdout
2

1

1

2