hdu acm 1004

  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. #define MAX 100000+200
  6. int n;
  7. __int64 c[MAX];
  8. int lowbit(int x)
  9. {0
  10. return x&(-x);
  11. }
  12. int bitcount(unsigned int n)
  13. {
  14. unsigned int c =0 ; // 计数器
  15. for (c =0; n; n >>=1) // 循环移位
  16. c += n &1 ; // 如果当前位是1,则计数器加1
  17. return c ;
  18. }
  19. __int64 sum(int x)
  20. {
  21. __int64 all = 0;
  22. for(int i=x; i>0; i-=lowbit(i))
  23. all += c[i];
  24. return all;
  25. }
  26. void modify(int x, int val)
  27. {
  28. for(int i=x; i<n+10; i+=lowbit(i))
  29. c[i] += val;
  30. }
  31. int main()
  32. {
  33. //freopen("read.txt", "r", stdin);
  34. int T;
  35. scanf("%d", &T);
  36. while(T--)
  37. {
  38. scanf("%d", &n);
  39. memset(c, 0 ,sizeof(c));
  40. __int64 tp;
  41. for(int i=1; i<=n; i++)
  42. {
  43. scanf("%I64d", &tp);
  44. modify(i, bitcount(tp));
  45. }
  46. int command;
  47. int q;
  48. scanf("%d", &q);
  49. int a, b;
  50. for(int i=1; i<=q; i++)
  51. {
  52. scanf("%d", &command);
  53. if(command == 1)
  54. {
  55. scanf("%d%d", &a, &b);
  56. printf("%d\n", sum(b)-sum(a-1));
  57. }
  58. else if(command == 2)
  59. {
  60. scanf("%d%d", &a, &b);
  61. modify(a, bitcount(b) - (sum(a)-sum(a-1)) );
  62. }
  63. else if(command == 3)
  64. {
  65. scanf("%d%d", &a, &b);
  66. }
  67. }
  68. }
  69. return 0;
  70. }





附件列表

     

    posted @ 2015-01-29 10:17  sober_reflection  阅读(157)  评论(0编辑  收藏  举报