-
Notifications
You must be signed in to change notification settings - Fork 0
/
C.cpp
65 lines (55 loc) · 1.32 KB
/
C.cpp
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
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define endl "\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
int main(){
fast;
ll n,m;
cin>>n>>m;
string arr[n];
for(ll i=0;i<n;i++)
cin>>arr[i];
//precalculation
ll dpr[n][m], dpc[m][n];
// horizontally
for(ll i=0;i<n;i++)
dpr[i][0] = 0;
for(ll i=0;i<n;i++){
for(ll j=1;j<m;j++){
if(arr[i][j]=='.' && arr[i][j-1]=='.')
dpr[i][j] = dpr[i][j-1] + 1;
else
dpr[i][j] = dpr[i][j-1];
}
}
// vertically
for(ll i=0;i<m;i++)
dpc[i][0] = 0;
for(ll i=0;i<m;i++){
for(ll j=1;j<n;j++){
if(arr[j][i]=='.' && arr[j-1][i]=='.')
dpc[i][j] = dpc[i][j-1] + 1;
else
dpc[i][j] = dpc[i][j-1];
}
}
// queries
ll q;
cin>>q;
while(q--){
ll r1,r2,c1,c2;
cin>>r1>>c1>>r2>>c2;
r1--;
r2--;
c1--;
c2--;
ll ans = 0;
for(ll i=r1;i<=r2;i++)
ans += ( dpr[i][c2] - dpr[i][c1] );
for(ll i=c1;i<=c2;i++)
ans += ( dpc[i][r2] - dpc[i][r1] );
cout<<ans<<endl;
}
return 0;
}