E. Inna and Large Sweet Matrixtime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputInna loves sweets very much. That's why she wants to play the "Sweet Matrix" game with Dima and Sereja. But Sereja is a large person, so the game proved small for him. Sereja suggested playing the "Large Sweet Matrix" game.
The "Large Sweet Matrix" playing field is an n × m matrix. Let's number the rows of the matrix from 1 to n, and the columns — from 1 tom. Let's denote the cell in the i-th row and j-th column as (i, j). Each cell of the matrix can contain multiple candies, initially all cells are empty. The game goes in w moves, during each move one of the two following events occurs:
- Sereja chooses five integers x1, y1, x2, y2, v (x1 ≤ x2, y1 ≤ y2) and adds v candies to each matrix cell (i, j) (x1 ≤ i ≤ x2; y1 ≤ j ≤ y2).
- Sereja chooses four integers x1, y1, x2, y2 (x1 ≤ x2, y1 ≤ y2). Then he asks Dima to calculate the total number of candies in cells(i, j) (x1 ≤ i ≤ x2; y1 ≤ j ≤ y2) and he asks Inna to calculate the total number of candies in the cells of matrix (p, q), which meet the following logical criteria: (p < x1 OR p > x2) AND (q < y1 OR q > y2). Finally, Sereja asks to write down the difference between the number Dima has calculated and the number Inna has calculated (D - I).
Unfortunately, Sereja's matrix is really huge. That's why Inna and Dima aren't coping with the calculating. Help them!
InputThe first line of the input contains three integers n, m and w (3 ≤ n, m ≤ 4·106; 1 ≤ w ≤ 105).
The next w lines describe the moves that were made in the game.
- A line that describes an event of the first type contains 6 integers: 0, x1, y1, x2, y2 and v (1 ≤ x1 ≤ x2 ≤ n; 1 ≤ y1 ≤ y2 ≤ m; 1 ≤ v ≤ 109).
- A line that describes an event of the second type contains 5 integers: 1, x1, y1, x2, y2 (2 ≤ x1 ≤ x2 ≤ n - 1; 2 ≤ y1 ≤ y2 ≤ m - 1).
It is guaranteed that the second type move occurs at least once. It is guaranteed that a single operation will not add more than 109candies.
Be careful, the constraints are very large, so please use optimal data structures. Max-tests will be in pretests.
OutputFor each second type move print a single integer on a single line — the difference between Dima and Inna's numbers.
Sample test(s)input4 5 5 0 1 1 2 3 2 0 2 2 3 3 3 0 1 5 4 5 1 1 2 3 3 4 1 3 4 3 4output2 -21
第一次用树状数组的区间更新、查询。
1.对于区间更新需要维护一个数组d[i] = a[i] - a[i-1], d[1] = a[1];
即a[i] = d[1] + d[2] + …… + d[i];
当a[l]到a[r]增加v的时候只需用树状数组维护d[l] += v, d[r+1] -= v;
2.对于区间查询则需再建一个数组c[i] = d[i]*i;
求a[1]到a[r]的和时,我们发现d[1]被加了r次,,d[2]被加了r-1次,d[3]被加了r-2次……
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<cmath> using namespace std; #define maxn 4*1000005 #define inf 0x3f3f3f3f #define maxe 1000000 #define LL long long int n,m,w,ma; LL bx[maxn],cx[maxn]; LL by[maxn],cy[maxn]; int lowbit ( int x ){ return x & (-x); } void add ( LL a[], int i, LL v ){ while( i <= ma ){ a[i] += v; i += lowbit(i ); } } LL getsum ( LL a[], int end ){ LL s = 0; while( end ){ s += a[end]; end -= lowbit(end); } return s; } int main (){ scanf("%d%d%d",&n,&m,&w); int op,x1,y1,x2,y2; LL sum = 0,v; while( w-- ){ scanf("%d",&op); if( op ){ scanf("%d%d%d%d",&x1,&y1,&x2,&y2); LL sx = getsum( bx,x2) * ( x2 + 1 ) - getsum( cx,x2 ) - getsum( bx,x1-1) * ( x1 ) + getsum( cx,x1-1 ); LL sy = getsum( by,y2) * ( y2 + 1 ) - getsum( cy,y2 ) - getsum( by,y1-1) * ( y1 ) + getsum( cy, y1-1 ); printf("%I64d\n",sx + sy - sum ); } else { scanf("%d%d%d%d%I64d",&x1,&y1, &x2, &y2, &v ); LL xx = ( x2 - x1 + 1), yy = ( y2 - y1 + 1); sum += xx * yy * v; ma = n; add( bx,x1,yy*v),add(bx, x2+1, -yy*v); add( cx,x1,yy*v*x1 ), add( cx, x2 + 1, -yy*( x2+1 ) * v); ma = m; add( by, y1, xx*v), add( by, y2+1, -xx*v ); add( cy, y1, xx*v*y1), add( cy, y2+1, -xx*( y2+1 ) * v ); } } return 0; }