# 牛客多校第二场 J farm

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

num 中  S[i] 表示当前位 为 0 或者 1 的总数  T[ i ] 表示 当前位为 1 的总数

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>

using namespace std;

typedef long long ll;
const int N =1e6+5;
int S[N],T[N];
int n,m,q;
int x1[N],x2[N],yy1[N],y2[N];
ll num[N];

int id(int x,int y)
{
return (x-1)*m+y;
}

{
++a[id(x1[i],yy1[i])];
if(y2[i]+1<=m) --a[id(x1[i],y2[i]+1)];
if(x2[i]+1<=n) --a[id(x2[i]+1,yy1[i])];
if(x2[i]+1<=n&&y2[i]+1<=m) ++a[id( x2[i]+1, y2[i]+1 )];
}

void work(int *a)
{
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(i>1) a[id(i,j)]+=a[id(i-1,j)];
if(j>1) a[id(i,j)]+=a[id(i,j-1)];
if(i>1&&j>1) a[id(i,j)]-=a[id(i-1,j-1)];
}
}
}

ll a[N];
int die[N];

int main()
{
scanf("%d %d %d",&n,&m,&q);
for(int i=1;i<=n*m;i++) scanf("%lld",&a[i]);
for(int i=1;i<=q;i++){
scanf("%d %d %d %d %lld",&x1[i],&yy1[i],&x2[i],&y2[i],&num[i]);
}
work(S);

for(int u=0;u<20;u++)   // 表示第  u  位
{
for(int i=0;i<=n*m;i++) T[i]=0;

for(int i=1;i<=q;i++){
if((num[i]>>u)&1){  // 表示 num[i] 的第 u 位为1
}
}
work(T);

for(int i=1;i<=n*m;i++){

int xx=(a[i]>>u); xx=xx&1;
if(xx==0){
if(T[i]) die[i]=1;  // a[i] 的当前位 为 0  但是在更改的数中当前位有为1 的数
}
else{
if(S[i]>T[i]) die[i]=1; // a[i] 的当前位 为 1 但是在更改的数中当前位有为0 的数
}
}
}

int cnt=0;
for (int i=1;i<=n*m;i++) {
if(die[i]) cnt++;
}

printf("%d\n",cnt);
return 0;
}