【POJ - 2019】Cornfields(二维st表,模板)
題干:
FJ has decided to grow his own corn hybrid in order to help the cows make the best possible milk. To that end, he's looking to build the cornfield on the flattest piece of land he can find.?
FJ has, at great expense, surveyed his square farm of N x N hectares (1 <= N <= 250). Each hectare has an integer elevation (0 <= elevation <= 250) associated with it.?
FJ will present your program with the elevations and a set of K (1 <= K <= 100,000) queries of the form "in this B x B submatrix, what is the maximum and minimum elevation?". The integer B (1 <= B <= N) is the size of one edge of the square cornfield and is a constant for every inquiry. Help FJ find the best place to put his cornfield.?
Input
* Line 1: Three space-separated integers: N, B, and K.?
* Lines 2..N+1: Each line contains N space-separated integers. Line 2 represents row 1; line 3 represents row 2, etc. The first integer on each line represents column 1; the second integer represents column 2; etc.?
* Lines N+2..N+K+1: Each line contains two space-separated integers representing a query. The first integer is the top row of the query; the second integer is the left column of the query. The integers are in the range 1..N-B+1.?
Output
* Lines 1..K: A single integer per line representing the difference between the max and the min in each query.?
Sample Input
5 3 1 5 1 2 6 3 1 3 5 2 7 7 2 4 6 1 9 9 8 6 5 0 6 9 3 9 1 2Sample Output
5題目大意:
給出一個N*N?(N<=250)的方陣,以及K(<=100000)個詢問。每次詢問如下:以(Xi,Yi)為左上角,邊長為B的子方陣中,最大值和最小值的差是多少?注意對于所有的詢問,B都是一個定值。
Input
第一行N,B(<=N),K。含義如上。
接下來N行N列的一個矩陣,每個數<=250。
接下來K行表示詢問,每行兩個數Xi, Yi?表示詢問的方陣的左上角。
解題報告:
首先這題做法賊雞兒多,因為是練模板嘛所以我就選了不是最快的方式。最快的方式好像是單調隊列,也能做到回答O1查詢。
注意不能先枚舉x再枚舉j、、我真是個麻瓜、、倍增這里的問題犯過一次了。(我記得是LCA的時候應該先dfs進去然后再處理而我是先處理了然后再dfs子樹的)
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 300 + 5; int Log[MAX]; int F[MAX][MAX][22],f[MAX][MAX][22],a[MAX][MAX]; int n,B,K; PII Min_Max(int x,int y,int z,int q) {int b[4] = {x,y,z,q};sort(b,b+4);return pm(b[0],b[3]); } void ST() {for(int j = 1; (1<<j) <=n; j++) {for(int x = 1; x<=n; x++) {int t = 1<<j-1;for(int y = 1; y+(1<<j) - 1 <= n; y++) {F[x][y][j] = Min_Max(F[x][y][j-1],F[x][y+t][j-1],F[x+t][y][j-1],F[x+t][y+t][j-1]).SS;f[x][y][j] = Min_Max(f[x][y][j-1],f[x][y+t][j-1],f[x+t][y][j-1],f[x+t][y+t][j-1]).FF;}}} } int solve(int x,int y) {int k = (int)(log(B)/log(2));//Log[B];int mx = Min_Max(F[x][y][k],F[x+B-(1<<k)][y][k],F[x][y+B-(1<<k)][k],F[x+B-(1<<k)][y+B-(1<<k)][k]).SS;int mn = Min_Max(f[x][y][k],f[x+B-(1<<k)][y][k],f[x][y+B-(1<<k)][k],f[x+B-(1<<k)][y+B-(1<<k)][k]).FF;return mx - mn; } int main() {cin>>n>>B>>K;for(int i = 2; i<=n; i++) Log[i] = Log[i>>1] + 1;for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) scanf("%d",&a[i][j]),F[i][j][0]=f[i][j][0]=a[i][j]; } ST();int x,y;while(K--) {scanf("%d%d",&x,&y);printf("%d\n",solve(x,y));}return 0 ; } /* 3 2 10 1 2 3 4 5 5 7 5 5 1 1 1 2 2 1 2 2*/?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的【POJ - 2019】Cornfields(二维st表,模板)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信用卡审核未通过怎么办 再次申请先找准被
- 下一篇: 两大房贷新规1月1日实施,对个人房贷影响