Codeforces Round #229 (Div. 2) E

 
 
 
   
   
E. Inna and Large Sweet Matrix
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

Inna 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:

  1. 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 ≤ x2y1 ≤ j ≤ y2).
  2. 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 ≤ x2y1 ≤ 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!

Input

The first line of the input contains three integers nm 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: 0x1y1x2y2 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: 1x1y1x2y2 (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.

Output

For each second type move print a single integer on a single line — the difference between Dima and Inna's numbers.

Sample test(s)
input
4 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 4
output
2
-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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值