HihoCoder - 1336 二维数状数组(单点更新 区间查询)

Posted yinbiao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HihoCoder - 1336 二维数状数组(单点更新 区间查询)相关的知识,希望对你有一定的参考价值。

You are given an N × N matrix. At the beginning every element is 0. Write a program supporting 2 operations:

 1. Add x y value: Add value to the element Axy. (Subscripts starts from 0

2. Sum x1 y1 x2 y2: Return the sum of every element Axy for x1xx2, y1yy2.

Input

The first line contains 2 integers N and M, the size of the matrix and the number of operations.

Each of the following M line contains an operation.

1 ≤ N ≤ 1000, 1 ≤ M ≤ 100000

For each Add operation: 0 ≤ x < N, 0 ≤ y < N, -1000000 ≤ value ≤ 1000000

For each Sum operation: 0 ≤ x1x2 < N, 0 ≤ y1y2 < N

Output

For each Sum operation output a non-negative number denoting the sum modulo 109+7.

 

Sample Input

5 8
Add 0 0 1
Sum 0 0 1 1
Add 1 1 1
Sum 0 0 1 1
Add 2 2 1
Add 3 3 1
Add 4 4 -1
Sum 0 0 4 4 

Sample Output

1
2
3 
题目大意:给你一个N*N二维矩阵,初始值时都为0,有两种操作
1:单点修改值
2:给出一个子矩阵的左上和右下角的坐标,询问一个子矩形范围内的值的和.
二维树状数组模板题,单点更新区间查询
#include<queue>
#include<set>
#include<cstdio>
#include <iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define max_v 1005
#define mod 1000000007
typedef long long LL;
int c[max_v][max_v];
int lowbit(int x)
{
    return x&(-x);
}
LL getsum(int x,int y)
{
    LL sum=0;
    for(int i=x;i!=0;i-=lowbit(i))
    {
        for(int j=y;j!=0;j-=lowbit(j))
        {
            sum=(sum+c[i][j]);
        }
    }
    return sum;
}
void update(int x,int y,int v)
{
    for(int i=x;i<max_v;i+=lowbit(i))
    {
        for(int j=y;j<max_v;j+=lowbit(j))
        {
            c[i][j]=(c[i][j]+v);
        }
    }
}
int main()
{
    char str[10];
    int n,m;
    while(~scanf("%d %d",&n,&m))
    {
        memset(c,0,sizeof(c));
        int a,b,w;
        while(m--)
        {
            scanf("%s",str);
            if(str[0]==A)
            {
                scanf("%d %d %d",&a,&b,&w);
                a++;
                b++;
                update(a,b,w);
            }else
            {
                int sx,sy,ex,ey;
                scanf("%d %d %d %d",&sx,&sy,&ex,&ey);
                sx++;
                sy++;
                ex++;
                ey++;
                printf("%lld
",(getsum(ex,ey)-getsum(sx-1,ey)-getsum(ex,sy-1)+getsum(sx-1,sy-1)+mod)%mod);
            }
        }
    }
    return 0;
}
/*
题目大意:给你一个N*N二维矩阵,初始值时都为0,有两种操作
1:单点修改值 
2:给出一个子矩阵的左上和右下角的坐标,询问一个子矩形范围内的值的和.

二维树状数组模板题,单点更新区间查询

*/

 

 

以上是关于HihoCoder - 1336 二维数状数组(单点更新 区间查询)的主要内容,如果未能解决你的问题,请参考以下文章

HihoCoder - 1336 Matrix Sum

数状数组求逆序对

一维数状数组区间修改,查询

HDU 1394Minimum Inversion Number 数状数组 逆序对数量和

为啥在使用单指针传递给函数时必须对二维数组进行类型转换?

哪个更好,单 for 循环或双 for 循环迭代二维数组? C++