2018牛客多校训练---farm(二维树状数组)

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

题目描述

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

 给出一个n*m的田地矩阵,每个格子上种着一种植物。给格子施肥t次,每一次给出五个数字,x1,y1,x2,y2,k,要施肥的区域坐标和要施的肥料种类。如果植物和施肥种类不匹配,植物会死亡。问最终会死多少个植物。

思路:

  用二维树状数组维护每个格子施肥种类总和和施肥次数,最后判断植物种类*施肥次数==施肥种类总和,如果相等,这个格子植物就没有死。但如果一个格子植物种类是3,施的肥料分别是1、2、4、5,满足上面判断条件,但这个植物还是死了。

  用随机数处理这个问题,因为随机数分布离散,上面这种情况几乎不可能发生。

#include
using namespace std;

typedef long long ll;
const int maxn=1e6+10;

int a[maxn],cnt[maxn],ran[maxn];
ll sum[maxn];
int n,m,t,x,y,z,w,k;

int lowbit(int x){
    return x&-x;
}
void update(int x,int y,int add,int d)
{
    int tempy=y;
    while(x<=n)
    {
        y=tempy;
        while(y<=m)
        {
            sum[(x-1)*m+y-1]+=add;
            cnt[(x-1)*m+y-1]+=d;
            y+=lowbit(y);
        }
        x+=lowbit(x);
    }
}
bool query(int x,int y)
{
    ll rets=0,retc=0;
    int tmp=ran[a[(x-1)*m+y-1]];
    int tempy=y;
    while(x>0){
        y=tempy;
        while(y>0){
            rets+=sum[(x-1)*m+y-1];
            retc+=cnt[(x-1)*m+y-1];
            y-=lowbit(y);
        }
        x-=lowbit(x);
    }
    if(rets==retc*tmp) return true;
    else return false;
}
void init(){
    for(int i=0;i

 

你可能感兴趣的:(数据结构)