Codeforces Good Bye 2015 C. New Year and Domino 前缀和
C. New Year and Domino
题目连接:
http://www.codeforces.com/contest/611/problem/C
Description
They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so.
Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right.
Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid.
Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle?
Input
The first line of the input contains two integers h and w (1 ≤ h, w ≤ 500) – the number of rows and the number of columns, respectively.
The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' — denoting an empty or forbidden cell, respectively.
The next line contains a single integer q (1 ≤ q ≤ 100 000) — the number of queries.
Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1 ≤ r1i ≤ r2i ≤ h, 1 ≤ c1i ≤ c2i ≤ w) — the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle.
Output
Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle.
Sample Input
5 8
....#..#
.#......
.#....
..#.##
........
4
1 1 2 3
4 1 4 1
1 2 4 5
2 5 5 8
Sample Output
4
0
10
15
Hint
题意:
给你一个500 500的矩形区域,然后有一些位置是不能放的。有Q次询问,每次询问问你在(x1,y1,x2,y2)矩形区域中,能放入多少个1*2的矩形块。
题解
我们横着和竖着的都分开考虑。横着和竖着能放多少个呢?我们维护前缀和去解决就好了,如果上一位是空位,并且这一位置也是空位,就可以+1。n^2预处理,O(500)的单次询问。
代码
#include<bits/stdc++.h>
using namespace std;
int h,w;
char s[505][505];
int num[505][505];
int A[505][505];
int B[505][505];
int main()
{
cin>>h>>w;
for(int i=1;i<=h;i++)
scanf("%s",s[i]+1);
for(int i=1;i<=h;i++)
for(int j=1;j<=w;j++)
if(s[i][j]=='.')num[i][j]=1;
else num[i][j]=0;
for(int i=1;i<=h;i++)
{
for(int j=1;j<=w;j++)
{
if(num[i][j]&&num[i][j+1])A[i][j]=A[i][j-1]+1;
else A[i][j]=A[i][j-1];
}
}
for(int i=1;i<=w;i++)
{
for(int j=1;j<=h;j++)
{
if(num[j][i]&&num[j+1][i])B[j][i]=B[j-1][i]+1;
else B[j][i]=B[j-1][i];
}
}
int q;cin>>q;
while(q--)
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
int ans = 0;
for(int i=x1;i<=x2;i++)
ans+=A[i][y2-1]-A[i][y1-1];
for(int i=y1;i<=y2;i++)
ans+=B[x2-1][i]-B[x1-1][i];
printf("%d\n",ans);
}
}
Codeforces Good Bye 2015 C. New Year and Domino 前缀和的更多相关文章
- Codeforces Good bye 2015 B. New Year and Old Property dfs 数位DP
B. New Year and Old Property 题目连接: http://www.codeforces.com/contest/611/problem/B Description The y ...
- Codeforces Good Bye 2015 A. New Year and Days 水题
A. New Year and Days 题目连接: http://www.codeforces.com/contest/611/problem/A Description Today is Wedn ...
- codeforces Good Bye 2015 B. New Year and Old Property
题目链接:http://codeforces.com/problemset/problem/611/B 题目意思:就是在 [a, b] 这个范围内(1 ≤ a ≤ b ≤ 10^18)统计出符合二进制 ...
- Codeforces Good Bye 2015 D. New Year and Ancient Prophecy 后缀数组 树状数组 dp
D. New Year and Ancient Prophecy 题目连接: http://www.codeforces.com/contest/611/problem/C Description L ...
- Good Bye 2015 C. New Year and Domino 二维前缀
C. New Year and Domino They say "years are like dominoes, tumbling one after the other". ...
- Good Bye 2015 C - New Year and Domino
题意:计算给定矩形面积(r1,c1),(r2,c2)内长度为2的有多少个?向右或向下计算. 思路:预处理字符.分别向右和向下处理.注意边界情况,可能算多了.用容斥原理计算长度为二的单位. #inclu ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】
[CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...
- Codeforces:Good Bye 2018(题解)
Good Bye 2018! 题目链接:https://codeforces.com/contest/1091 A. New Year and the Christmas Ornament 题意: 给 ...
随机推荐
- 让jquery.tmpl.js支持index序号
在写Web程序时,想简单处理会使用JS模板,常用的是Jquery的jquery.tmpl.js插件.整个插件还是比较好用的,后续有机会结合实际应用案例,分享下应用方法. 本次文章想分享的一点是其中的一 ...
- 把raw目录下的几张照片存放到SD卡里面去
try { //SD卡路径 String filename =android.os.Environment .getExternalStorageDirectory().getAbsolutePath ...
- Selenium2Library系列 keywords 之 _SelectElementKeywords 之 unselect_from_list(self, locator, *items)
def unselect_from_list(self, locator, *items): """Unselects given values from select ...
- Yii笔记---redirect重定向
Yii的redirect方法在CControler与CHttpRequest之中都有被定义,CController中的redirect调用了CHttpRequest中的redirect方法.我们平常调 ...
- python中的多线程【转】
转载自: http://c4fun.cn/blog/2014/05/06/python-threading/ python中关于多线程的操作可以使用thread和threading模块来实现,其中th ...
- 15+ 易响应的CSS框架快速开启你的敏捷网站项目
由 于移动互联用户的快速增加,现在数量已经超出10亿,几乎可以肯定的是你的网站每天都会有移动用户访问.如果你组织计划创建一个对移动用户友好的浏览体 验,有多个方面需要考虑.响应快速的网站设计似乎现在很 ...
- 一行 Python 实现并行化 -- 日常多线程操作的新思路
春节坐在回家的火车上百无聊赖,偶然看到 Parallelism in one line 这篇在 Hacker News 和 reddit 上都评论过百的文章,顺手译出,enjoy:-) http:// ...
- VS2012打包部署Winform程序
打包前的准备工作: 新建一个打包部署项目,点OK,如果是第一次使用的话,会打开一个网页,按照提示的步骤来做, 点击上面的step2的网址,进入到另一个网页: 填写完右边的信息,点击“download ...
- Strider 持续集成(gitlab)
Strider安装后运行: Mac: strider Ubuntu: bin/strider 本地运行时浏览器访问: http://127.0.0.1:3000 其他服务器:服务器地址 + 端口号(3 ...
- 【转】类中如何引用server.MapPath()
转至:http://blog.csdn.net/tangjianft/article/details/5357151 今天在写一个上传图片方法时遇到了两个问题:1.public string getI ...