HihoCoder-1336 two-dimensional array of numbers (single-point update interval query)

Source: Internet
Author: User

You is given an N x N matrix. The beginning every element is 0. Write a program supporting 2 operations:

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

2. Sum x1 y1 x2 y2 : Return The sum of every element Axy x1 for ≤ x ≤ x2 , y1 ≤ y ≤ .

Input

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

Each of the following line M 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

Output

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

Sample Input

Sample Output


give you a n*n two-dimensional matrix, the initial value is 0, there are two kinds of operations
1: Single point modified value
2: Gives the coordinates of the upper-left and lower-right corners of a sub-matrix, asking for the and of the values within a sub-rectangular range.Two-dimensional tree array template problem, single-point update interval query
#include <queue>#include<Set>#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<cmath>using namespacestd;#defineMax_v 1005#defineMoD 1000000007typedefLong LongLL;intC[max_v][max_v];intLowbit (intx) {    returnx& (-x);} LL Getsum (intXinty) {LL sum=0;  for(inti=x;i!=0; i-=lowbit (i)) {         for(intj=y;j!=0; j-=Lowbit (j)) {Sum= (sum+C[i][j]); }    }    returnsum;}voidUpdateintXintYintv) {     for(inti=x;i<max_v;i+=lowbit (i)) {         for(intj=y;j<max_v;j+=Lowbit (j)) {C[i][j]= (c[i][j]+v); }    }}intMain () {Charstr[Ten]; intn,m;  while(~SCANF ("%d%d",&n,&m) {memset (c,0,sizeof(c)); inta,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            {                intSx,sy,ex,ey; scanf ("%d %d%d%d",&sx,&sy,&ex,&ey); SX++; Sy++; Ex++; EY++; printf ("%lld\n", (Getsum (Ex,ey)-getsum (sx-1, EY)-getsum (ex,sy-1) +getsum (sx-1, sy-1) +mod)%MoD); }        }    }    return 0;}/*The main topic: give you a n*n two-dimensional matrix, the initial value is 0, there are two operations 1: a single point to modify the value 2: Give a sub-matrix of the upper-left and lower-right corner of the coordinates, ask a sub-rectangular range of values and. Two-dimensional tree array template problem, single-point update interval query*/

HihoCoder-1336 two-dimensional array of numbers (single-point update interval query)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.