Luo Gu P1950 rectangular _NOI Guide 2009 increase (2)

Face questions

 

As if this title is the second time to do it?

Last do not know how O (N ^ 3) cards in the past, really Niubi. . . .

This is done decent O (N ^ 2).

We considered only in the lower right corner of a rectangular statistics to it, and then draw a drawing can be found at a number of points for the lower right corner of the rectangle, equal to the weight of each point of this line of value can be seen as extending up to point up number, then monotonically composed of these weights (rising) stack Σh [s [i]] (s [i] -s [i-1]) (i.e. monotone pattern area drawn to stack two-dimensional plane) since this value is just the number of all the left end points can be taken to the location.

 

Um, and then run directly monotonically like stack maintenance, O (N ^ 2)

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1005;

int h[N],n,m,now,s[N],tp;
char ch;
ll ans;

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;tp=now=0,i++)
	    for(int j=1;j<=m;j++){
	    	ch=getchar();
	    	while(ch!='.'&&ch!='*') ch=getchar();
	    	h[j]=ch=='.'?h[j]+1:0;
	    	
	    	while(tp&&h[j]<=h[s[tp]]) now-=h[s[tp]]*(s[tp]-s[tp-1]),tp--;
	    	s[++tp]=j,now+=h[j]*(s[tp]-s[tp-1]); 
	    	
	    	ans+=(ll)now;
		}
	printf("%lld\n",ans);
	return 0;
}

  

Guess you like

Origin www.cnblogs.com/JYYHH/p/11297437.html
Recommended