Live2d Test Env

HihoCoder1336 Matrix Sum(二维树状数组求和)

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

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 x1 ≤ x ≤ x2y1 ≤ y ≤ y2.

输入

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 ≤ x1 ≤ x2 < N, 0 ≤ y1 ≤ y2 < N 

输出

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

样例输入
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 
样例输出
1
2
3 

 

 

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int n,c[1100][1100];
const int Mod=1e9+7;
int lowbit(int x)
{
    return x&(-x);
}
void add(int x,int y,int val)
{
    for(int i=x;i<=n;i+=lowbit(i))
     for(int j=y;j<=n;j+=lowbit(j))
      c[i][j]=(c[i][j]+val)%Mod;    
}
int getsum(int x, int y) {
    int res = 0;

    for (int i = x; i; i -= lowbit(i)) {
        for (int j = y; j; j -= lowbit(j)) {
            res += c[i][j];
        }
    }

    return res;
}
int main()
{
    int m,i,a,b,x,y,z;
    char c[10];
    scanf("%d%d",&n,&m);n++;
    for(i=1;i<=m;i++){
        scanf("%s",c);
        if(c[0]=='A'){
            scanf("%d%d%d",&x,&y,&z);
            add(x+1,y+1,z);
        }
        else {
            scanf("%d%d%d%d",&x,&y,&a,&b);
            printf("%d\n",((getsum(a+1,b+1)+getsum(x,y)-getsum(a+1,y)-getsum(x,b+1))%Mod+Mod)%Mod);
        }
    }
    return 0;
}

 

posted @ 2017-12-04 08:02  nimphy  阅读(252)  评论(0编辑  收藏  举报