zoukankan      html  css  js  c++  java
  • 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));
                }
            }
        }
    }
  • 相关阅读:
    使用GUI工具Portainer.io管控Docker容器
    Pycharm-汉化的方法
    Python-Socketserver实现FTP,文件上传、下载
    Pycharm下载安装,本人亲测100% 破解
    Python-反射机制
    Python-操作XML文件
    Python-时间戳、元组时间的格式、自定义时间格式之间的转换
    Python-String字符串操作
    Python-生成器实现简单的"生产者消费者"模型
    Python- 装饰器
  • 原文地址:https://www.cnblogs.com/8023spz/p/12274228.html
Copyright © 2011-2022 走看看