zoukankan      html  css  js  c++  java
  • POJ 1195 Mobile phones【二维树状数组】

    题目链接:POJ 1195 Mobile phones

    Mobile phones

    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 21926   Accepted: 10200

    Description

    Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The squares form an S * S matrix with the rows and columns numbered from 0 to S-1. Each square contains a base station. The number of active mobile phones inside a square can change because a phone is moved from a square to another or a phone is switched on or off. At times, each base station reports the change in the number of active phones to the main base station along with the row and the column of the matrix. 

    Write a program, which receives these reports and answers queries about the current total number of active mobile phones in any rectangle-shaped area. 

    Input

    The input is read from standard input as integers and the answers to the queries are written to standard output as integers. The input is encoded as follows. Each input comes on a separate line, and consists of one instruction integer and a number of parameter integers according to the following table. 

    The values will always be in range, so there is no need to check them. In particular, if A is negative, it can be assumed that it will not reduce the square value below zero. The indexing starts at 0, e.g. for a table of size 4 * 4, we have 0 <= X <= 3 and 0 <= Y <= 3. 

    Table size: 1 * 1 <= S * S <= 1024 * 1024 
    Cell value V at any time: 0 <= V <= 32767 
    Update amount: -32768 <= A <= 32767 
    No of instructions in input: 3 <= U <= 60002 
    Maximum number of phones in the whole table: M= 2^30 

    Output

    Your program should not answer anything to lines with an instruction other than 2. If the instruction is 2, then your program is expected to answer the query by writing the answer as a single line containing a single integer to standard output.

    Sample Input

    0 4
    1 1 2 3
    2 0 0 2 2 
    1 1 1 2
    1 1 2 -1
    2 1 1 2 3 
    3
    

    Sample Output

    3
    4

    Source

    题目大意:

    有四种操作:

    0 -- N 初始化一个 N*N 的矩阵;

    1 -- X -- Y -- Value  在矩阵的点(X,Y)加上 Value;

    2 -- X_1-- Y_1 -- X_2 -- Y_2   查询左上角坐标为(X_1, Y_1) && 右下角坐标为(X_2, Y_2) 子矩阵的和

    3 结束程序

    大概思路:

    使用二维树状数组模拟,需要注意的是

    ①题目下标从0开始,但是树状数组下标从1开始

    ②因为维护的是前缀和,所以求子矩阵的和等于删掉上边的和左边的再加上重复删掉的部分

    AC code(4556K 516MS):

     1 #include <map>
     2 #include <set>
     3 #include <list>
     4 #include <stack>
     5 #include <queue>
     6 #include <vector>
     7 #include <math.h>
     8 #include <bitset>
     9 #include <stdio.h>
    10 #include <cstring>
    11 #include <climits>
    12 #include <iostream>
    13 #include <algorithm>
    14 #define INF 0x3f3f3f3f
    15 #define ll long long int
    16 using namespace std;
    17 
    18 const int MAXN = 1100;
    19 int c[MAXN][MAXN];
    20 int N;
    21 
    22 int lowbit(int x)
    23 {
    24     return x&(-x);
    25 }
    26 
    27 void add(int x, int y, int value)
    28 {
    29     for(int i = x; i <= N; i+=lowbit(i))
    30         for(int j = y; j <= N; j+=lowbit(j))
    31             c[i][j]+=value;
    32 }
    33 
    34 int sum(int x, int y)
    35 {
    36     int res = 0;
    37     for(int i = x; i > 0; i-=lowbit(i))
    38         for(int j = y; j > 0; j-=lowbit(j))
    39         res+=c[i][j];
    40     return res;
    41 }
    42 
    43 void MEM()
    44 {
    45     for(int i = 0; i <= N; i++)
    46         for(int j = 0; j <= N; j++)
    47             c[i][j] = 0;
    48 }
    49 int main()
    50 {
    51     ll ans;
    52     int com;
    53     int xi, yi, v, x1, x2, y1, y2;
    54     while(~scanf("%d", &com))
    55     {
    56         if(com == 3) break;
    57         else if(com == 0)
    58         {
    59             scanf("%d", &N);
    60             MEM();
    61         }
    62         else if(com == 1)
    63         {
    64             scanf("%d%d%d", &xi, &yi, &v);
    65             add(xi+1, yi+1, v);
    66         }
    67         else if(com == 2)
    68         {
    69             scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    70             x1++, y1++, x2++, y2++;
    71             ans = (sum(x2, y2) + sum(x1-1, y1-1) - sum(x2, y1-1) - sum(x1-1, y2));
    72             printf("%lld
    ", ans);
    73         }
    74     }
    75     return 0;
    76 }
  • 相关阅读:
    想起来好久没更新博客了
    操作系统文件管理
    PreparedStatement是如何大幅度提高性能的
    Java中快速排序的实现
    详解HashMap的内部工作原理
    关于Java集合的总结
    浅谈JVM内存区域划分
    解决java压缩图片透明背景变黑色的问题
    Vmware15.5中centos7minimal版 窗口字体太小
    字符长度还是字节长度
  • 原文地址:https://www.cnblogs.com/ymzjj/p/9393736.html
Copyright © 2011-2022 走看看