poj 1195 Mobile phones(二维数组)

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

int c[1048][1048],n;
int lowbit[1048];

void add(int x,int y,int d)
{
    int i,j;
    for(i=x;i<=n;i+=lowbit[i])
        for(j=y;j<=n;j+=lowbit[j])
    {
        c[i][j]+=d;
    }
}

int sum(int x,int y)
{
    int ret=0,i,j;
    for(i=x;i>0;i-=lowbit[i])
        for(j=y;j>0;j-=lowbit[j])
        {
            ret+=c[i][j];
        }
    return ret;
}

int main()
{
    int cmd,i,j,x,y,d,r,l,b,t;
    for(i=1;i<=1024+5;i++)
        lowbit[i]=i&(-i);
    while(~scanf("%d",&cmd))
    {
        if(cmd==0)
        {
            scanf("%d",&n);
            memset(c,0,sizeof(c));
        }
        else if(cmd==1)
        {
            scanf("%d%d%d",&x,&y,&d);
            add(x+1,y+1,d);
        }
        else if(cmd==2)
        {
            scanf("%d%d%d%d",&l,&b,&r,&t);
            int ans=sum(r+1,t+1)+sum(l,b)-sum(r+1,b)-sum(l,t+1);
            printf("%d\n",ans);
        }
        else  if(cmd==3) return 0;
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-09-30 00:48:08

poj 1195 Mobile phones(二维数组)的相关文章

poj 1195:Mobile phones(二维树状数组,矩阵求和)

Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14489   Accepted: 6735 Description Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The

poj 1195:Mobile phones(二维线段树,矩阵求和,经典题)

Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14391   Accepted: 6685 Description Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The

POJ1195 Mobile phones 二维树状数组的应用

这题可以用线段树离散化做,用二维树状数组做了一下,不懂得可以看一下这篇文章:http://www.java3z.com/cwbwebhome/article/article1/1369.html?id=4804 题意: 给你一个s*s的正方形区域,先输入一个x,若x==0,则再输入一个s,若x==1,则输入x,y,a,表示矩阵中(x,y)这点的值加上a,若x==2,输入l,b,r,t,代表以左上角的点(l,b)右下角的点(r,t),求这一片矩形内的矩阵元素之和,若x==3则结束此次程序 就是最基

POJ 1195 Mobile phones【 二维树状数组 】

题意:基础的二维数组,注意 0 + lowbit(0)会陷入无限循环----- 之前做一道一维的一直tle,就是因为这个-------------------------- 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<ma

●POJ 1195 Mobile phones

题链: http://poj.org/problem?id=1195 题解: 二维树状数组 #include<cstdio> #include<cstring> #include<iostream> #define MAXN 1500 using namespace std; struct BIT{ int val[MAXN][MAXN],N; void Reset(int n){N=n; memset(val,0,sizeof(val));} int Lowbit(i

(简单) 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 contain

POJ 1195 Mobile phones(二维树状数组)

题目链接:POJ 1195 题意: 给出一个S*S的矩阵(行.列号从1开始),每个元素初始值为0,有两种操作:一种是第X行第Y列元素值加A:另一种是查询给定范围矩阵的所有元素之和(L<=X<=R,B<=Y<=T). 分析: 查询给定范围矩阵的所有元素之和是二维区间和,可以转换为二维前缀和求值.类比一维前缀和求法,二维区间和S(L, B, R, T) = S(1, 1, R, T) - S(1 ,1, L-1, T) - S(1, 1, R, B-1) + S(1, 1, L-1,

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 contain

POJ 1195——Mobile phones(二维树状数组)

Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 15013   Accepted: 6957 Description Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The