牛客网第二场 j farm (二维树状数组+hash)

farm

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

时间限制: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

题目大意: 给出一个矩阵,矩阵里每一个格子里边有一个数字,代表这朵花可以接受的肥料编号,如果浇了别的编号的肥料,这朵花就会死亡。

解题思路: 题解是把1e6内的数字都用一个随机数替代。然后每次对二维区间修改的时候,就用这个随机数去更新。如果只浇了一种肥料,那么该点的值%该点原来的值一定等于0.

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<vector>
#include<ctime>
#include<cmath>
#include<algorithm>
using namespace std;
#define LL long long
#define inf 0x3f3f3f3f
#define lowb(x) x&-x
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define per(i,j,k) for(int i=j;i>=k;i--)
#define N 10005
#define pb(x) push_back(x)
#define esp 1e-6
#define debug cout<<"------debug------"<<endl;
vector<LL>mp[1000005];
vector<LL>sum[1000005];
LL h[1000005];
int  n,m,t;

void init()
{
   for(int i=1;i<=1000000;i++)
   {
       h[i]=i*i+1e9+7;
   }
}

void add(int x,int y,LL k)
{
    for(int i=x;i<=n;i+=lowb(i))
    {

        for(int j=y;j<=m;j+=lowb(j))
        {
            sum[i][j]+=k;
        }
    }
}

LL query(int x,int y)
{
     LL ans=0;
     for(int i=x;i>0;i-=lowb(i))
     {
         for(int j=y;j>0;j-=lowb(j))
         {
             ans+=sum[i][j];
         }
     }
     return ans;
}


int main()
{
    scanf("%lld%lld%lld",&n,&m,&t);
    init();
    int  tem;
    rep(i,1,n)
    {
        mp[i].resize(m+1);
        sum[i].resize(m+1);
        sum[i][0]=0;
        rep(j,1,m)
        {
            scanf("%d",&tem);
            mp[i][j]=h[tem];
        }
    }
    int x1,y1,x2,y2,w;
    rep(i,1,t)
    {
        scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&w);
        add(x1,y1,h[w]);
        add(x2+1,y2+1,h[w]);
        add(x2+1,y1,-h[w]);
        add(x1,y2+1,-h[w]);
    }
    int ss=0;
    rep(i,1,n)
    {
        rep(j,1,m)
        {
            if(query(i,j)%mp[i][j]!=0)ss++;
        }
    }
    printf("%d\n",ss);
}

猜你喜欢

转载自blog.csdn.net/weixin_40894017/article/details/81211433