牛客暑期多校第二场 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(nm<=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

思路

牛客网暑期ACM多校训练营(第二场)J.farm (随机数+二维树状数组)

之前一直想补这个题目,但是当时没看明白题解(主要是没明白大家说的随机是什么意思

今天回头补题的时候看到了这个博客,感觉写的很好,豁然开朗

PS:

注意在牛客提交的时候选择C++11

我的二维树状数组模板

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* ===============================================================
*
* Filename: J.cpp
*
* Link: https://www.nowcoder.com/acm/contest/140/J
*
* Version: 1.0
* Created: 2018/10/21 19时50分25秒
* Revision: none
* Compiler: g++
*
* Author: 杜宁元 (https://duny31030.top/), duny31030@126.com
* Organization: QLU_浪在ACM
*
* ================================================================
*/
#include <bits/stdc++.h>
using namespace std;
#define clr(a, x) memset(a, x, sizeof(a))
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define pre(i,a,n) for(int i=n;i>=a;i--)
#define ll long long
#define max3(a,b,c) fmax(a,fmax(b,c))
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 1e6+10;
int t,n,m,x,x11,y11,x22,y22;
vector<ll> tree[N];
vector<ll> mp[N];
ll has[N];
void init()
{
srand(time(NULL));
for(int i = 1;i < N-5;i++)
has[i] = (ll)rand()*1e6+rand()*rand();
}


void add(int x,int y,ll z)
{
int memy_y = y;
while(x <= n)
{
y = memy_y;
while(y <= m)
tree[x][y] += z,y += y&(-y);
x += x&(-x);
}
}

void range_add(int xa,int ya,int xb,int yb,ll z)
{
add(xa,ya,z);
add(xa,yb+1,-z);
add(xb+1,ya,-z);
add(xb+1,yb+1,z);
}

ll ask(int x,int y)
{
ll res = 0,memy_y = y;
while(x)
{
y = memy_y;
while(y)
res += tree[x][y],y -= y&(-y);
x -= x&(-x);
}
return res;
}

int main()
{
ios
#ifdef ONLINE_JUDGE
#else
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
scanf("%d %d %d",&n,&m,&t);
init();
rep(i,1,n)
{
mp[i].push_back(0);
tree[i].push_back(0);
rep(j,1,m)
{
scanf("%d",&x);
mp[i].push_back(has[x]);
tree[i].push_back(0);
}
}
ll ans = 0,xx;
while(t--)
{
scanf("%d %d %d %d %lld",&x11,&y11,&x22,&y22,&xx);
range_add(x11,y11,x22,y22,has[xx]);
}
rep(i,1,n)
rep(j,1,m)
if(ask(i,j) % mp[i][j])
ans++;
printf("%lld\n",ans);
fclose(stdin);
// fclose(stdout);
return 0;
}