HihoCoder - 1336 Matrix Sum

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 y1: 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 二维的树状数组,做法与一维类似,注意求区间时加减范围。
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5
 6 using namespace std;
 7
 8 const int maxn=1005;
 9 int g[maxn][maxn],n;
10
11 void update(int x,int y,int v)
12 {
13     int j=y;
14     for(;x<=n;x+=x&(-x))
15         for(y=j;y<=n;y+=y&(-y))
16         {
17             g[x][y]+=v;
18             g[x][y]%=1000000007;
19         }
20 }
21
22 long long getsum(int x,int y)
23 {
24     long long sum=0;
25     int j=y;
26     for(;x>0;x-=x&(-x))
27         for(y=j;y>0;y-=y&(-y))
28         {
29             sum+=g[x][y];
30             sum%=1000000007;
31         }
32     return sum;
33 }
34
35 int main()
36 {
37     int m,x1,y1,x2,y2,v;
38     while(~scanf("%d%d",&n,&m))
39     {
40         memset(g,0,sizeof(g));
41         char s[5];
42         for(int i=0;i<m;i++)
43         {
44             cin>>s;
45             if(s[0]==‘A‘)
46             {
47                 scanf("%d%d%d",&x1,&y1,&v);
48                 update(x1+1,y1+1,v);
49             }
50             else
51             {
52                 scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
53                 long long ans=0;
54                 ans+=getsum(x2+1,y2+1)%(1000000007);
55                 ans-=getsum(x1,y2+1)%(1000000007);
56                 ans-=getsum(x2+1,y1)%(1000000007);
57                 ans+=getsum(x1,y1)%(1000000007);
58                 ans%=1000000007;
59                 if(ans<0)
60                     ans+=1000000007;
61                 printf("%lld\n",ans);
62             }
63         }
64     }
65
66
67     return 0;
68 } 
时间: 2024-10-23 10:05:35

HihoCoder - 1336 Matrix Sum的相关文章

ACdream-1171 Matrix sum, 最大费用最大流

Matrix sum Time Limit: 8000/4000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description sweet和zero在玩矩阵游戏,sweet画了一个N * M的矩阵,矩阵的每个格子有一个整数.zero给出N个数Ki,和M个数Kj,zero要求sweet选出一些数,满足从第 i 行至少选出了Ki个数,第j列至少选出了K

Acdream 1171 Matrix sum 上下界费用流

题目链接:点击打开链接 Matrix sum Time Limit: 8000/4000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description sweet和zero在玩矩阵游戏,sweet画了一个N * M的矩阵,矩阵的每个格子有一个整数.zero给出N个数Ki,和M个数Kj,zero要求sweet选出一些数,满足从第 i 行至少选出了Ki

E - Evaluate Matrix Sum

Description Given a matrix, the elements of which are all integer number from 0 to 50, you are required to evaluate the square sum of its specified sub-matrix. Input The first line of the input contains a single integer T (1 <= T <= 5), the number o

[HIHO]hihoCoder太阁最新面经算法竞赛7

题目链接:http://hihocoder.com/contest/hihointerview12 期末完事了,终于有时间成套刷题了.这套题比较简单,难度上感觉和上一套差不多.除了最后一个题是看了讨论版说数据水才敢写的. A Word Construction 解法:正解应该是dfs+剪枝,我的思路是贪心,竟然过了.先把字符串按字典序排序,然后枚举每一个串作为起始,记下串内有什么字符,再从头到尾枚举所有字符串,看看每一个是否符合条件.就那么100个字符串,数据弱得很. 1 #include <a

POJ 3233 Matrix Power Series

矩阵快速幂+二分求前n项和 矩阵快速幂是有模板的,多做几道题就会理解,前提是要会快速幂取模: 之所以用二分是因为求和的过程:A^1+A^2...+A^(k-1)+A^k,   k是1e9的,所以暴力求和肯定会TLE,在网上找到 了二分求矩阵和的方法: 公式为  (1+A^(k/2))*(A+A^2+..+A^k/2)   的,所以可以写成二分递归,如果k为奇数的话,sum就加上A^k(k为当 前的k值,不再是最初的值),反正是个公式,你要不信的话可以证明一下,所以就贴代码了,感觉到姿势不够优美

POJ3233:Matrix Power Series(矩阵快速幂+二分)

http://poj.org/problem?id=3233 题目大意:给定矩阵A,求A + A^2 + A^3 + … + A^k的结果(两个矩阵相加就是对应位置分别相加).输出的数据mod m.k<=10^9.这道题两次二分,相当经典.首先我们知道,A^i可以二分求出.然后我们需要对整个题目的数据规模k进行二分.比如,当k=6时,有:A + A^2 + A^3 + A^4 + A^5 + A^6 =(A + A^2 + A^3) + A^3*(A + A^2 + A^3)应用这个式子后,规模

poj 3233 Matrix Power Series(等比矩阵求和)

http://poj.org/problem?id=3233 ps转: 用二分方法求等比数列前n项和:即 原理: (1)若n==0 (2)若n%2==0     (3)若n%2==1 代码如下: LL sum(LL p,LL n) { if(n==0) return 1; if(n&1) return (1+pow(p,(n>>1)+1))*sum(p,n>>1); else return (1+pow(p,(n>>1)+1))*sum(p,(n-1)>&

poj Matrix Power Series 矩阵幂求和

题意:给一个n*n的矩阵A,求k次幂之和 S = A + A2 + A3 + … + Ak 思路:矩阵快速幂. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef struct node { int matrix[55][55]; }Matrix; Matrix a,sa,unit; int n,m,k,i,j; Matrix add(Matrix a,M

C++题解:Matrix Power Series ——矩阵套矩阵的矩阵加速

Matrix Power Series r时间限制: 1 Sec 内存限制: 512 MB 题目描述 给定矩阵A,求矩阵S=A^1+A^2+--+A^k,输出矩阵,S矩阵中每个元都要模m. 数据范围: n (n ≤ 30) , k (k ≤ 109) ,m (m < 104) 输入 输入三个正整数n,k,m 输出 输出矩阵S mod m 样例输入 2 2 4 0 1 1 1 样例输出 1 2 2 3 这道题不多说,可以得出加速矩阵(E为单位矩阵,也就是形为\(\begin{bmatrix}1&