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
分析: 我们重新给每种化肥安排序号,尽可能的不让其之间有倍数的关系,这样的话,q次施肥之后如果当前位置的所有肥料之和是当前植物序号(也就是我们重新分配的序号)的倍数,那么当前植物不会死亡。
代码
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define bug(x) cout<<"@@ "<<x<<"\n"
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int N = (int) 1e6 + 11;
const int M = (int) 733573173; // 这个HASHD的数字一定要很奇怪质数,这样才可以更大概率的产生我们想要的数字
const int INF = (int)0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
#include <ext/rope>
using namespace __gnu_cxx;
vector<int>ve[N], X;
vector<ll>sum[N];
int ID[N];
int n, m;
int lowbit(int x){ return x & (-x); }
void add(int x, int y, int val){
for(int i = x; i < n + 2; i += lowbit(i)){
for(int j = y; j < m + 2;j += lowbit(j)){
sum[i][j] += val;
}
}
}
ll query(int x, int y){
ll ans = 0;
for(int i = x; i >= 1; i -= lowbit(i)){
for(int j = y; j >= 1; j -= lowbit(j)){
ans += sum[i][j];
}
}
return ans;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
srand((unsigned)time(NULL));
int q; scanf("%d%d%d", &n, &m, &q);
for(int i = 0; i < n + 5; i++){
for(int j = 0; j < m + 5; j++) {
ve[i].push_back(0);
sum[i].push_back(0);
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
int val; scanf("%d", &val);
ve[i][j] = val;
X.push_back(val);
}
}
int t;
memset(ID, -1, sizeof(ID)); // 离散化,重新分配标号
sort(X.begin(), X.end());
X.erase(unique(X.begin(), X.end()), X.end());
for(int i = 0; i < (int)X.size(); i++) ID[X[i]] = ( t = rand() % M ) ? t : 1 ;
while(q--){
int x1, x2, y1, y2, k; scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
if(ID[k] == -1) ID[k] = (t = rand() % M ) ? t : 1;
k = ID[k]; x2++; y2++;
add(x1, y1, k);
add(x2, y1, -k);
add(x1, y2, -k);
add(x2, y2, k);
}
int ans = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(query(i, j) % ID[ve[i][j]] != 0) ans ++;
}
}
printf("%d\n", ans);
return 0;
}
因为查询只有最后一次,所以没有必要用树状数组,可以直接标记然后查询所有位置做到o(n*m) .
代码
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define bug(x) cout<<"@@ "<<x<<"\n"
#define rep(i, l, r) for(int i = l; i <= r; i++)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int N = (int) 1e5 + 11;
const int M = (int) 1e6 + 11;
const int INF = (int)0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
#include <ext/rope>
using namespace __gnu_cxx;
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
srand((unsigned)time(NULL));
int n, m, q; scanf("%d%d%d", &n, &m, &q);
vector<vector<ll>> sum(n + 2, vector<ll>(m + 2));
vector<vector<int>> a(n + 2, vector<int>(m + 2));
vector<int>ID( n * m + 2);
for(int i = 1; i <= n * m; i++) ID[i] = rand() % MOD;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
scanf("%d", &a[i][j]);
}
}
while(q--){
int x1, y1, x2, y2, k;scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
y2++; x2++;
sum[x1][y1] += ID[k];
sum[x2][y1] -= ID[k];
sum[x1][y2] -= ID[k];
sum[x2][y2] += ID[k];
}
int ans = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
if(sum[i][j] % ID[a[i][j]] != 0) ans ++;
}
}
printf("%d\n", ans);
return 0;
}