牛客网暑期ACM多校训练营(第二场)J题

牛客网暑期ACM多校训练营(第二场)J题

 

链接: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次操作,每次给你一个矩形区域的左上角和右下角坐标,在该区域喷洒一种农药,与农药不是同一种类型的植物都会死亡,最后求所有操作之后共有多少植物死去。

题解:假设种类只有01两种,那么只需要利用矩阵前缀和统计每个位置喷洒01的个数,然后遍历每一块区域,如果0和1的个数均大于0,那么该植物死亡。再回到原问题,如果两个数不同,那么他们的二进制中至少有一位不同,数据范围是1e6,二进制位不会超过20位,我们可以遍历每一位二进制位,这样就可以转换成只有01两种了。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int map[2000005], s[2000005],used[2000005],num[2000005][2],x1[2000005],yy1[2000005],x2[2000005],y2[2000005],kk[2000005];
int find(int n,int k) {
    int a;
    while (k--) {
        a = n % 2;
        n /= 2;
    }
    return a;
}
int main() {
    int n, m, t,res=0;
    scanf("%d%d%d", &n, &m, &t);
    int i, j;
    for (i = 1; i <= n; i++) {
        for(j=1;j<=m;j++)
            scanf("%d", &map[i*m+j]);
    }
    for (i = 1; i <= t; i++)
        scanf("%d%d%d%d%d", &x1[i], &yy1[i], &x2[i], &y2[i], &kk[i]);
    memset(s, 0, sizeof s);
    
    for (int k = 1; k <= 21; k++) {
        for (i = 1; i <= n; i++)
            for (j = 1; j <= m; j++)
                num[i*m + j][0] = num[i*m + j][1] = 0;
        for (int ii = 1; ii <= t; ii++) {
            int e = find(kk[ii], k);
            num[x1[ii]*m+yy1[ii]][e]++;
            num[(x2[ii] + 1)*m+y2[ii] + 1][e]++;
            num[x1[ii]*m+y2[ii] + 1][e]--;
            num[(x2[ii] + 1)*m+yy1[ii]][e]--;
        }
        for (i = 1; i <= n; i++) {
            for (j = 1; j <= m; j++) {
                int c = 0, d = 0;
                if (find(map[i*m + j], k) == 0)
                    c++;
                else
                    d++;
                num[i*m + j][0] = num[i*m + j][0] + num[(i - 1)*m + j][0] + num[i*m + j - 1][0] - num[(i - 1)*m + j - 1][0];
                num[i*m + j][1] = num[i*m + j][1] + num[(i - 1)*m + j][1] + num[i*m + j - 1][1] - num[(i - 1)*m + j - 1][1];
                c += num[i*m + j][0]; d += num[i*m + j][1];
                if (c > 0 && d > 0)
                    s[i*m + j]++;
            }
        }
    }
 
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= m; j++) {
            if (s[i*m + j])
                res++;
        }
    }
    printf("%d\n", res);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值