Coj 2105 Submatrix

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.