题意,给出一个n行m列的矩阵

里面元素是0或者1

给出q个询问

a,b,c,d

求(a,b)到(c,d)有多少个由0组成的矩形

我们定义

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTc3NTY5MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">即为求(a,b)到(c,d)有多少个由0组成的矩形

对一个矩形来说

dp[a][b][c][d]=dp[a][b][c][d-1]+dp[a][b][c-1][d]-dp[a][b][c-1][d-1]+包括右下角(当前点)的矩形;

重点就在包括右下角(当前点c,d)的矩形。怎样计算这个

我们能够暴力扫描。须要nm的复杂度。乘上原有复杂度

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTc3NTY5MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">,,,已经会超过时限

也能够用一个VECTOR来记录1的位置。对我们扫描到的行,二分查找G[该行]中1的位置(在第b列到第d列间第一个1),然后依据相对位置再来计算,最坏查找次复杂度为

也能够状压整张图

我们要知道的是b列到d列中1的情况,我们把代表该行状态的数s先左位移再右位移到仅仅剩b列到d列的状态

然后作运算s&(-s)返回第一个1的位置比方。假设s是0101000,则返回8

为什么能这样?来看下是怎么回事

还是以s=0101000为例

负数即正数的补码,-s即是1010111+1=1011000

0101000

1011000 &

0001000

可是这里

会返回2^40

可是没关系。我们对返回的数模997(经測验没有地址冲突)

这样就能够把它丢到表里

vis[2^1]=1

vis[2^2]=2

vis[2^3]=3

...

vis[2^40]=40

这样就能够得到当前行中b到d列里从右往左第一个1的相对于第d列的位置

算下复杂度,全是几个O(1)的操作,大致为O(1)

说下我认为最厉害最省事的方法

CF上最短size的代码

是这么做的

记录每一个格子距离上一个1的距离d[i][j]



在找包括右下角(当前点c,d)的矩形的时候,我们取adder=min(记录数d[i][d],adder)(adder一開始是d-b+1),每次加上这个adder。从c行到a行(d[c][d]~d[a][d]),一直维护这个adder

O(1)且很方便

#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_JUDGE
freopen("/home/rainto96/in.txt","r",stdin);
#endif int 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+数据维护]的更多相关文章

  1. Codeforces 372B Counting Rectangles is Fun:dp套dp

    题目链接:http://codeforces.com/problemset/problem/372/B 题意: 给你一个n*m的01矩阵(1 <= n,m <= 40). 然后有t组询问( ...

  2. Codeforces 372B Counting Rectangles is Fun

    http://codeforces.com/problemset/problem/372/B 题意:每次给出一个区间,求里面有多少个矩形 思路:预处理,sum[i][j][k][l]代表以k,l为右下 ...

  3. Codeforces Round #219 (Div. 2) D. Counting Rectangles is Fun 四维前缀和

    D. Counting Rectangles is Fun time limit per test 4 seconds memory limit per test 256 megabytes inpu ...

  4. Counting Rectangles

    Counting Rectangles Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1043 Accepted: 546 De ...

  5. Project Euler 85 :Counting rectangles 数长方形

    Counting rectangles By counting carefully it can be seen that a rectangular grid measuring 3 by 2 co ...

  6. UVA - 10574 Counting Rectangles

    Description Problem H Counting Rectangles Input: Standard Input Output:Standard Output Time Limit: 3 ...

  7. UVA 10574 - Counting Rectangles(枚举+计数)

    10574 - Counting Rectangles 题目链接 题意:给定一些点,求可以成几个边平行于坐标轴的矩形 思路:先把点按x排序,再按y排序.然后用O(n^2)的方法找出每条垂直x轴的边,保 ...

  8. Codeforces 372 B. Counting Rectangles is Fun

    $ >Codeforces \space 372 B.  Counting Rectangles is Fun<$ 题目大意 : 给出一个 \(n \times m\) 的 \(01\) ...

  9. BSA基础数据维护

    平台 BSA基础数据维护 .扇区五个字段的内容 本来值为0,经过107上计算解析,得出正常的数值.然后106上报(200050),得到回复(200051). 查看回复数据,是否有错误.比如提示104 ...

随机推荐

  1. [POI 2007] 堆积木

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1109 [算法] DP [代码] #include<bits/stdc++.h& ...

  2. 【POJ 2976】 Dropping Tests

    [题目链接] http://poj.org/problem?id=2976 [算法] 0/1分数规划 [代码] #include <algorithm> #include <bits ...

  3. hammer教程

    一.前言 移动端框架当前还处在初级阶段,但相对于移动端的应用来说已经有很长时间了.虽然暂时还没有PC端开发的需求量大,但移动端的Web必然是一种趋势,在接触移动端脚本的过程中,最开始想到的是juqer ...

  4. C - Twins(贪心)

    Problem description Imagine that you have a twin brother or sister. Having another person that looks ...

  5. Nginx介绍及知识点(摘抄)

    正向代理是把自己的网络环境切换成代理的网络 反向代理是代理机器返回给我要我的资源 本文借鉴参考于http://tengine.taobao.org/book/chapter_02.html. 属于纯干 ...

  6. 2.Ventuz Designer常用工具介绍

    Ventuz Designer常用工具介绍 1.  打开Ventuz Designer 图1.1 2.  Ventuz Designer第一个界面 图2.1 Recent Projects:最近创建的 ...

  7. java8 Stream 笔记

    stream的定义:对一个源中的一系列元素进行聚合操作. 一系列元素:stream对一组有特定类型的元素提供了一个接口.但是stream并不真正存储元素,元素根据需求被计算出来. 源:stream可以 ...

  8. vue-cli简介(中文翻译)

    vue-cli是一个简单的vuejs脚手架命令行工具. 安装 准备:Node.js(>=4.x,推荐6.x版本),npm版本3以上和Git. $npm install -g vue-cli 使用 ...

  9. SVD分解.潜语义分析.PythonCode

    原文链接:http://www.cnblogs.com/appler/archive/2012/02/02/2335886.html 原始英文链接:http://www.puffinwarellc.c ...

  10. highcharts例子

    直接看代码 <script language="JavaScript"> $(document).ready(function() { $.ajax({ type:'p ...