Mobile phones_二维树状数组

【题意】给你一个矩阵(初始化为0)和一些操作,1 x y a表示在arr[x][y]加上a,2 l b r t 表示求左上角为(l,b),右下角为(r,t)的矩阵的和。

【思路】帮助更好理解树状数组。

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=1050;
int c[N][N];
int s;
int lowbit(int x)
{
    return x&(-x);
}
void update(int x,int y,int a)
{
    for(int i=x;i<=s;i+=lowbit(i))
    {
        for(int j=y;j<=s;j+=lowbit(j))
        {
            c[i][j]+=a;
        }
    }
}
int get_sum(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 ins;
    int x,y,a;
    int l,b,r,t;
    while(scanf("%d",&ins))
    {
        if(ins==0)
        {
            scanf("%d",&s);
            memset(c,0,sizeof(c));
        }
        else if(ins==1)
        {
            scanf("%d%d%d",&x,&y,&a);
            update(x+1,y+1,a);
        }
        else if(ins==2)
        {
            scanf("%d%d%d%d",&l,&b,&r,&t);
            l++,b++,t++,r++;
            printf("%d\n",get_sum(r,t)+get_sum(l-1,b-1)-get_sum(r,b-1)-get_sum(l-1,t));
        }
        else break;
    }
    return 0;
}
时间: 2024-10-11 16:00:43

Mobile phones_二维树状数组的相关文章

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则结束此次程序 就是最基

POJ1195: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

【poj1195】Mobile phones(二维树状数组)

题目链接:http://poj.org/problem?id=1195 [题意] 给出一个全0的矩阵,然后一些操作 0 S:初始化矩阵,维数是S*S,值全为0,这个操作只有最开始出现一次 1 X Y A:对于矩阵的X,Y坐标增加A 2 L B R T:询问(L,B)到(R,T)区间内值的总和 3:结束对这个矩阵的操作 [思路] 二维树状数组单点更新+区域查询,可作为模板题. 注意坐标是从0开始,所以要+1 [代码] 1 #include<cstdio> 2 #include<cstrin

POJ1195 Mobile phones 【二维树状数组】

Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14288   Accepted: 6642 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

【POJ1195】【二维树状数组】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: 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: 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,二维树状数组。

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