[sicily online]1033. City Road

来源:互联网 发布:淘宝红色外套 编辑:程序博客网 时间:2024/05/20 20:17

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

Long long ago, city Old was all around the water and was divided into M*N small square houses. The city Old had only two bridges, in the southwest corner and northeast corner. It’s obvious that the citizens can have C(m+n, n) different ways with shortest length, to go from one bridge to the other.

     As the city developed, bigger buildings came out and blocked some streets. These new buildings were always large rectangles combining many small houses. Since the new buildings may affect the number of shortest ways a lot, it’s you job now to recalculate the number.

     The city has M rows and N columns of houses, M+1 horizontal streets, and N+1 vertical streets. Suppose the house in the southwest corner is (1, 1), then the house in the northeast corner can be marked as (m, n). And then each new building can be described by its southwest corner(house) and its size(number of houses) in horizontal and vertical directions.

     The picture below shows that the city Old has 5*6 houses and one 3*2 building with its southwest corner located at (2,4). The number of different shortest ways in the picture is 192.

Input

This problem has multiple test cases. In each test case, the first line is two integer m, n(m*n<=1,000,000)which indicate the size of city Old, in vertical and horizontal directions respectively. And the second line has one integer B(B<=1000) what is the number of the new buildings. After that there are B lines. Each line has four integers x, y, a, b, describing the southwest corner (x, y) , the vertical size a, and the horizontal size b of the building. The buildings may be overlapped with each other. The input is terminated by a case with m*n=0, which should not be processed.

Output

For each test case output one integer representing the number of shortest ways in a single line. This number is guaranteed to be less than 2^63.

Sample Input

5 612 4 3 20 0

Sample Output

192

题目分析:

任意一点的路径数都等于它左面点的路径数+它下面点的路径数。这样递归公式就有了,按照常理应该是用一个二维数组来模拟,但是题目中的m和n会很大。不能直接申请二维数组。

因为m*n<1000000,所以我根据m和n的值动态申请空间,但还是MLE

后来改成用全局一维数组来模拟二维数组,AC了a[i][j]=a[i*n+j]

/**/#include<iostream>#include <iomanip>#include<stdio.h>#include<cmath>#include<iomanip>#include<list>#include <map>#include <vector>#include <string>#include <algorithm>#include <sstream>#include <stack>#include<queue>#include<string.h>#include<set>using namespace std;typedef struct CROSS{bool used; //该点是否可以通过long long value;//到该点有多少路径}Cross;Cross gra[2000002];//注意为啥是2000002int main(){int n,m;while(cin>>m>>n&&m*n!=0){m++;n++;for(int i=0;i<m;i++){for(int j=0;j<n;j++){Cross tmp;tmp.used=false;tmp.value=1;gra[i*n+j]=tmp;}}int B;cin>>B;for(int i=0;i<B;i++){int ii,jj,sizei,sizej;cin>>ii>>jj>>sizei>>sizej;for(int x=0;x<sizei-1;x++){for(int y=0;y<sizej-1;y++){gra[(ii+x)*n+jj+y].used=true;gra[(ii+x)*n+jj+y].value=0;}}}for(int i=0;i<m;i++)//初始化行{if(gra[i*n].used){for(int j=i+1;j<m;j++)gra[j*n].value=0;break;}}for(int i=0;i<n;i++)//初始化列{if(gra[i].used){for(int j=i+1;j<m;j++)gra[j].value=0;break;}}for(int i=1;i<m;i++){for(int j=1;j<n;j++){if(!gra[i*n+j].used)gra[i*n+j].value=gra[(i-1)*n+j].value+gra[i*n+j-1].value;}}cout<<gra[(m-1)*n+n-1].value<<endl;}}