fork(3) download
  1. #include <stdio.h>
  2.  
  3. long int n=0,temp,number_of_coins,number_of_inputs,bit_mask;
  4. long int number_of_ones(long int i) //Return the number of bits set
  5. {
  6. return __builtin_popcountl(i);
  7. }
  8. int main(void)
  9. {
  10. long int ctr,lower,upper,length;
  11. int op;
  12.  
  13. scanf("%ld %ld",&number_of_coins,&number_of_inputs);
  14. length = number_of_coins-1;
  15. for(ctr = 0 ; ctr < number_of_inputs;ctr++) //Main loop
  16. {
  17. scanf("%d %ld %ld",&op,&lower,&upper);
  18. bit_mask = ((1 << length-lower+1)-1) & ~((1 << length-upper)-1);
  19.  
  20. if(op == 0)
  21. {
  22.  
  23. n ^= bit_mask ; //Toggle the bits in the range lower to upper
  24.  
  25. }
  26. else
  27. {
  28. temp = n;
  29. temp &= bit_mask;
  30. printf("%ld\n",number_of_ones(temp)); //Print number of bits set
  31. }
  32.  
  33. }
  34. return 0;
  35. }
Success #stdin #stdout 0.01s 2728KB
stdin
1000 5
1 0 999
0 999 999
1 0 999
0 0 999
1 0 999
stdout
0
1
7