CF Good Bye 2015 C- New Year and Domino(CF611C)
来源:互联网 发布:java防止接口频繁调用 编辑:程序博客网 时间:2024/05/16 04:57
http://codeforces.com/contest/611/problem/C
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 1through 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?
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 andc1i 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.
Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle.
5 8....#..#.#......##.#....##..#.##........41 1 2 34 1 4 11 2 4 52 5 5 8
401015
7 39........................................###..###..#..###.....###..###..#..###....#..#.#..#..#.........#..#.#..#..#....###..#.#..#..###.....###..#.#..#..###..#....#.#..#....#.....#....#.#..#..#.#..###..###..#..###.....###..###..#..###........................................61 1 3 202 10 6 302 10 7 302 2 7 71 7 7 71 8 7 8
53891202302
A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways.
题意:给你一张图,让你数有多少对能连起来的点(任何一个点都能和上下左右的点连起来)
思路:q有100000,那爆搜肯定要超时,然后最直接想到的就是能不能打表来降低时间复杂度,第一思路是用一个二维数组a[i][j]来存前i行j列有多少个点,然后我们假设全部都是点,算会连多少条线,接着我们只需要输出正方形四条边的上有几个#就行了,因为每个不在边上的#会使答案减8,在边上的#会使答案减6,在顶点的#会使答案减4,这样子时间复杂度就只有O(HQ)了,但毕竟q是100000,可能还是要超,后来又想了想,为什么不直接用数组来存每片区域有多少对点呢?用两个数组来存,一个存竖着的,一个存横着的,之后再进行简单的类似于面积计算就行了
code:
#include <iostream>#include <cstdio>#include <queue>#include <stack>#include <map>using namespace std;char m[505][505];int a[505][505];int b[505][505];int main(){ int h,w; cin>>h>>w; for(int i=1;i<=h;i++) { int n=0; for(int j=1;j<=w;j++) { cin>>m[i][j]; } } for(int i=1;i<=h;i++) { for(int j=1;j<=w;j++) { if(m[i][j]=='.' && m[i-1][j]=='.') a[i][j]++; if(m[i][j]=='.' && m[i][j-1]=='.') b[i][j]++; a[i][j]+=a[i-1][j]+a[i][j-1]-a[i-1][j-1]; b[i][j]+=b[i-1][j]+b[i][j-1]-b[i-1][j-1]; } } int x1,y1,x2,y2,q; cin>>q; while(q--) { cin>>x1>>y1>>x2>>y2; cout<< a[x2][y2]-a[x1][y2]-a[x2][y1-1]+a[x1][y1-1] + b[x2][y2]-b[x1-1][y2]-b[x2][y1]+b[x1-1][y1]<<endl; } return 0;}
- CF Good Bye 2015 C- New Year and Domino(CF611C)
- CF Good Bye 2015 C. New Year and Domino && D. New Year and Ancient Prophecy (DP)
- Codeforces Good Bye 2015 C. New Year and Domino (预处理)
- Good Bye 2015 C New Year and Domino(dp)
- codeforces Good Bye 2015 C - New Year and Domino
- codeforces-Good Bye 2015-New Year and Domino
- Codeforces 611C. New Year and Domino (Good Bye 2015 C题)
- CF Good Bye 2015 A- New Year and Days(CF611A)
- Good Bye 2015 C. New Year and Domino dp+容斥原理
- CF Good Bye 2016 C New Year and Rating 模拟乱搞
- CF~Good Bye 2014 C. New Year Book Reading
- Good Bye 2014 E. New Year Domino
- CF Good Bye 2015 B- New Year and Old Property(CF611B)
- CF Good Bye 2015 E. New Year and Three Musketeers(贪心+枚举)
- CF Good Bye 2015 F. New Year and Cleaning(思维)
- codeforces Good Bye 2016-C New Year and Rating(二分)
- Good Bye 2016 C. New Year and Rating(模拟)
- Good Bye 2016 C. New Year and Rating思维
- 最近使用Android studio 2.0 遇见的一些问题
- OC点语法的使用
- 大文件的下载(显示进度条)
- hashmap源码剖析
- Ransac
- CF Good Bye 2015 C- New Year and Domino(CF611C)
- MAC 系统下使用vmwarefusion 双虚拟机调试windows驱动
- 如何在Java中定义常量(Constant)
- CSRF 攻击的应对之道
- 31个Hadoop面试经典问题
- SimpleAdapter
- 关于python的open函数的误区。
- C++内容总结--面向对象语言特点
- 2016.1.1 Ubuntu 国内源 亲测