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
  • 相关阅读:
    .NET牛人应该知道些什么
    ASP.NET常用的分页
    获取字符串长度
    去掉网页的滚动条
    GridView动态生成字段常见问题及解决方法
    PagesSection.ValidateRequest 属性
    我刚刚做了一个艰难的决定
    Jquery UI Dialog 对话框学习
    c语言病毒分析(转)
    不用不熟悉的工具和方法
  • 原文地址:https://www.cnblogs.com/whywhy/p/4654642.html
Copyright © 2011-2022 走看看