• 7-26 Population (35分)


    It is always exciting to see people settling in a new continent. As the head of the population management office, you are supposed to know, at any time, how people are distributed in this continent.

    The continent is divided into square regions, each has a center with integer coordinates (. Hence all the people coming into that region are considered to be settled at the center position. Given the positions of the corners of a rectangle region, you are supposed to count the number of people living in that region.

    Input Specification:

    Each input file contains one test case. For each case, the character I in a line signals the coming in of new groups of people. In the following lines, each line contains three integers: XY, and N, where X and Y (1 ≤ XY ≤ 20000) are the coordinates of the region's center, and N (1 ≤ N ≤ 10000) is the number of people coming in.

    The character Q in a line signals the query of population. The following lines each contains four numbers: X​min​​, X​max​​, Y​min​​, Y​max​​, where ( and ( are the integer coordinates of the lower left corner and the upper right corner of the rectangle, respectively.

    The character E signals the end of the test case.

    Output Specification:

    For each Q case, print in a line the population in the given rectangle region. That is, you are supposed to count the number of people living at all the positions ( such that X​min​​≤x≤X​max​​, and Y​min​​≤y≤Y​max​​.

    Sample Input:

    I
    8 20 1
    4 5 1
    10 11 1
    12 10 1
    18 14 1
    Q
    8 10 5 15
    8 20 10 14
    I
    7 6 1
    10 3 2
    7 2 1
    2 3 2
    10 3 1
    Q
    2 20 2 20
    E
    
     

    Sample Output:

    1
    3
    12


    其实这道题就是用二维前缀和求矩形区域的人数和,但是要么内存超限,要么运行超时,运行超时可以用二维树状数组解决,内存超限在于有些内存用不到放在那很占空间,可以用map解决。
    代码:
    #include <cstdio>
    #include <cstdlib>
    #include <map>
    using namespace std;
    map<short,short> num[20001];
    int sta;
    int lowbit(int t) {
        return t & -t;
    }
    void update(int x,int y,int z) {
        for(int i = x;i <= 20000;i += lowbit(i)) {
            for(int j = y;j <= 20000;j += lowbit(j)) {
                num[i][j] += z;
            }
        }
    }
    int getsum(int x,int y) {
        int sum = 0;
        for(int i = x;i > 0;i -= lowbit(i)) {
            for(int j = y;j > 0;j -= lowbit(j)) {
                sum += num[i][j];
            }
        }
        return sum;
    }
    int main() {
        char op[10];
        int x,y,n,x1,y1;
        while(scanf("%s",op) && op[0] != 'E') {
            if(op[0] == 'I') sta = 1;
            else if(op[0] == 'Q') sta = 2;
            else {
                x = atoi(op);
                if(sta == 1) {
                    scanf("%d%d",&y,&n);
                    update(x,y,n);
                }
                else {
                    scanf("%d%d%d",&x1,&y,&y1);
                    printf("%d
    ",getsum(x1,y1) - getsum(x1,y - 1) - getsum(x - 1,y1) + getsum(x - 1,y - 1));
                }
            }
        }
    }
  • 相关阅读:
    【LeetCode】-- 73. Set Matrix Zeroes
    (第三场) A PACM Team 【dp,五维背包】
    POJ 1745 【0/1 背包】
    (第三场) H Diff-prime Pairs 【数论-素数线性筛法+YY】
    POJ 2299 【树状数组 离散化】
    树状数组 && 线段树应用 -- 求逆序数
    HDU 1698 【线段树,区间修改 + 维护区间和】
    HDU 1166 【线段树 || 树状数组,单点修改 维护区间和】
    (第二场)D Money 【dp贪心】
    (第二场)A Run 【动态规划】
  • 原文地址:https://www.cnblogs.com/8023spz/p/12274228.html
Copyright © 2020-2023  润新知