farm 二维树桩数组数很大,数组存不下,怎么搞?区间更新

链接:https://www.nowcoder.com/acm/contest/140/J
来源:牛客网
 

farm

时间限制:C/C++ 4秒,其他语言8秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

White Rabbit has a rectangular farmland of n*m. In each of the grid there is a kind of plant. The plant in the j-th column of the i-th row belongs the a[i][j]-th type.
White Cloud wants to help White Rabbit fertilize plants, but the i-th plant can only adapt to the i-th fertilizer. If the j-th fertilizer is applied to the i-th plant (i!=j), the plant will immediately die.
Now White Cloud plans to apply fertilizers T times. In the i-th plan, White Cloud will use k[i]-th fertilizer to fertilize all the plants in a rectangle [x1[i]...x2[i]][y1[i]...y2[i]].
White rabbits wants to know how many plants would eventually die if they were to be fertilized according to the expected schedule of White Cloud.

 

 

输入描述:

The first line of input contains 3 integers n,m,T(n*m<=1000000,T<=1000000)
For the next n lines, each line contains m integers in range[1,n*m] denoting the type of plant in each grid.
For the next T lines, the i-th line contains 5 integers x1,y1,x2,y2,k(1<=x1<=x2<=n,1<=y1<=y2<=m,1<=k<=n*m)

输出描述:

Print an integer, denoting the number of plants which would die.

示例1

输入

复制

2 2 2
1 2
2 3
1 1 2 2 2
2 1 2 1 1

输出

复制

3
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn = 1e6+10;
vector<ll>mp[maxn];     //存二维矩阵
vector<ll>tre[maxn];    //存二维树状数组结果
ll has[maxn];
int n, m;
void fun() {           //预处理出1e6以内的所有数的has值(这里指随机生成的大于1e6的数)
	//srand(time(NULL));
	for (int i = 1; i < maxn - 5; i++)
		has[i] = (ll)rand()*1e6 + rand()*rand();
}
//一下三个函数使用的二维树状数组
ll lowbit(ll x) {     
	return x&(-x);
}
void update(int x, int y, ll z)
{
	for (int i = x; i <= n; i += lowbit(i))
		for (int j = y; j <= m; j += lowbit(j))
			tre[i][j] += z;
}
ll sum(int x, int y)
{
	ll ret = 0;
	for (int i = x; i >= 1; i -= lowbit(i))
		for (int j = y; j >= 1; j -= lowbit(j))
			ret += tre[i][j];
	return ret;
}
int main() {
	int t, x;
	scanf("%d%d%d", &n, &m, &t);
	fun();
	for (int i = 1; i <= n; i++) {
		mp[i].push_back(0);         //使mp的列从1开始(方便后面查询)
		tre[i].push_back(0);
		for (int j = 1; j <= m; j++) {
			scanf("%d", &x);
			mp[i].push_back(has[x]);//将给出的值进行随机化
			tre[i].push_back(0);   //初始化树状数组
		}
	}
	int x1, y1, x2, y2;
	ll w;
	while (t--) {                //给的是左上角和右下角的坐标
		scanf("%d%d%d%d%lld", &x1, &y1, &x2, &y2, &w);
		update(x1, y1, has[w]);  //通过更新这4个几个实现区间更新
		update(x2 + 1, y2 + 1, has[w]);
		update(x2 + 1, y1, -has[w]);
		update(x1, y2 + 1, -has[w]);
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) //若是最终在该点撒过得农药总和值不是原值得倍数,则表示死亡
		for (int j = 1; j <= m; j++)
			if (sum(i, j) % mp[i][j] != 0) ans++;
	printf("%d\n", ans);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值