Codeforces 372B counting rectangles is fun

Source: Internet
Author: User

Test instructions: give you a 40x40 01 rectangle, and now give you a Q query (1,1000000), how many rectangles in a rectangle and 0.

Thinking of solving problems: Dp,n^4 pretreatment, mainly two state transfer equations

1) Dp[i][j][s] = Dp[i][j-1][s] + 1 (mp[i][j] = = 0)

2) Ans[lx][ly][i][j] + + ans[lx][ly][i-1][j] + ans[lx][ly][i][j-1]-ans[lx][ly][i-1][j-1]

Problem Solving Code:

1 //File name:372b.cpp2 //Author:darkdream3 //Created time:2015 March 29 Sunday 20:52 13 seconds4 5#include <vector>6#include <list>7#include <map>8#include <Set>9#include <deque>Ten#include <stack> One#include <bitset> A#include <algorithm> -#include <functional> -#include <numeric> the#include <utility> -#include <sstream> -#include <iostream> -#include <iomanip> +#include <cstdio> -#include <cmath> +#include <cstdlib> A#include <cstring> at#include <ctime> - #defineLL Long Long - #defineMAXN 45 - using namespacestd; - intN, M, q; - intDP[MAXN][MAXN][MAXN]; in intVAL[MAXN][MAXN]; - intANS[MAXN][MAXN][MAXN][MAXN]; to intMP[MAXN][MAXN]; + voidSolveintLxintly) - { theMemset (DP,0,sizeof(DP)); *    for(inti = lx; I <= N;i + +) $       for(intj = ly; J <= M;j + +)Panax Notoginseng      { -            for(ints = i; s >= lx; S--) the           { +             if(Mp[s][j] = =0 ) A             { theDp[i][j][s] = dp[i][j-1][s] +1;  +ANS[LX][LY][I][J] + =Dp[i][j][s]; -}Else{ $                Break; $             } -           } -ANS[LX][LY][I][J] + = ans[lx][ly][i-1][J] + ans[lx][ly][i][j-1]-ans[lx][ly][i-1][j-1] ; the      } - }Wuyi intMain () { thescanf" %d%d%d",&n,&m,&q); -      for(inti =1; I <= N;i + +) Wu     { -         for(intj =1; J <= M;j + +) About        { $scanf"%1d",&mp[i][j]); -        } -     } -      for(inti =1; I <= N;i + +) A          for(intj =1; J <= M;j + +) +         { the solve (i,j); -         } $     intLx,ly,rx,ry; the      while(q--) the     { thescanf"%d %d%d%d",&lx,&ly,&rx,&ry); theprintf"%d\n", Ans[lx][ly][rx][ry]); -     } in return 0; the}
View Code

Codeforces 372B counting rectangles is fun

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.