fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.  
  7. long q;
  8. int s=0;
  9.  
  10. cin>>q;
  11.  
  12. while(q--)
  13. {
  14. char a[5];
  15. long x,y,p,q,r;
  16. int h1=0,h2=0,high,low;
  17. int red=0,black=0;
  18.  
  19. cin>>a;
  20.  
  21. if(a[1]=='i')
  22. {
  23. s=1-s;
  24. continue;
  25. }
  26.  
  27. cin>>x>>y;
  28. p=x;
  29. q=y;
  30.  
  31. while(x>0)
  32. {
  33. x>>=1;
  34. h1++;
  35. }
  36.  
  37. if(p==q)
  38. {
  39. if(h1%2==0)
  40. red++;
  41. else
  42. black++;
  43.  
  44. if((a[1]=='r' && s==0) || (a[1]=='b' && s==1))
  45. {
  46. cout<<red<<"\n";
  47. continue;
  48. }
  49. if((a[1]=='b' && s==0) || (a[1]=='r' && s==1))
  50. {
  51. cout<<black<<"\n";
  52. continue;
  53. }
  54. }
  55.  
  56. while(y>0)
  57. {
  58. y>>=1;
  59. h2++;
  60. }
  61.  
  62. if(h1>h2)
  63. {
  64. high=h1;
  65. low=h2;
  66. }
  67. else
  68. {
  69. high=h2;
  70. low=h1;
  71.  
  72. r=p;
  73. p=q;
  74. q=r;
  75. }
  76.  
  77. while(high!=low)
  78. {
  79. p=p/2;
  80.  
  81. if(high%2==0)
  82. red++;
  83. else
  84. black++;
  85.  
  86. high--;
  87. }
  88.  
  89. while(p!=q)
  90. {
  91. p=p/2;
  92. q=q/2;
  93.  
  94. if(high%2==0)
  95. red+=2;
  96. else
  97. black+=2;
  98.  
  99. high--;
  100. }
  101.  
  102. if(high%2==0)
  103. red++;
  104. else
  105. black++;
  106.  
  107.  
  108. if((a[1]=='r' && s==0) || (a[1]=='b' && s==1))
  109. {
  110. cout<<red<<"\n";
  111. continue;
  112. }
  113. if((a[1]=='b' && s==0) || (a[1]=='r' && s==1))
  114. {
  115. cout<<black<<"\n";
  116. continue;
  117. }
  118. }
  119. return 0;
  120. }
  121.  
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