zoukankan      html  css  js  c++  java
  • HDOJ1892 See you~(二维树状数组)

    See you~

    Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
    Total Submission(s): 2508    Accepted Submission(s): 798


    Problem Description
    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. 
    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. 
    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. 
     
    Input
    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. 
    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. 
    There are 4 kind of queries, sum, add, delete and move. 
    For example: 
    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. 
    A x1 y1 n1 means I put n1 books on the position (x1,y1) 
    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. 
    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. 
    Make sure that at first, there is one book on every grid and 0<=x1,y1,x2,y2<=1000,1<=n1<=100. 
     
    Output
    At the beginning of each case, output "Case X:" where X is the index of the test case, then followed by the "S" queries. 
    For each "S" query, just print out the total number of books in that area. 
     
    Sample Input
    2 3 S 1 1 1 1 A 1 1 2 S 1 1 1 1 3 S 1 1 1 1 A 1 1 2 S 1 1 1 2
     
    Sample Output
    Case 1: 1 3 Case 2: 1 4
     
    Author
    Sempr|CrazyBird|hust07p43
     
    Source
     
    Recommend
    lcy
     
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 
     5 using namespace std;
     6 
     7 const int size = 1010;
     8 int a[size+1][size+1];
     9 
    10 int lowbit(int i)
    11 {
    12     return i & (-i);
    13 }
    14 
    15 void add(int x, int y, int num)
    16 {
    17     for(int i = x; i <= size; i += lowbit(i))
    18         for(int j = y; j <= size; j += lowbit(j))
    19             a[i][j] += num;
    20 }
    21 
    22 int getSum(int x, int y)
    23 {
    24     int tot = 0;
    25     for(int i = x; i > 0; i -= lowbit(i))
    26         for(int j = y; j > 0; j -= lowbit(j))
    27             tot += a[i][j];
    28     return tot;
    29 }
    30 
    31 void init()
    32 {
    33     memset(a, 0, sizeof(a));
    34     for(int i = 1; i < size; ++i)
    35         for(int j = 1; j < size; ++j)
    36             add(i, j, 1);
    37 }
    38 
    39 
    40 
    41 int main()
    42 {
    43     int T, n, x1, x2, y1, y2, n1;
    44     char cmd[3];
    45     scanf("%d", &T);
    46     for(int cnt = 1; cnt <= T; ++cnt)
    47     {
    48         init();
    49         scanf("%d", &n);
    50         printf("Case %d:\n", cnt);
    51         while(n--)
    52         {
    53             scanf("%s", cmd);
    54             if(cmd[0] == 'A')
    55             {
    56                 scanf("%d%d%d", &x1, &y1, &n1);
    57                 add(x1+1, y1+1, n1);
    58             }
    59             else if(cmd[0] == 'S')
    60             {
    61                 scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    62                 ++x1;
    63                 ++x2;
    64                 ++y1;
    65                 ++y2;
    66                 if(x1 > x2)
    67                     swap(x1, x2);
    68                 if(y1 > y2)
    69                     swap(y1, y2);
    70                 printf("%d\n", getSum(x2,y2) - getSum(x1-1,y2) - getSum(x2,y1-1) + getSum(x1-1,y1-1));
    71             }
    72             else if(cmd[0] == 'D')
    73             {
    74                 scanf("%d%d%d", &x1, &y1, &n1);
    75                 ++x1;
    76                 ++y1;
    77                 int tmp = getSum(x1,y1) - getSum(x1-1,y1) - getSum(x1,y1-1) + getSum(x1-1,y1-1);
    78                 if(n1 > tmp)
    79                     n1 = tmp;
    80                 add(x1, y1, -n1);
    81             }
    82             else if(cmd[0] == 'M')
    83             {
    84                 scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &n1);
    85                 ++x1;
    86                 ++x2;
    87                 ++y1;
    88                 ++y2;
    89                 int tmp = getSum(x1,y1) - getSum(x1-1,y1) - getSum(x1,y1-1) + getSum(x1-1,y1-1);
    90                 if(n1 > tmp)
    91                     n1 = tmp;
    92                 add(x1, y1, -n1);
    93                 add(x2, y2, n1);
    94             }
    95         }
    96     }
    97     return 0;
    98 }
  • 相关阅读:
    谷歌AI中国中心成立,人工智能势不可挡?
    谷歌 AI 中国中心成立,人工智能势不可挡?
    谷歌 AI 中国中心成立,人工智能势不可挡?
    谷歌 AI 中国中心成立,人工智能势不可挡?
    Python将被加入高考科目?你怎么看?
    Python将被加入高考科目?你怎么看?
    Python将被加入高考科目?你怎么看?
    2833 奇怪的梦境
    奖金
    4040 EZ系列之奖金
  • 原文地址:https://www.cnblogs.com/dongsheng/p/3089786.html
Copyright © 2011-2022 走看看