| Submatrix |
| Difficulty level: A; programming language: Unlimited; run time limit: 2000ms; run space limit: 131072KB; code length limit: 102400B |
| Question Description |
| Small A has a nxm matrix in which 1~n*m (n*m) integers appear once. Now small a selects a sub-matrix within the matrix whose weights are equal to the minimum value of all the numbers in the sub-matrix. Little A would like to know if the weight of the sub-matrix he chose was I (1<=I<=NXM), then how many of the sub-matrices he chose might be? Little a wants to know the result of all possible I values, but these results are too many, he can't count, so he asks you for help. |
| Input |
First line, two integers n, M. The next n rows, m integers per line, represent the elements in the matrix. |
| Output |
| NXM lines, one integer per line, where the integer of line I represents the number of sub-matrices that he chooses if the sub-matrix weights for small A are selected as I. |
| Input example |
2 3 2 5 1 6 3 4 |
| Output example |
6 4 5 1 1 1 |
| Other Notes |
For 30% of data, 1<=n, m<=50; For all the data, 1<=n, m<=300. |
|
Solution:%%% Zheng, first consider a one-dimensional, with a monotonous stack maintenance can go to the left to the right, update the answer. For two-dimensional, the one-dimensional beat can be .... Remember to take the minimum value ...
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <algorithm>5#include <queue>6#include <cstring>7 #definePAU Putchar (")8 #defineENT Putchar (' \ n ')9 using namespacestd;Ten Const intmaxn= -+Ten, maxans=90000+Ten, inf=-1u>>1; One inta[maxn][maxn],mi[maxn],r[maxn],l[maxn],s[maxn],pos[maxn],p[maxans],n,m; AInlineintRead () { - intx=0, sig=1;CharCh=GetChar (); - for(;! IsDigit (CH); Ch=getchar ())if(ch=='-') sig=0; the for(; isdigit (ch); Ch=getchar ()) x=Ten*x+ch-'0'; - returnsig?x:-x; - } -InlinevoidWriteintx) { + if(x==0) {Putchar ('0');return;}if(x<0) Putchar ('-'), x=-x; - intlen=0, buf[ the]; while(x) buf[len++]=x%Ten, x/=Ten; + for(inti=len-1; i>=0; i--) Putchar (buf[i]+'0');return; A } at voidinit () { -N=read (); m=read (); - for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) a[i][j]=read (); - for(intI=1; i<=n;i++){ - for(intj=1; j<=m;j++) mi[j]=inf; - for(intj=i;j<=n;j++){ in for(intk=1; k<=m;k++) Mi[k]=min (Mi[k],a[j][k]);inttop=0; - for(intk=1; k<=m;k++) {//is to while(top&& (Mi[k]<s[top])) r[pos[top--]]=k-1; +s[++top]=mi[pos[top]=K]; -} while(top) r[pos[top--]]=m; the for(intk=m;k>=1; k--) {//Anti- * while(top&& (Mi[k]<s[top])) l[pos[top--]]=k+1; $s[++top]=mi[pos[top]=K];Panax Notoginseng} while(top) l[pos[top--]]=1; - for(intk=1; k<=m;k++) p[mi[k]]+= (r[k]-k+1) * (k-l[k]+1); the } +}intAll=n*m; for(intI=1; i<=all;i++) write (P[i]), ENT; A return; the } + voidWork () { - return; $ } $ voidprint () { - return; - } the intMain () {init (); work ();p rint ();return 0;}
Coj 2105 Submatrix