牛客网第二场Jfarm(随机化+二维前缀和)

链接: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

随机化,避免4=1+3=2+2

让他只有2+2;


#include<cstdio> #include<algorithm> #include<cmath> #include<cstdlib> #include<ctime> #include<vector> using namespace std; #define ll long long int n,m,t; int b[1000005]; ll a[1000008],qw; vector<ll>g[1000008]; int x,y,q,w,z; int main() { srand(time(0)); scanf("%d%d%d",&n,&m,&t); for(int i=0;i<=n+1;i++) g[i].resize(m+5); for(int i=0;i<=n*m;i++) { a[i]=(ll)rand()*1000001+(ll)rand(); a[i]=a[i]+(ll)rand()*1000001+(ll)rand(); } for(int i=1;i<=n*m;i++) scanf("%d",&b[i]); while(t--) { scanf("%d%d%d%d%d",&x,&y,&q,&w,&z); g[x][y]+=a[z]; g[x][w+1]-=a[z]; g[q+1][y]-=a[z]; g[q+1][w+1]+=a[z]; } int ans=0; for(int i=1;i<=n*m;i++) { x=i/m+1; y=i%m; if(y==0) y=m,x--; qw=g[x][y]+=g[x-1][y]+g[x][y-1]-g[x-1][y-1]; if(qw%a[b[i]]) ans++; } printf("%d",ans); return 0; }

猜你喜欢

转载自www.cnblogs.com/2014slx/p/9388068.html