hdu 5749 Colmerauer
题意:对于给定的$n \times m$矩阵$M$,定义$S(a,b)$为$M$的所有$a \times b$子矩阵的权重之和。一个矩阵的权重是指矩阵中所有马鞍点权值之和,在一个矩阵中某点是马鞍点当且仅当它在所在行是唯一一个最小的,同时在所在列中是唯一一个最大的。现在输入矩阵$M$,要求计算$W= \sum\sum{abS(a,b)}, 1 \leq a \leq n, 1 \leq b \leq m$。数据范围$1 \leq n, m \leq 1000, 0 \leq M(i, j) \leq 1000000$。
分析: 考虑每个马鞍点对答案$W$的贡献,答案可以重写为每个马鞍点的权值与其所在子矩阵面积的和的积的和。考虑位于$i$行$j$列的点,我们设在$j$列从该点出发向上连续的$y_1$个点中除了点$(i, j)$其余点对应的权值均严格小于该点权值,并假设$y_1$是最大的。记$y1$为该点的最大向上关联长度,同理我们可以得到向下,向左,向右的最大关联长度,显然这些长度至少为$1$,按照左右上下的顺序得到关于$(i, j)$的四元组$(x_1, x_2, y_1, y_2)$,把四个方向对应到坐标轴方向,该点对应到原点,可以得到四个所谓的象限。那么如果该点在某个子矩阵内为马鞍点,必然有该子矩阵左上顶点在第二象限,右下顶点在第四象限(可以通过画图很直观得看出来)。那么所有子矩阵的面积之和为$g(i, j) = \sum\sum\sum\sum{(p + q + 1)(r + s + 1)}$其中$p, q, r, s$分别独立地在$[0, x_1), [0, x_2), [0, y_1), [0, y_2)$中取值,那么不难将其展开得到关于$x_1, x_2, y_1, y_2$的代数表达式。那么最终答案应该是$\sum\sum{g(i, j)w(i, j)}$,$w$是权值函数。于是现在只需要对每个点计算其四元组,可以采用rmq+二分的方法(尽管这种做法并非最优)。这样我们算法的复杂度就是$O(n^2log(n))$。取模使用unsigned int自然溢出就可以了。代码如下:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <ctime>
#include <cmath>
#include <iostream>
#include <assert.h>
#define PI acos(-1.)
#pragma comment(linker, "/STACK:102400000,102400000")
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define mp std :: make_pair
#define st first
#define nd second
#define keyn (root->ch[1]->ch[0])
#define lson (u << 1)
#define rson (u << 1 | 1)
#define pii std :: pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define type(x) __typeof(x.begin())
#define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
#define FOR(i, s, t) for(int i = (s); i <= (t); i++)
#define ROF(i, t, s) for(int i = (t); i >= (s); i--)
#define dbg(x) std::cout << x << std::endl
#define dbg2(x, y) std::cout << x << " " << y << std::endl
#define clr(x, i) memset(x, (i), sizeof(x))
#define maximize(x, y) x = max((x), (y))
#define minimize(x, y) x = min((x), (y))
using namespace std;
typedef long long ll;
const int int_inf = 0x3f3f3f3f;
const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
const int INT_INF = (int)((1ll << ) - );
const double double_inf = 1e30;
const double eps = 1e-;
typedef unsigned long long ul;
typedef unsigned int ui;
inline int readint(){
int x;
scanf("%d", &x);
return x;
}
inline int readstr(char *s){
scanf("%s", s);
return strlen(s);
}
//Here goes 2d geometry templates
struct Point{
double x, y;
Point(double x = , double y = ) : x(x), y(y) {}
};
typedef Point Vector;
Vector operator + (Vector A, Vector B){
return Vector(A.x + B.x, A.y + B.y);
}
Vector operator - (Point A, Point B){
return Vector(A.x - B.x, A.y - B.y);
}
Vector operator * (Vector A, double p){
return Vector(A.x * p, A.y * p);
}
Vector operator / (Vector A, double p){
return Vector(A.x / p, A.y / p);
}
bool operator < (const Point& a, const Point& b){
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
int dcmp(double x){
if(abs(x) < eps) return ;
return x < ? - : ;
}
bool operator == (const Point& a, const Point& b){
return dcmp(a.x - b.x) == && dcmp(a.y - b.y) == ;
}
double Dot(Vector A, Vector B){
return A.x * B.x + A.y * B.y;
}
double Len(Vector A){
return sqrt(Dot(A, A));
}
double Angle(Vector A, Vector B){
return acos(Dot(A, B) / Len(A) / Len(B));
}
double Cross(Vector A, Vector B){
return A.x * B.y - A.y * B.x;
}
double Area2(Point A, Point B, Point C){
return Cross(B - A, C - A);
}
Vector Rotate(Vector A, double rad){
//rotate counterclockwise
return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
}
Vector Normal(Vector A){
double L = Len(A);
return Vector(-A.y / L, A.x / L);
}
void Normallize(Vector &A){
double L = Len(A);
A.x /= L, A.y /= L;
}
Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){
Vector u = P - Q;
double t = Cross(w, u) / Cross(v, w);
return P + v * t;
}
double DistanceToLine(Point P, Point A, Point B){
Vector v1 = B - A, v2 = P - A;
return abs(Cross(v1, v2)) / Len(v1);
}
double DistanceToSegment(Point P, Point A, Point B){
if(A == B) return Len(P - A);
Vector v1 = B - A, v2 = P - A, v3 = P - B;
if(dcmp(Dot(v1, v2)) < ) return Len(v2);
else if(dcmp(Dot(v1, v3)) > ) return Len(v3);
else return abs(Cross(v1, v2)) / Len(v1);
}
Point GetLineProjection(Point P, Point A, Point B){
Vector v = B - A;
return A + v * (Dot(v, P - A) / Dot(v, v));
}
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
//Line1:(a1, a2) Line2:(b1,b2)
double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
return dcmp(c1) * dcmp(c2) < && dcmp(c3) * dcmp(c4) < ;
}
bool OnSegment(Point p, Point a1, Point a2){
return dcmp(Cross(a1 - p, a2 - p)) == && dcmp(Dot(a1 - p, a2 -p)) < ;
}
Vector GetBisector(Vector v, Vector w){
Normallize(v), Normallize(w);
return Vector((v.x + w.x) / , (v.y + w.y) / );
} bool OnLine(Point p, Point a1, Point a2){
Vector v1 = p - a1, v2 = a2 - a1;
double tem = Cross(v1, v2);
return dcmp(tem) == ;
}
struct Line{
Point p;
Vector v;
Point point(double t){
return Point(p.x + t * v.x, p.y + t * v.y);
}
Line(Point p, Vector v) : p(p), v(v) {}
};
struct Circle{
Point c;
double r;
Circle(Point c, double r) : c(c), r(r) {}
Circle(int x, int y, int _r){
c = Point(x, y);
r = _r;
}
Point point(double a){
return Point(c.x + cos(a) * r, c.y + sin(a) * r);
}
};
int GetLineCircleIntersection(Line L, Circle C, double &t1, double& t2, std :: vector<Point>& sol){
double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
double e = a * a + c * c, f = * (a * b + c * d), g = b * b + d * d - C.r * C.r;
double delta = f * f - * e * g;
if(dcmp(delta) < ) return ;
if(dcmp(delta) == ){
t1 = t2 = -f / ( * e); sol.pb(L.point(t1));
return ;
}
t1 = (-f - sqrt(delta)) / ( * e); sol.pb(L.point(t1));
t2 = (-f + sqrt(delta)) / ( * e); sol.pb(L.point(t2));
return ;
}
double angle(Vector v){
return atan2(v.y, v.x);
//(-pi, pi]
}
int GetCircleCircleIntersection(Circle C1, Circle C2, std :: vector<Point>& sol){
double d = Len(C1.c - C2.c);
if(dcmp(d) == ){
if(dcmp(C1.r - C2.r) == ) return -; //two circle duplicates
return ; //two circles share identical center
}
if(dcmp(C1.r + C2.r - d) < ) return ; //too close
if(dcmp(abs(C1.r - C2.r) - d) > ) return ; //too far away
double a = angle(C2.c - C1.c); // angle of vector(C1, C2)
double da = acos((C1.r * C1.r + d * d - C2.r * C2.r) / ( * C1.r * d));
Point p1 = C1.point(a - da), p2 = C1.point(a + da);
sol.pb(p1);
if(p1 == p2) return ;
sol.pb(p2);
return ;
}
int GetPointCircleTangents(Point p, Circle C, Vector* v){
Vector u = C.c - p;
double dist = Len(u);
if(dist < C.r) return ;//p is inside the circle, no tangents
else if(dcmp(dist - C.r) == ){
// p is on the circles, one tangent only
v[] = Rotate(u, PI / );
return ;
}else{
double ang = asin(C.r / dist);
v[] = Rotate(u, -ang);
v[] = Rotate(u, +ang);
return ;
}
}
int GetCircleCircleTangents(Circle A, Circle B, Point* a, Point* b){
//a[i] store point of tangency on Circle A of tangent i
//b[i] store point of tangency on Circle B of tangent i
//six conditions is in consideration
int cnt = ;
if(A.r < B.r) { std :: swap(A, B); std :: swap(a, b); }
int d2 = (A.c.x - B.c.x) * (A.c.x - B.c.x) + (A.c.y - B.c.y) * (A.c.y - B.c.y);
int rdiff = A.r - B.r;
int rsum = A.r + B.r;
if(d2 < rdiff * rdiff) return ; // one circle is inside the other
double base = atan2(B.c.y - A.c.y, B.c.x - A.c.x);
if(d2 == && A.r == B.r) return -; // two circle duplicates
if(d2 == rdiff * rdiff){ // internal tangency
a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++;
return ;
}
double ang = acos((A.r - B.r) / sqrt(d2));
a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang);
a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang);
if(d2 == rsum * rsum){
//one internal tangent
a[cnt] = A.point(base);
b[cnt++] = B.point(base + PI);
}else if(d2 > rsum * rsum){
//two internal tangents
double ang = acos((A.r + B.r) / sqrt(d2));
a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang + PI);
a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang + PI);
}
return cnt;
}
Point ReadPoint(){
double x, y;
scanf("%lf%lf", &x, &y);
return Point(x, y);
}
Circle ReadCircle(){
double x, y, r;
scanf("%lf%lf%lf", &x, &y, &r);
return Circle(x, y, r);
}
//Here goes 3d geometry templates
struct Point3{
double x, y, z;
Point3(double x = , double y = , double z = ) : x(x), y(y), z(z) {}
};
typedef Point3 Vector3;
Vector3 operator + (Vector3 A, Vector3 B){
return Vector3(A.x + B.x, A.y + B.y, A.z + B.z);
}
Vector3 operator - (Vector3 A, Vector3 B){
return Vector3(A.x - B.x, A.y - B.y, A.z - B.z);
}
Vector3 operator * (Vector3 A, double p){
return Vector3(A.x * p, A.y * p, A.z * p);
}
Vector3 operator / (Vector3 A, double p){
return Vector3(A.x / p, A.y / p, A.z / p);
}
double Dot3(Vector3 A, Vector3 B){
return A.x * B.x + A.y * B.y + A.z * B.z;
}
double Len3(Vector3 A){
return sqrt(Dot3(A, A));
}
double Angle3(Vector3 A, Vector3 B){
return acos(Dot3(A, B) / Len3(A) / Len3(B));
}
double DistanceToPlane(const Point3& p, const Point3 &p0, const Vector3& n){
return abs(Dot3(p - p0, n));
}
Point3 GetPlaneProjection(const Point3 &p, const Point3 &p0, const Vector3 &n){
return p - n * Dot3(p - p0, n);
}
Point3 GetLinePlaneIntersection(Point3 p1, Point3 p2, Point3 p0, Vector3 n){
Vector3 v = p2 - p1;
double t = (Dot3(n, p0 - p1) / Dot3(n, p2 - p1));
return p1 + v * t;//if t in range [0, 1], intersection on segment
}
Vector3 Cross(Vector3 A, Vector3 B){
return Vector3(A.y * B.z - A.z * B.y, A.z * B.x - A.x * B.z, A.x * B.y - A.y * B.x);
}
double Area3(Point3 A, Point3 B, Point3 C){
return Len3(Cross(B - A, C - A));
}
class cmpt{
public:
bool operator () (const int &x, const int &y) const{
return x > y;
}
}; int Rand(int x, int o){
//if o set, return [1, x], else return [0, x - 1]
if(!x) return ;
int tem = (int)((double)rand() / RAND_MAX * x) % x;
return o ? tem + : tem;
}
void data_gen(){
srand(time());
freopen("in.txt", "w", stdout);
int kases = ;
printf("%d\n", kases);
while(kases--){
int sz = 2e4;
int m = 1e5;
printf("%d %d\n", sz, m);
FOR(i, , sz) printf("%d ", Rand(, ));
printf("\n");
FOR(i, , sz) printf("%d ", Rand(1e9, ));
printf("\n");
FOR(i, , m){
int l = Rand(sz, );
int r = Rand(sz, );
int c = Rand(1e9, );
printf("%d %d %d %d\n", l, r, c, Rand(, ));
}
}
} struct cmpx{
bool operator () (int x, int y) { return x > y; }
};
const int maxn = 1e3 + ;
ui mt[maxn][maxn];
ui x1[maxn][maxn], x2[maxn][maxn], y1[maxn][maxn], y2[maxn][maxn];
int n, m;
ui bg[maxn][];
ui query(int l, int r, int o){
int len = (r - l + );
int i = ;
while(( << i) <= len) ++i;
--i;
int sp = r - ( << i) + ;
if(o) return max(bg[l][i], bg[sp][i]);
else return min(bg[l][i], bg[sp][i]);
}
int main(){
//data_gen(); return 0;
//C(); return 0;
int debug = ;
if(debug) freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int T = readint();
while(T--){
scanf("%d%d", &n, &m);
FOR(i, , n) FOR(j, , m) scanf("%u", &mt[i][j]);
FOR(i, , n) FOR(j, , m) x1[i][j] = x2[i][j] = y1[i][j] = y2[i][j] = ;
FOR(i, , n){
FOR(j, , m) bg[j][] = mt[i][j];
for(int i = ; ( << i) <= m; i++){
int len = << i;
for(int j = ; j + len - <= m; j++) bg[j][i] = min(bg[j][i - ], bg[j + len / ][i - ]);
}
FOR(j, , m){
if(j == || mt[i][j] >= mt[i][j - ]) continue;
int l = , r = j - ;
while(r - l > ){
int mid = (l + r) >> ;
ui tem = query(mid, r, );
if(tem > mt[i][j]) r = mid;
else l = mid;
}
x1[i][j] = j - r + ;
}
FOR(j, , m){
if(j == m || mt[i][j] >= mt[i][j + ]) continue;
int l = j + , r = m + ;
while(r - l > ){
int mid = (l + r) >> ;
ui tem = query(l, mid, );
if(tem > mt[i][j]) l = mid;
else r = mid;
}
x2[i][j] = l - j + ;
}
}
FOR(j, , m){
FOR(i, , n) bg[i][] = mt[i][j];
for(int i = ; ( << i) <= n; i++){
int len = << i;
for(int j = ; j + len - <= n; j++) bg[j][i] = max(bg[j][i - ], bg[j + len / ][i - ]);
}
FOR(i, , n){
if(i == || mt[i][j] <= mt[i - ][j]) continue;
int l = , r = i - ;
while(r - l > ){
int mid = (l + r) >> ;
ui tem = query(mid, r, );
if(tem < mt[i][j]) r = mid;
else l = mid;
}
y1[i][j] = i - r + ;
}
FOR(i, , n){
if(i == n || mt[i][j] <= mt[i + ][j]) continue;
int l = i + , r = n + ;
while(r - l > ){
int mid = (l + r) >> ;
ui tem = query(l, mid, );
if(tem < mt[i][j]) l = mid;
else r = mid;
}
y2[i][j] = l - i + ;
}
}
ui ans = ;
FOR(i, , n) FOR(j, , m){
ui X1 = x1[i][j] * (x1[i][j] - ) / ;
ui X2 = x2[i][j] * (x2[i][j] - ) / ;
ui Y1 = y1[i][j] * (y1[i][j] - ) / ;
ui Y2 = y2[i][j] * (y2[i][j] - ) / ;
ui _x1 = x1[i][j], _x2 = x2[i][j], _y1 = y1[i][j], _y2 = y2[i][j];
ui para = _x2 * _y2 * X1 * Y1 + X1 * Y2 * _x2 * _y1 + X2 * Y1 * _x1 * _y2 + X2 * Y2 * _x1 * _y1;
para += X1 * _x2 * _y1 * _y2 + X2 * _x1 * _y1 * _y2 + Y1 * _x1 * _x2 * _y2 + Y2 * _x1 * _x2 * _y1;
para += _x1 * _y1 * _y2 * _x2;
ans += para * mt[i][j];
}
printf("%u\n", ans);
}
return ;
}
code:
hdu 5749 Colmerauer的更多相关文章
- HDU 5749 Colmerauer 单调队列+暴力贡献
BestCoder Round #84 1003 分析:(先奉上zimpha巨官方题解) 感悟:看到题解单调队列,秒懂如何处理每个点的范围,但是题解的一句算贡献让我纠结半天 已知一个点的up,do ...
- HDU5479 Colmerauer 单调栈+暴力优化
http://acm.hdu.edu.cn/showproblem.php?pid=5749 思路: bestcoder 84 贡献:所有可能的子矩阵的面积和 //len1:子矩阵所有长的和 ;i&l ...
- hdu-5749 Colmerauer(单调栈)
题目链接: Colmerauer Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Oth ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
随机推荐
- Xstream(对象和xml转换)
package com.vcredit.framework.utils; import java.io.Writer; import org.apache.commons.lang3.StringUt ...
- 【iCore3 双核心板_FPGA】Quartus 如何生成jic文件
PDF下载: http://pan.baidu.com/s/1i5lQ0Rj iCore3 购买链接: https://item.taobao.com/item.htm?id=524229438677
- Python CRC16校验算法
def crc16(x, invert): a = 0xFFFF b = 0xA001 for byte in x: a ^= ord(byte) for i in range(8): last = ...
- 今天Apple证书更新,提供 "证书的签发者无效" 解决办法
首先 下载苹果新证书 developer.apple.com/certificationauthority/AppleWWDRCA.cer 然后在"钥匙串访问"中 "显 ...
- Android RSA加密解密
概述 RSA是目前最有影响力的公钥加密算法,该算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那时想要对其乘积进行因式分解却极其困 难,因此可以将乘积公开作为加密密钥,即公钥,而两个大素数 ...
- c#创建、安装、卸载、调试windows服务的简单事例
最近工作中用到了windows服务,对其有深刻理解和丰富经验谈不上,本篇文章只是简单陈诉用c#创建.安装.卸载.调试windows服务的步骤. 一.创建windows服务 1.用VS创建windows ...
- wireless tool 移植
在linux上调试wifi, 需要移植wireless tool进行验证,本文记录移植方法. 参考链接 http://www.cnblogs.com/zengjfgit/p/5601473.html ...
- Android ListView 子控件点击事件
android:descendantFocusability beforeDescendants:viewgroup会优先其子类控件而获取到焦点 afterDescendants:viewgroup只 ...
- mysql中engine=innodb和engine=myisam的区别
最开始用MySQL Administrator建数据库的时候,表缺省是InnoDB类型,也就没有在意.后来用Access2MySQL导数据的时候发现只能导成 MyISAM类型的表,不知道这两种类型有什 ...
- lua 环境揭秘
什么是环境? http://www.lua.org/manual/5.1/manual.html#2.9 Besides metatables, objects of types thread, fu ...