POJ 1195 Mobile phones

  二维树状数组

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
int n;
int c[1030][1030];
int lowbit(int k)
{
    return k&(-k);
}
void add(int x,int y,int num)
{
    for(int i = x;i <= n;i += lowbit(i))
    for(int j = y;j <= n;j += lowbit(j))
    c[i][j] += num;
}
void sub(int x,int y,int num)
{
    for(int i = x;i >= 0;i -= lowbit(i))
    for(int j = y;j >= 0;j -= lowbit(j))
    c[i][j] -= num;
}
int cal(int x,int y)
{
    int sum = 0;
    for(int i = x;i > 0;i -= lowbit(i))
    for(int j = y;j > 0;j -= lowbit(j))
     sum += c[i][j];
     return sum;
}
int main()
{
    int first,op,size;
    scanf("%d%d",&first,&n);
    {
        size = n;
        memset(c,0,sizeof(c));
        for(int i = 1;i <= size;i++)
        for(int j = 1;j <= size;j++)
        add(i,j,first);
        int op;
        while(scanf("%d",&op) != EOF)
        {
            if(op == 1)
            {
                int x,y,a;
                scanf("%d%d%d",&x,&y,&a);
                x++,y++;
                add(x,y,a);
            }
            else if(op == 2)
            {
                int x1,y1,x2,y2;
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                x1++,y1++,x2++,y2++;
                int sum = cal(x2,y2) + cal(x1-1,y1-1) - cal(x2,y1-1) - cal(x1-1,y2);
                printf("%d\n",sum);
            }
            else break;
        }

    }
}
时间: 2024-10-13 14:35:59

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

(简单) 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 题目传送:Mobile phones AC代码: /************************************************ > Auther : zzuspy > Mail : [email protected] > Created Time : 2015/8/7 15:24:00 ************************************************/ #include <map> #inclu

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

●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【 二维树状数组 】

题意:基础的二维数组,注意 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