Cf 372b counting rectangles is fun [DP + Data Maintenance]

Source: Internet
Author: User

Returns a matrix of N rows and M columns.

The element is 0 or 1.

Q inquiries

A, B, c, d

Evaluate the number of rectangles (A, B) To (c, d) composed of 0.


We define

That is, how many rectangles (A, B) To (c, d) are composed of 0?

For a rectangle

DP [a] [B] [C] [d] = DP [a] [B] [C] [D-1] + dp [a] [B] [C-1] [d ]-DP [a] [B] [C-1] [D-1] + contains the lower right corner (current point);

The focus is onContains the rectangle (current point c, d) in the lower right corner, How to calculate this


We can perform a brute-force scan. The complexity of the NM is multiplied by the complexity of the original, and the time limit has been exceeded.


We can also use a vector to record the position of 1 and scan the rows we have scanned, perform a binary search for the position 1 in G [this row] (first 1 between column B and column D), and then calculate the result based on the relative position. The worst search complexity is

You can also press the whole graph.


We need to know the situation where Column B is 1 in column D,We first shift the number of S representing the status of this row to the state where only column B is left to column D.

Then perform the operation S & (-S) to return the position of the first 1. For example, if S is 0101000, 8 is returned.

Why? Let's see what's going on.

Take S = 0101000 as an example.

A negative number is a positive complement, and-S is 1010111 + 1 = 1011000.

0101000

1011000 &

0001000

But here

Returns 2 ^ 40

But it does not matter,We return a digital value of 997.(No address conflict was detected)

In this way, you can throw it to the table.

Vis [2 ^ 1] = 1

Vis [2 ^ 2] = 2

Vis [2 ^ 3] = 3

...

Vis [2 ^ 40] = 40

In this way, we can obtain the position of the first 1 column from right to left in the column B to column D in the current row relative to column D.

Calculate the complexity, which is the number of O (1) operations, roughly O (1)


I think the most effective and easy-to-use method

Code of the shortest size on CF

Yes.

Record the distance from each grid to the previous 1. d [I] [J]


SearchingContains the rectangle (current point c, d) in the lower right cornerWhen we add this adder, we take adder = min (number of records d [I] [d], adder) (adder starts with D-B + 1, from Row C to row A (d [C] [d] ~ D [a] [d]), always maintain this Adder

O (1) and very convenient

#include <cstdio>#include <cstring>#include <cmath>#include <iostream>#include <algorithm>using namespace std;int g[55][55];int num[55][55];int dp[55][55][55][55];int main(){#ifndef ONLINE_JUDGEfreopen("/home/rainto96/in.txt","r",stdin);#endifint n,m,q;cin>>n>>m>>q;for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){scanf("%1d",&g[i][j]);num[i][j]=num[i][j-1]+1;if(g[i][j]==1)num[i][j]=0;}for(int a=1;a<=n;a++)for(int b=1;b<=m;b++)for(int c=a;c<=n;c++)for(int d=b;d<=m;d++){int& now=dp[a][b][c][d]=dp[a][b][c][d-1]+dp[a][b][c-1][d]-dp[a][b][c-1][d-1];int add=d-b+1;for(int i=c;i>=a;i--){add=min(add,num[i][d]);now+=add;}}for(int i=1;i<=q;i++){int a,b,c,d;cin>>a>>b>>c>>d;cout<<dp[a][b][c][d]<<endl;}}


Cf 372b counting rectangles is fun [DP + Data Maintenance]

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.