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
  • 相关阅读:
    10.矩形覆盖 Java
    09.变态跳台阶 Java
    08.青蛙跳台阶 Java
    07.斐波那契数列 Java
    06.旋转数组的最小数字 Java
    05.用两个栈实现队列 Java
    04.重建二叉树 (Java)
    03.从尾到头打印链表 (Java)
    数据结构-有序链表的合并
    数据结构-判断链表是否存在环形链表
  • 原文地址:https://www.cnblogs.com/whywhy/p/4654642.html
Copyright © 2011-2022 走看看