链接: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.
输入
2 2 2
1 2
2 3
1 1 2 2 2
2 1 2 1 1
输出
3
题意:n*m的花圃,每个格子上都有一种类型为x[i][j]的花,T次操作,每次操作给一个子矩阵内所有的花都喷洒类型为k的农药,这个子矩阵内所有类型不为k的花就会全部枯萎,问最后有多少花会枯萎
将每个格子上花也当成一次操作,例如第3行4列花种类为7,那么就同等于向第3行第4列这一个格子构成的“子矩阵”里喷洒第7种农药,那么最后对于每个格子只要被喷上了两种不同的农药,那么这个格子上的花就会枯萎
这样每个格子最多只会被暴力2次,因为第2次这个格子就可以直接扔掉了,它上面的花已经枯萎了
写法应该不唯一,而且我好像写丑了
#include<stdio.h>
#include<set>
#include<vector>
using namespace std;
typedef struct
{
int x1, y1;
int x2, y2;
}Res;
Res now, del[1000005];
vector<Res> G[1000005];
set<int> st1[1000005], st2[1000005], zt[2]; //s[0][i]里面所有的花还未被喷洒农药、s[1][i]里面所有的花都只被喷洒过一种农药,很显然被喷洒过两种农药的花必死无疑
int main(void)
{
set<int>::iterator t1, t2, L1, L2, R1, R2;
int cnt, n, m, T, i, j, x, ans, a, b, x1, x2, y1, y2, k;
scanf("%d%d%d", &n, &m, &T);
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%d", &x);
now.x1 = now.x2 = i;
now.y1 = now.y2 = j;
G[x].push_back(now); //将每个格子上花的种类也当成一次操作,例如第3行4列花种类为7,那么就同等于向第3行第4列这一个格子构成的“子矩阵”里喷洒第7种农药
}
}
for(i=1;i<=T;i++)
{
scanf("%d%d%d%d%d", &now.x1, &now.y1, &now.x2, &now.y2, &x);
G[x].push_back(now);
}
for(i=1;i<=n;i++)
{
zt[0].insert(i);
for(j=1;j<=m;j++)
st1[i].insert(j);
}
ans = 0;
for(i=1;i<=n*m;i++)
{
for(j=0;j<G[i].size();j++)
{
x1 = G[i][j].x1, x2 = G[i][j].x2;
y1 = G[i][j].y1, y2 = G[i][j].y2;
L1 = zt[1].lower_bound(x1);
R1 = zt[1].lower_bound(x2+1);
cnt = 0;
for(t1=L1;t1!=R1;t1++)
{
a = *t1;
L2 = st2[a].lower_bound(y1);
R2 = st2[a].lower_bound(y2+1);
for(t2=L2;t2!=R2;t2++)
{
b = *t2;
ans++;
now.x1 = a, now.y1 = b;
del[++cnt] = now;
}
}
for(k=1;k<=cnt;k++)
{
st2[del[k].x1].erase(del[k].y1);
if(st2[del[k].x1].empty())
zt[1].erase(del[k].x1);
}
}
for(j=0;j<G[i].size();j++)
{
x1 = G[i][j].x1, x2 = G[i][j].x2;
y1 = G[i][j].y1, y2 = G[i][j].y2;
L1 = zt[0].lower_bound(x1);
R1 = zt[0].lower_bound(x2+1);
cnt = 0;
for(t1=L1;t1!=R1;t1++)
{
a = *t1;
L2 = st1[a].lower_bound(y1);
R2 = st1[a].lower_bound(y2+1);
for(t2=L2;t2!=R2;t2++)
{
b = *t2;
now.x1 = a, now.y1 = b;
del[++cnt] = now;
}
}
for(k=1;k<=cnt;k++)
{
st1[del[k].x1].erase(del[k].y1);
if(st1[del[k].x1].empty())
zt[0].erase(del[k].x1);
if(st2[del[k].x1].empty())
zt[1].insert(del[k].x1);
st2[del[k].x1].insert(del[k].y1);
}
}
}
printf("%d\n", ans);
return 0;
}
/*
2 2 0
1 1
1 1
1 1 0
1
*/