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
  • 相关阅读:
    springmvc与servlet初识理解2
    springMVC与servlet的初识
    SpringMVC的依赖和视图解析器配置
    【内网渗透】— 内网信息收集(4)
    机械行业设计软件学习资源整理
    整理的电学课程
    redis src 目录组织结构
    解决Joi报错TypeError: Joi.Validate is not a function问题
    浅谈Node.js开发Web服务器
    JavaScript水仙花数(传递任意n位数)
  • 原文地址:https://www.cnblogs.com/whywhy/p/4654642.html
Copyright © 2011-2022 走看看