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
  • 相关阅读:
    使用片段嵌入进行文档搜索
    详解支持向量机
    使用NLP检测和对抗AI生成的假新闻
    Detectron2 API 之 config | 十五
    用Python可视化卷积神经网络
    六种用于文本分类的开源预训练模型
    解空间树(回溯算法,分支界限法)
    日记2
    C编程(C语言程序设计,大连理工大学MOOC)
    编程题(C/C++程序设计,同济大学mooc)
  • 原文地址:https://www.cnblogs.com/whywhy/p/4654642.html
Copyright © 2011-2022 走看看