牛客網暑期ACM多校訓練營(第二場)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
題意:
N*M的矩形,裡面種植物,每種植物適應的藥物不同,如果植物受到不適應的藥物就會死,現在T次打藥,每次在一個矩形中打藥,問最終死了多少植物?
分析:二位樹狀陣列+隨機化。對每種植物適應的藥性值進行隨機化,然後用二位樹狀陣列來維護T次矩形區域中每種植物所施加的藥性Hash值的總和,最終判斷藥性總和是否為植物所適應的藥性Hash值的倍數。
程式碼:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <vector>
#include <stack>
#define lowbit(x) x&(-x)
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
vector<ll> G[maxn];
vector<ll> ans[maxn];
ll Hash[maxn];
int n,m,t;
void init()
{
for(int i=1;i<=1e6+6;i++)
{
Hash[i]=rand()*1e6+rand()*rand();
}
}
void update(int x,int y,ll val)
{
for(int i=x;i<=n;i+=lowbit(i))
{
for(int j=y;j<=m;j+=lowbit(j))
{
ans[i][j]+=val;
}
}
}
ll qury(int x,int y)
{
ll res=0;
for(int i=x;i>=1;i-=lowbit(i))
{
for(int j=y;j>=1;j-=lowbit(j))
{
res+=ans[i][j];
}
}
return res;
}
int main()
{
init();
scanf("%d%d%d",&n,&m,&t);
for(int i=1;i<=n;i++)
{
G[i].push_back(0);
ans[i].push_back(0);
for(int j=1;j<=m;j++)
{
int x;
scanf("%d",&x);
G[i].push_back(Hash[x]);
ans[i].push_back(0);
}
}
for(int i=1;i<=t;i++)
{
int x1,x2,y1,y2,k;
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&k);
update(x1,y1,Hash[k]);
update(x2+1,y2+1,Hash[k]);
update(x1,y2+1,-Hash[k]);
update(x2+1,y1,-Hash[k]);
}
int sum=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
ll s=qury(i,j);
if(s%G[i][j])
sum++;
}
}
printf("%d\n",sum);
return 0;
}