时间限制: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 ≤ x2, y1 ≤ 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
题意:维护一个n*m的矩阵,操作存在某一子矩阵加减一个值,和子矩阵求和,
思路:二维树状数组。
注意:和有可能为负值。求和getSum(c+1,d+1)-getSum(c+1,b)-getSum(a,d+1)+getSum(a,b);
板子题:
/*
*/
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <bitset>
#include <stack>
#define ll long long
#define ull unsigned long long
#define inf 0x3f3f3f3f
const int mod=1000000007;
const int N=1005;
using namespace std;
int A[N][N];int n,m;
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int y,int v)
{
for(int i=x;i<=n;i+=lowbit(i))
{
for(int j=y;j<=n;j+=lowbit(j))
A[i][j]+=v;
}
}
ll getSum(int x,int y)
{
ll sum=0;
for(int i=x;i;i-=lowbit(i))
{
for(int j=y;j;j-=lowbit(j))
sum+=A[i][j];
}
return sum;
}
int main()
{
scanf("%d%d",&n,&m);
char s[10];
for(int i=1;i<=m;i++)
{
int a,b,c,d;
scanf("%s",s);
if(s[0]=='A')
{
scanf("%d%d%d",&a,&b,&c);
add(a+1,b+1,c);
}
else
{
scanf("%d%d%d%d",&a,&b,&c,&d);
ll ans=getSum(c+1,d+1)-getSum(c+1,b)-getSum(a,d+1)+getSum(a,b);
ans=(ans+mod)%mod;
printf("%lld\n",ans);
}
}
return 0;
}