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 | #include <bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
using namespace std;
inline ll read(){
ll x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
const int N=2e3+5,d[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int n,m,q;
char col[N][N];
pii spe[N][N];
int s[N][N],v[N][N],h[N][N];
bool in(int x,int y,int nx,int ny){
if(nx<0||ny<0||nx>n+1||ny>m+1)return 0;
if(nx==x+1)return col[x][y]!=col[x][y-1];
if(nx==x-1)return col[nx][y]!=col[nx][y-1];
if(ny==y+1)return col[x][y]!=col[x-1][y];
return col[x][ny]!=col[x-1][ny];
}
void dfs(int x,int y){//建图
rep(i,0,3){
int nx=x+d[i][0],ny=y+d[i][1];
if(in(x,y,nx,ny)){//如果两点间有边
if(nx==x+1)v[x][y]=1;
else if(nx==x-1)v[nx][y]=1;
else if(ny==y+1)h[x][y]=1;
else h[x][ny]=1;
if(!spe[nx][ny].fi){
spe[nx][ny]=spe[x][y];
dfs(nx,ny);
}
}
}
}
int sum(int f[N][N],int x,int y,int a,int b){
return f[a][b]+f[x][y]-f[x][b]-f[a][y];
}
bool flg[N][N];
bool out(int i,int j,int x,int y,int a,int b){
int nx=spe[i][j].fi,ny=spe[i][j].se;
if(nx>x&&nx<=a&&ny>y&&ny<=b&&!flg[nx][ny]){
flg[nx][ny]=1;
return 1;
}
return 0;
}
void rem(int x,int y){
int nx=spe[x][y].fi,ny=spe[x][y].se;
flg[nx][ny]=0;
}
int main(){
n=read(),m=read(),q=read();
rep(i,1,n){
scanf("%s",col[i]+1);
}
rep(i,1,n+1)rep(j,1,m+1){
if(!spe[i][j].fi){
s[i][j]=1;
spe[i][j]={i,j};
dfs(i,j);
}
}
rep(i,1,n+1)rep(j,1,m+1){
s[i][j]+=s[i][j-1];
h[i][j]+=h[i][j-1];
v[i][j]+=v[i][j-1];
}
rep(i,1,n+1)rep(j,1,m+1){
s[i][j]+=s[i-1][j];
h[i][j]+=h[i-1][j];
v[i][j]+=v[i-1][j];
}
while(q--){
int x=read(),y=read(),a=read(),b=read();
//这里 E 和 V 均去掉了外面一圈,答案不变
int E=sum(v,x-1,y,a,b)+sum(h,x,y-1,a,b);
int V=(a-x)*(b-y),C=sum(s,x,y,a,b);
rep(i,x,a+1){
if(out(i,b+1,x,y,a,b))C--;
if(out(i,y,x,y,a,b))C--;
}
rep(i,y,b+1){
if(out(a+1,i,x,y,a,b))C--;
if(out(x,i,x,y,a,b))C--;
}
rep(i,x,a+1){
rem(i,b+1);rem(i,y);
}
rep(i,y,b+1){
rem(x,i);rem(a+1,i);
}
//printf("%d %d %d\n",E,V,C);
printf("%d\n",E-V+C+1);
}
return 0;
}
|