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 }
  • 相关阅读:
    简介.Net对象序列化.txt
    如何在Web页面退出前提示用户保存数据?
    如何将图片存储到数据库中
    页面回车键响应,onkeydown事件
    用C#创建Windows服务(Windows Services)
    解决“Visual Studio 要求设计器使用文件中的第一个类。移动类代码使之成为文件中的第一个类,然后尝试重新加载设计器。”方法
    动态创建htm元素并添加到document中
    如何在Asp.net的Header中添加/title/Meta tages/CSS
    无法打开项目文件,“d:\web\webapp.csproj”,此安装不支持该项目类型
    用Intelligencia.UrlRewriter实现URL重写
  • 原文地址:https://www.cnblogs.com/dongsheng/p/3089786.html
Copyright © 2011-2022 走看看