zoukankan      html  css  js  c++  java
  • (简单) POJ 1195 Mobile phones,二维树状数组。

    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. 
     
      二维树状数组的裸题,不过要注意坐标要加一,因为从0开始。
     
    代码如下:
    // ━━━━━━神兽出没━━━━━━
    //    ┏┓       ┏┓
    //   ┏┛┻━━━━━━━┛┻┓
    //   ┃           ┃
    //   ┃     ━     ┃
    //     ████━████   ┃
    //   ┃           ┃
    //   ┃    ┻      ┃
    //   ┃           ┃
    //   ┗━┓       ┏━┛
    //     ┃       ┃
    //     ┃       ┃
    //    ┃       ┗━━━┓
    //    ┃           ┣┓
    //    ┃           ┏┛
    //    ┗┓┓┏━━━━━┳┓┏┛
    //     ┃┫┫     ┃┫┫
    //     ┗┻┛     ┗┻┛
    //
    // ━━━━━━感觉萌萌哒━━━━━━
    
    // Author        : WhyWhy
    // Created Time  : 2015年07月17日 星期五 14时44分13秒
    // File Name     : 1195.cpp
    
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    const int MaxN=1100;
    
    int C[MaxN][MaxN];
    int N;
    
    inline int lowbit(int x)
    {
        return x&(-x);
    }
    
    void add(int x,int y,int d)
    {
        int t;
    
        while(x<=N)
        {
            t=y;
    
            while(t<=N)
            {
                C[x][t]+=d;
                t+=lowbit(t);
            }
    
            x+=lowbit(x);
        }
    }
    
    int query(int x,int y)
    {
        int ret=0;
        int t;
    
        while(x>0)
        {
             t=y;
    
             while(t>0)
             {
                 ret+=C[x][t];
                 t-=lowbit(t);
             }
    
             x-=lowbit(x);
        }
    
        return ret;
    }
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
    
        int a,b,c,d,e;
    
        while(1)
        {
            scanf("%d",&a);
    
            if(a==1)
            {
                scanf("%d %d %d",&b,&c,&d);
                add(b+1,c+1,d);
            }
            else if(a==2)
            {
                scanf("%d %d %d %d",&b,&c,&d,&e);
                printf("%d
    ",query(d+1,e+1)-query(d+1,c)-query(b,e+1)+query(b,c));
            }
            else if(a==0)
            {
                scanf("%d",&N);
                memset(C,0,sizeof(C));
            }
            else
                break;
        }
        
        return 0;
    }
    View Code
  • 相关阅读:
    过滤器
    联系数据库 电话本例子
    连接数据库日志例题
    登录注册 servlet
    Pandas截取列部分字符,并据此修改另一列的数据
    Excel 如何判断某列哪些单元格包含某些字符
    Pandas逐行读取Dateframe并转为list
    Pandas: 使用str.replace() 进行文本清洗
    如何在xlwt中编写多个列的单元格?
    python:循环定义、赋值多个变量
  • 原文地址:https://www.cnblogs.com/whywhy/p/4654642.html
Copyright © 2011-2022 走看看