二维 ST POJ 2019
题目大意:给你一个n*n的矩阵,每次给你一个点(x,y),以其为左上角,宽度为b的矩阵中最小的数值和最大数值的差是多少? 一共k个询问。
思路:简单的二维st。
定义dp(i,j,k,L)表示以(i,j)为左上角,宽度为(2^k, 2^L)的区间内的最大(小)值。
//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
#include<iostream>
#include<utility>
#include<stdlib.h>
#include<time.h>
#include<cmath>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = + ;
int dp[maxn][maxn][][], dp2[maxn][maxn][][];
int n, b, k; void init_st(){
for (int i = ; ( << i) <= n; i++){
for (int j = ; ( << j) <= n; j++){
if (i == && j == ) continue;
for (int x = ; x + ( << i) - <= n; x++){
for (int y = ; y + ( << j) - <= n; y++){
if (i == ) {
dp[x][y][i][j] = min(dp[x][y][i][j - ], dp[x][y + ( << (j - ))][i][j - ]);
dp2[x][y][i][j] = max(dp2[x][y][i][j - ], dp2[x][y + ( << (j - ))][i][j - ]);
}
else {
dp[x][y][i][j] = min(dp[x][y][i - ][j], dp[x + ( << (i - ))][y][i - ][j]);
dp2[x][y][i][j] = max(dp2[x][y][i - ][j], dp2[x + ( << (i - ))][y][i - ][j]);
}
}
}
}
}
} int query(int x, int y, int X, int Y){
int maxi = , mini = 1e8;
int k1 = , k2 = ;
while ( << ( + k1) <= X - x) k1++;
while ( << ( + k2) <= Y - y) k2++;
maxi = max(maxi, max(dp2[x][y][k1][k2], dp2[X - ( << k1) + ][y][k1][k2]));
maxi = max(maxi, max(dp2[x][Y - ( << k2) + ][k1][k2], dp2[X - ( << k1) + ][Y - ( << k2) + ][k1][k2])); mini = min(mini, min(dp[x][y][k1][k2], dp[X - ( << k1) + ][y][k1][k2]));
mini = min(mini, min(dp[x][Y - ( << k2) + ][k1][k2], dp[X - ( << k1) + ][Y - ( << k2) + ][k1][k2]));
//printf("maxi = %d mini = %d\n", maxi, mini);
return maxi - mini;
} int main(){
//while (true){
cin >> n >> b >> k;
for (int i = ; i <= n; i++){
for (int j = ; j <= n; j++){
scanf("%d", &dp[i][j][][]);
dp2[i][j][][] = dp[i][j][][];
}
}
init_st();
for (int i = ; i <= k; i++){
int x, y; scanf("%d%d", &x, &y);
int ans = query(x, y, x + b - , y + b - );
printf("%d\n", ans);
}
//}
return ;
}
二维 ST POJ 2019的更多相关文章
- BZOJ3577:玩手机(最大流,二维ST表)
Description 现在有一堆手机放在坐标网格里面(坐标从1开始),坐标(i,j)的格子有s_(i,j)个手机. 玩手机当然需要有信号,不过这里的手机与基站与我们不太一样.基站分为两种:发送站和接 ...
- BZOJ1047[HAOI2007]理想的正方形——二维ST表
题目描述 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 输入 第一行为3个整数,分别表示a,b,n的值第二行至第a+1行每行为b个非 ...
- 【CodeForces】713 D. Animals and Puzzle 动态规划+二维ST表
[题目]D. Animals and Puzzle [题意]给定n*m的01矩阵,Q次询问某个子矩阵内的最大正方形全1子矩阵边长.n,m<=1000,Q<=10^6. [算法]动态规划DP ...
- 【洛谷 P2216】 [HAOI2007]理想的正方形(二维ST表)
题目链接 做出二维\(ST\)表,然后\(O(n^2)\)扫一遍就好了. #include <cstdio> #include <cstring> #include <a ...
- Codeforces 713D Animals and Puzzle(二维ST表+二分答案)
题目链接 Animals and Puzzle 题意 给出一个1e3 * 1e3的01矩阵,给出t个询问,每个询问形如x1,y1,x2,y2 你需要回答在以$(x1, y1)$为左上角,$(x1, ...
- [模板]二维ST表
考试yy二维ST表失败导致爆零. 其实和一维的ST表很像... 也是设$f[i][j][p][q]$为以$(i, j)$为左上角,长为$2^p$,宽为$2^q$的矩形的最大值. 算法流程是先把每一行都 ...
- [HNOI2007] 理想正方形 二维ST表
题目描述 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 输入输出格式 输入格式: 第一行为3个整数,分别表示a,b,n的值 第二行至 ...
- [总结] 二维ST表及其优化
二维 \(\mathcal{ST}\) 表,可以解决二维 \(\mathcal{RMQ}\) 问题.这里不能带修改,如果要修改,就需要二维线段树解决了. 上一道例题吧 ZOJ2859 类比一维 \(\ ...
- hdu2888 二维ST表(RMQ)
二维RMQ其实和一维差不太多,但是dp时要用四维 /* 二维rmq */ #include<iostream> #include<cstring> #include<cs ...
随机推荐
- MySQL字段自增自减的SQL语句
MySQL的自增语句大家应该都很熟悉 也很简单 update `info` set `comments` = `comments`+1 WHERE `id` = 32 这样就可以了,但是有时候我们会涉 ...
- ACdream 1020 The Game about KILL
找规律. 11 3 1 3 5 7 1 3 5 7 9 11 13 15 ....... #pragma comment(linker, "/STACK:1024000000,1024000 ...
- 你我公益模式系统APP开发
你我公益模式系统APP开发(微or电 158.1500.1390 小凡团队)你我公益系统开发,你我公益系统模式定制,你我公益系统开发软件,你我公益平台系统开发. 互联网世界无边无界,互联网创业者应敢于 ...
- 1-jQuery - AJAX load() 方法【基础篇】
jQuery load() 方法是简单但强大的 AJAX 方法:load() 方法从服务器加载数据,并把返回的数据放入被选元素中. 格式 $(selector).load(URL 源码 index.h ...
- JS复习:第十、十一章
第十章 NodeList是一种类数组对象,用于保存一组有序的节点,可以通过位置来访问这些节点,但它并不是Array实例,将其转化为数组的方法: function converToArray(nodes ...
- Introducing 'bind'
原文地址:http://fsharpforfunandprofit.com/posts/computation-expressions-bind/ 上一篇讨论了如何理解let作为一个能实现contin ...
- C#连接sqlserver数据库
// 混合登录 写法1:Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPas ...
- openwrt启动过程(脚本)
来源: http://wiki.openwrt.org/doc/techref/preinit_mount#first.boot 基本的openwrt启动顺序为: 1.boot loader loa ...
- openwrt 汉化
make menuconfig 添加luci LuCI--->Collections----- <*> luci 添加luci的中文语言包 LuCI--->Translatio ...
- ios 将彩色照片转化成黑白等几种类型
-(UIImage *)changeColoursImageTograyScaleImage:(UIImage *)anImage type:(int)type { CGImageRef imageR ...