版本比较
题解:CF611C New Year and Domino
正文
增加 46 行删除 45 行
18 行未改动1920212223232425262711 行未改动39404142434445464713 行未改动61626364646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810810918 行未改动垂直骨牌:记录在上端点位置 (i,j),下端点在 (i+1,j)
2. 前缀和数组定义三个二维前缀和数组:c[N][N], d[N[N], b[N][N];c[N][N], d[N][N], b[N][N];
4. 查询处理对于查询矩形 (r1,c1) 到 (r2,c2):11 行未改动
注意:不需要考虑左边界和上边界,因为如果骨牌跨越这些边界,它根本不会被包含在查询矩形的任何前缀和中。
第三步:最终计算公式:
b[x2][y2]-b[x1-1][y2]-b[x2][y1-1]+b[x1-1][y1-1] - c[x2][y2]-c[x2][y1-1] - d[x2][y2]-d[x1-1][y2];
其中:13 行未改动 char a[N][N];
int main(){
ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); cin >> h >> w; for(int i = 1; i <= h; i++){ for(int j = 1; j <= w; j++){ cin >> a[i][j]; } } for(int i = 1; i <= h; i++){ for(int j = 1; j <= w; j++){ if(a[i][j] == '.'){ if(a[i+1][j] == '.'){ c[i][j]++; } if(a[i][j+1] == '.'){ d[i][j]++; } } } } for(int i = 1; i <= h; i++){ for(int j = 1; j <= w; j++){ b[i][j] = b[i-1][j]+b[i][j-1]+c[i][j]+d[i][j]-b[i-1][j-1]; } } for(int i = 1; i <= h; i++){ for(int j = 1; j <= w; j++){ c[i][j] += c[i][j-1]; } } for(int j = 1; j <= w; j++){ for(int i = 1; i <= h; i++){ d[i][j] += d[i-1][j]; } } cin >> q; for(int i = 1, x1, x2, y1, y2; i <= q; i++){ cin >> x1 >> y1 >> x2 >> y2; int cnt = b[x2][y2]-b[x1-1][y2]-b[x2][y1-1]+b[x1-1][y1-1]; int ans = c[x2][y2]-c[x2][y1-1]; int tmp = d[x2][y2]-d[x1-1][y2]; cout << cnt-ans-tmp << "\n"; } ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); cin >> h >> w; for(int i = 1; i <= h; i++){ for(int j = 1; j <= w; j++){ cin >> a[i][j]; } } for(int i = 1; i <= h; i++){ for(int j = 1; j <= w; j++){ if(a[i][j] == '.'){ if(a[i+1][j] == '.'){ c[i][j]++; } if(a[i][j+1] == '.'){ d[i][j]++; } } } } for(int i = 1; i <= h; i++){ for(int j = 1; j <= w; j++){ b[i][j] = b[i-1][j]+b[i][j-1]+c[i][j]+d[i][j]-b[i-1][j-1]; } } for(int i = 1; i <= h; i++){ for(int j = 1; j <= w; j++){ c[i][j] += c[i][j-1]; } } for(int j = 1; j <= w; j++){ for(int i = 1; i <= h; i++){ d[i][j] += d[i-1][j]; } } cin >> q; for(int i = 1, x1, x2, y1, y2; i <= q; i++){ cin >> x1 >> y1 >> x2 >> y2; int cnt = b[x2][y2]-b[x1-1][y2]-b[x2][y1-1]+b[x1-1][y1-1]; int ans = c[x2][y2]-c[x2][y1-1]; int tmp = d[x2][y2]-d[x1-1][y2]; cout << cnt-ans-tmp << "\n"; } return 0;} }