2016 ACM Amman Collegiate Programming Contest D Rectangles
5 seconds
256 megabytes
standard input
standard output
Given an R×C grid with each cell containing an integer, find the number of subrectangles in this grid that contain only one distinct integer; this means every cell in a subrectangle contains the same integer.
A subrectangle is defined by two cells: the top left cell (r1, c1), and the bottom-right cell (r2, c2) (1 ≤ r1 ≤ r2 ≤ R) (1 ≤ c1 ≤ c2 ≤ C), assuming that rows are numbered from top to bottom and columns are numbered from left to right.
The first line of input contains a single integer T, the number of test cases.
The first line of each test case contains two integers R and C (1 ≤ R, C ≤ 1000), the number of rows and the number of columns of the grid, respectively.
Each of the next R lines contains C integers between 1 and 109, representing the values in the row.
For each test case, print the answer on a single line.
1
3 3
3 3 1
3 3 1
2 2 5
16
分析:按行处理,对每个点找到向上和向左的的最大矩形;
然后对于左边小矩形,直接加上之前答案即可;
向上向左预处理后RMQ+二分找最大矩形即可,复杂度O(n2logn);
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
#define intxt freopen("in.txt","r",stdin)
const int maxn=1e3+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,a[maxn][maxn],le[maxn][maxn],up[maxn][maxn],ans[maxn],mi[][maxn],p[maxn];
void init(int now)
{
for(int i=;i<=m;i++)mi[][i]=up[now][i];
for(int i=;i<=;i++)
for(int j=;j+(<<i)-<=m;j++)
mi[i][j]=min(mi[i-][j],mi[i-][j+(<<(i-))]);
}
int query(int l,int r)
{
int x=p[r-l+];
return min(mi[x][l],mi[x][r-(<<x)+]);
}
int main()
{
int i,j;
rep(i,,)p[i]=+p[i>>];
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
rep(i,,n)rep(j,,m)a[i][j]=read();
rep(i,,n)rep(j,,m)
{
up[i][j]=le[i][j]=;
if(a[i][j]==a[i-][j])up[i][j]=up[i-][j]+;
if(a[i][j]==a[i][j-])le[i][j]=le[i][j-]+;
}
ll ret=;
rep(i,,n)
{
init(i);
memset(ans,,sizeof(ans));
rep(j,,m)
{
int l=j-le[i][j]+,r=j,now_ans;
while(l<=r)
{
int mid=l+r>>;
if(query(mid,j)>=up[i][j])now_ans=mid,r=mid-;
else l=mid+;
}
ans[j]+=up[i][j]*(j-now_ans+);
if(now_ans>j-le[i][j]+)ans[j]+=ans[now_ans-];
}
rep(j,,m)ret+=ans[j];
}
printf("%lld\n",ret);
}
//system("Pause");
return ;
}
2016 ACM Amman Collegiate Programming Contest D Rectangles的更多相关文章
- Codeforces 2016 ACM Amman Collegiate Programming Contest B. The Little Match Girl(贪心)
传送门 Description Using at most 7 matchsticks, you can draw any of the 10 digits as in the following p ...
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...
- Gym 101102A Coins -- 2016 ACM Amman Collegiate Programming Contest(01背包变形)
A - Coins Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Descript ...
- 18春季训练01-3/11 2015 ACM Amman Collegiate Programming Contest
Solved A Gym 100712A Who Is The Winner Solved B Gym 100712B Rock-Paper-Scissors Solved C Gym 100712C ...
- ACM Amman Collegiate Programming Contest(7.22随机组队娱乐赛)
题目链接 https://vjudge.net/contest/240074#overview 只写一下自己做的几个题吧 /* D n^2的暴力dp怎么搞都可以的 这里先预处理 i到j的串时候合法 转 ...
- 2015 ACM Amman Collegiate Programming Contest 题解
[题目链接] A - Who Is The Winner 模拟. #include <bits/stdc++.h> using namespace std; int T; int n; s ...
- 2017 ACM Amman Collegiate Programming Contest 题解
[题目链接] A - Watching TV 模拟.统计一下哪个数字最多即可. #include <bits/stdc++.h> using namespace std; const in ...
- 2017 ACM Amman Collegiate Programming Contest
A - Watching TV /* 题意:求出出现次数最多的数字 */ #include <cstdio> #include <algorithm> #include < ...
- gym100712 ACM Amman Collegiate Programming Contest
非常水的手速赛,大部分题都是没有算法的.巨慢手速,老年思维.2个小时的时候看了下榜,和正常人差了3题(,最后还没写完跑去吃饭了.. A 水 Sort 比大小 /** @Date : 2017-09-0 ...
随机推荐
- mosquitto awareness when before it's being compiling, and you do settings and testing
Mostly, this clearify the usage of ' mosquitto.conf ' in easy-understanding language. 1. compile ...
- django admin.py settings 操作
dango, 怎么说呢,什么东西都内置了,什么东西都是自己的东西.用过flask, cherrypy, web.py, pyramid 等等python 框架后,再选用dango 觉得,理念有很大的区 ...
- linux c编程获得当前进程的进程名和执行路径
如何得到当前进程的进程名和执行路径.写了个程序分享一下. [sam@hzhsan test]$ more test_processname.cpp #include <limits.h> ...
- are both mapped to the url-pattern 错误解决方法
今天运行tomcat的时候出现报了一大波错误,下面我截取了部分错误信息: 严重:A child container failed during start java.util.concurrent.E ...
- 高频交易算法研发心得--WAVT指标(Warensoft交易量趋势指标)算法及应用
高频交易算法研发心得--WAVT指标(Warensoft交易量趋势指标)算法及应用 注:WAVT指标由Warensoft(王宇)原创. 前面聊了一系列的常见应用指标,包括短线.长线的指标,并且也无耐的 ...
- 关于Ueditor 前后端分离实现文件上传到独立服务器的问题 望大神们赐教
最近,由于网站实现多台服务器负载均衡,导致编辑器上传文件需要同步,可是使用同步软件太慢,不太现实,所以想到实现编辑器上传文件直接上传到独立文件服务器.可是没想到遇到坑了. 1.在本地IIS 中添加网站 ...
- C++ 头文件系列(stack)
简介 该头文件只含有一个类模版stack, 它实现栈的概念,是一个容器适配器(说实话,在写这篇随笔之前我都不知道有这么个类模版). 栈 栈只有一个重要的特性: LIFO(last-in first-o ...
- Web VLC 设置 tcp 协议播放
前言 web 端播放rtsp 流,一般都是采用vlc插件,默认是用 UDP 协议播放,这就会存在丢包的可能性,导致界面会变花,要想不花,需要使用更可靠的TCP协议.关于这两种协议的区别,大家可以自行查 ...
- 文档在线预览开源实现方案二:OpenOffice + pdf.js
文档在线预览的另一种实现方式采用的技术栈是OpenOffice + pdf.js, office文档转换为pdf的工作依然由OpenOffice的服务来完成,pdf生成后我们不再将其转换为其他文件而是 ...
- noip 2016 提高组总结(不是题解)
小弱鸡杨树辰是第一次参加像noip这样的高大上的比赛,于是他非常,非常,非常激动. 当他第二天考完试后,他正在yy自己的分数:day1T1应该是a掉了,T2写了个30分的暴力,T3也是个40分的暴力, ...