18牛客多校训练第二场 J farm

题意:一个n×m的农田, 每个小格子都有一种作物, 现在喷t次农药,每次农药覆盖一个矩形, 该矩形里面与农药类型不同的植物都会死掉, 求最后植物的死亡数是多少。

题解:二维树状数组。 每次喷农药的时候将这个覆盖的区间加一。 然后对于[1,n*m]的植物, 先删除同种植物的同种农药对区间的影响, 然后查询该种植物的是否被标记过了 即 该位置的值 > 1, 最后处理完这种植物再把这种植物的农药再加回去.

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
 4 #define LL long long
 5 #define ULL unsigned LL
 6 #define fi first
 7 #define se second
 8 #define pb push_back
 9 #define lson l,m,rt<<1
10 #define rson m+1,r,rt<<1|1
11 #define max3(a,b,c) max(a,max(b,c))
12 #define min3(a,b,c) min(a,min(b,c))
13 typedef pair<int,int> pll;
14 const int inf = 0x3f3f3f3f;
15 const LL INF = 0x3f3f3f3f3f3f3f3f;
16 const LL mod =  (int)1e9+7;
17 const int N = 1e6 + 100;
18 vector<int> dp[N];
19 vector<pll> save[N];
20 int n, m;
21 int lowbit(int x)
22 {
23     return x &(-x);
24 }
25 void Add(int x, int y, int c)
26 {
27     for(int i = x; i <= n; i += lowbit(i))
28         for(int j = y; j <= m; j += lowbit(j))
29             dp[i][j] += c;
30 }
31 int Query(int x, int y)
32 {
33     int cnt = 0;
34     for(int i = x; i > 0; i -= lowbit(i))
35         for(int j = y; j > 0; j -= lowbit(j))
36            cnt += dp[i][j];
37     return cnt;
38 }
39 struct Node{
40     int x1, y1, x2, y2, k;
41 }A[N];
42 bool cmp(Node &x1, Node &x2){
43     return x1.k < x2.k;
44 }
45 int main()
46 {
47     int  t, v;
48     int ans = 0;
49     scanf("%d%d%d", &n, &m, &t);
50     for(int i = 1; i <= n; i++)
51         dp[i].resize(m+10);
52     for(int i = 1; i <= n; i++){
53         for(int j = 1; j <= m; j++){
54             scanf("%d", &v);
55             save[v].pb(pll(i,j));
56         }
57     }
58     for(int i = 1; i <= t; i++){
59         scanf("%d%d%d%d%d", &A[i].x1, &A[i].y1 , &A[i].x2, &A[i].y2, &A[i].k);
60         Add(A[i].x1, A[i].y1, 1);
61         Add(A[i].x2+1,A[i].y2+1,1);
62         Add(A[i].x1,A[i].y2+1,-1);
63         Add(A[i].x2+1,A[i].y1,-1);
64     }
65     sort(A+1,A+1+t,cmp);
66     for(int i = 1, j = 1, zz; i <= n * m; i++){
67  
68         while(A[j].k < i && j <= t)   j++;
69         if(save[i].size() == 0) continue;
70         zz = j;
71         while(A[zz].k == i && zz <=t) zz++;
72         for(int z = j; z < zz; ++z){
73             Add(A[z].x2+1,A[z].y2+1,-1);
74             Add(A[z].x1,A[z].y1,-1);
75             Add(A[z].x1,A[z].y2+1,1);
76             Add(A[z].x2+1,A[z].y1,1);
77         }
78         for(auto it : save[i]){
79             if(Query(it.fi, it.se)){
80                 ans++;
81             }
82         }
83         for(int z = j; z < zz; ++z){
84             Add(A[z].x2+1,A[z].y2+1,1);
85             Add(A[z].x1,A[z].y1,1);
86             Add(A[z].x1,A[z].y2+1,-1);
87             Add(A[z].x2+1,A[z].y1,-1);
88         }
89         j = zz;
90     }
91     cout << ans << endl;
92     return 0;
93 }
View Code

 

posted @ 2018-07-21 17:03  Schenker  阅读(349)  评论(0编辑  收藏  举报