• hdu 1892 二维树状数组


    1. #include <stdio.h>
    2. #include <string.h>
    3. #include <iostream>
    4. using namespace std;
    5. #define MAX 1000+10
    6. #define MAXN 1002
    7. int c[MAX][MAX], list[MAX][MAX];
    8. int lowbit(int x)
    9. {
    10. return x&(-x);
    11. }
    12. void modify(int x, int y, int num)
    13. {
    14. for(int i=x; i<=MAXN; i+=lowbit(i) )
    15. for(int j=y; j<=MAXN; j+=lowbit(j) )
    16. c[i][j] += num;
    17. }
    18. int sum(int x, int y)
    19. {
    20. int all = 0;
    21. for(int i=x; i>0; i-=lowbit(i) )
    22. for(int j=y; j>0; j-=lowbit(j) )
    23. all += c[i][j];
    24. return all;
    25. }
    26. void mswap(int &a, int &b)
    27. {
    28. if(a < b)
    29. {
    30. int tp = a; a = b; b= tp;
    31. }
    32. }
    33. int main()
    34. {
    35. //freopen("read.txt", "r", stdin);
    36. int T;
    37. scanf("%d", &T);
    38. int co = 1;
    39. while(T--)
    40. {
    41. printf("Case %d: ", co++);
    42. memset(c, 0, sizeof(c)); //初始化啊!!!!
    43. for(int i=1; i<=MAXN; i++)
    44. for(int j=1; j<=MAXN; j++)
    45. {
    46. list[i][j] = 1;
    47. modify(i, j, 1);
    48. }
    49. int n;
    50. scanf("%d", &n);
    51. while(n--)
    52. {
    53. int x1, y1;
    54. char str;
    55. cin>>str>>x1>>y1;
    56. x1++; y1++; // 题目的坐标是从0开始的,树状数组要求从1开始,
    57. if(str == 'S')
    58. {
    59. int x2, y2;
    60. scanf("%d%d", &x2, &y2);
    61. x2++, y2++;
    62. mswap(x2, x1); mswap(y2, y1); // 果然,(x2,y2) 可能比 (x1,y1) 要小……
    63. int tp = sum(x2, y2) + sum(x1-1, y1-1) - sum(x1-1, y2) - sum(x2, y1-1);
    64. printf("%d ", tp);
    65. }
    66. else if(str == 'A')
    67. {
    68. int tp; scanf("%d", &tp);
    69. modify(x1, y1, tp);
    70. list[x1][y1] += tp;//
    71. }
    72. else if(str == 'D')
    73. {
    74. int tp; scanf("%d", &tp);
    75. if(list[x1][y1] < tp) tp = list[x1][y1];
    76. modify(x1, y1, -tp);
    77. list[x1][y1] -= tp;
    78. }
    79. else if(str == 'M')
    80. {
    81. int x2, y2, tp;
    82. scanf("%d %d %d", &x2, &y2, &tp);
    83. x2++; y2++;
    84. if(list[x1][y1] < tp) tp = list[x1][y1];
    85. list[x1][y1] -= tp;
    86. list[x2][y2] += tp;
    87. modify(x1, y1, -tp);
    88. modify(x2, y2, tp);
    89. }
    90. }
    91. }
    92. return 0;
    93. }
    94. Problem Description
    95. Now I am leaving hust acm. In the past two and half years, I learned so many knowledge about Algorithm and Programming, and I met so many good friends. I want to say sorry to Mr, Yin, I must leave now ~~>.<~~. I am very sorry, we could not advanced to the World Finals last year.
    96. When coming into our training room, a lot of books are in my eyes. And every time the books are moving from one place to another one. Now give you the position of the books at the early of the day. And the moving information of the books the day, your work is to tell me how many books are stayed in some rectangles.
    97. To make the problem easier, we divide the room into different grids and a book can only stayed in one grid. The length and the width of the room are less than 1000. I can move one book from one position to another position, take away one book from a position or bring in one book and put it on one position.
    98. Input
    99. In the first line of the input file there is an Integer T(1<=T<=10), which means the number of test cases in the input file. Then N test cases are followed.
    100. For each test case, in the first line there is an Integer Q(1<Q<=100,000), means the queries of the case. Then followed by Q queries.
    101. There are 4 kind of queries, sum, add, delete and move.
    102. For example:
    103. S x1 y1 x2 y2 means you should tell me the total books of the rectangle used (x1,y1)-(x2,y2) as the diagonal, including the two points.
    104. A x1 y1 n1 means I put n1 books on the position (x1,y1)
    105. D x1 y1 n1 means I move away n1 books on the position (x1,y1), if less than n1 books at that position, move away all of them.
    106. M x1 y1 x2 y2 n1 means you move n1 books from (x1,y1) to (x2,y2), if less than n1 books at that position, move away all of them.
    107. Make sure that at first, there is one book on every grid and 0<=x1,y1,x2,y2<=1000,1<=n1<=100.
    108. Output
    109. At the beginning of each case, output "Case X:" where X is the index of the test case, then followed by the "S" queries.
    110. For each "S" query, just print out the total number of books in that area.
    111. Sample Input
    112. 2
    113. 3
    114. S 1 1 1 1
    115. A 1 1 2
    116. S 1 1 1 1
    117. 3
    118. S 1 1 1 1
    119. A 1 1 2
    120. S 1 1 1 2
    121. Sample Output
    122. Case 1:
    123. 1
    124. 3
    125. Case 2:
    126. 1
    127. 4
    128. */





    附件列表

    • 相关阅读:
      Windows Azure: Service Bus Queues 入门
      HTTP权威指南读书
      学习OpenWebkitSharp
      Intro to ASP.NET MVC 4 with Visual Studio [译五:添加一个模型]
      正则表达式
      运行page页面时的事件执行顺序
      C#cookie自动获取工具发布
      (译) Facebook工程师2012年小创意合集
      三层+临时表存储过程+巧用json+贱招图片加载+无刷新分页
      iOS开发那些事编写OCUnit测试方法应用测试方法
    • 原文地址:https://www.cnblogs.com/sober-reflection/p/4db382b19f9b49a523fec787f8795736.html
    Copyright © 2020-2023  润新知