farm

本文介绍了一个关于在矩阵中施肥的问题,需要通过编程算法来确定错误施肥导致植物死亡的数量。使用了树状数组进行区间更新和查询,并引入随机数解决冲突。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接: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的矩形里,给植物施肥,如果不匹配植物就会死去,问植物死了多少

分析:

如果用树状数组更新区间,再还原过85%,就会TLE,所以用判断该点改变的总和跟(改变的次数与该点的数的乘积)是否相等,这样做会wrong,不会TLE,wrong是因为存在当原数为3,更新4次,分别为1、5、2、4这种情况下,和恰好为改变的次数与该点的数的乘积,这个时候可以做一个映射,消除数与数之间的联系(可以用随机数实现)

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
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<maxn;i++){
        ran[i]=rand();
    }
}
int main(){
    init();
    while(scanf("%d%d%d",&n,&m,&t)==3){
        memset(cnt,0,sizeof(cnt));
        memset(sum,0,sizeof(sum));
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                scanf("%d",&a[i*m+j]);
            }
        }
        for(int i=0;i<t;i++){
            scanf("%d%d%d%d%d",&x,&y,&z,&w,&k);
            update(z+1,w+1,ran[k],1);
            update(x,y,ran[k],1);
            update(x,w+1,-ran[k],-1);
            update(z+1,y,-ran[k],-1);
        }
        int ans=n*m;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++)
                if(query(i,j)) ans--;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

### 关于Phone Farm的理解 在当前提供的引用中,并未直接提及“Phone Farm”的具体定义或相关内容。然而,通过分析已有的引用内容以及扩展的知识背景,“Phone Farm”可能涉及以下几个方面的理解: 1. **与移动设备测试相关** 在引用[1]中提到的Device Farm是一个用于自动化测试iOS和Android应用的服务平台[^1]。虽然这里的重点在于iOS设备上的`getSize()`方法可能出现的问题,但可以推测“Phone Farm”可能是某种形式的移动端设备集群管理方案,类似于AWS Device Farm这样的服务,专门用来批量管理和测试多台手机设备。 2. **与驾驶行为检测项目关联** 引用[2]提到了一个名为“State Farm Distracted Driver Detection”的Kaggle竞赛项目[^2]。该项目的目标是基于车内拍摄的照片预测驾驶员的行为(如发短信、吃东西等)。因此,“Phone Farm”也可能指代一种数据采集环境或者模拟器,用于生成大量关于手机使用的场景数据以支持类似的AI模型训练。 3. **算法优化中的潜在含义** 引用[3]讨论了一种特定的数据结构——树状数组的应用及其局限性[^3]。尽管这一部分主要关注的是编程技巧而非实际应用场景,但从广义上看,“Farm”一词通常表示集合体的概念。“Phone Farm”或许可被解释为一组协同工作的智能手机,在分布式计算环境中共同完成某些复杂任务。 综上所述,“Phone Farm”并没有明确的标准定义,但在不同语境下可能存在如下几种可能性: - 移动端设备的大规模部署与管理; - 针对手机使用习惯的研究实验场所; - 利用多个真实/虚拟化的手机节点来提升性能表现的技术架构设计思路。 以下是针对上述假设提供的一段Python伪代码示例展示如何创建简单的phone farm框架雏形: ```python class Phone: def __init__(self, id): self.id = id def run_test(self): pass # 实现具体的测试逻辑 def create_phone_farm(num_phones=10): phones = [] for i in range(1, num_phones + 1): new_device = Phone(i) phones.append(new_device) return phones if __name__ == "__main__": my_farm = create_phone_farm() for device in my_farm: print(f'Start testing on {device.id}') device.run_test() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值