2018 ACM-ICPC南京赛区网络赛 B The writing on the wall

题目

单调栈真的是很可爱的东西!

应用也很多啊,应该多想想

#include<iostream>
#include<stdio.h> 
#include<algorithm>
#include<string.h>
#include<vector>
#include<set>
#include<math.h>
#include<queue>
#include<map>
#include<stack>
#define go(i,a,b) for (int (i)=(a);(i)<=(b);(i)++)
#define ll long long
#define N 1000005
#define M 105
using namespace std;
typedef pair<int,int> pai;
vector<int>v[M];

int h[M];
bool cmp(int a, int b){
	return a>b;
}
int main(){
	int T,n,m,k,x,y,ca=0;
	scanf("%d",&T);
	while (T--){
		scanf("%d%d%d",&n,&m,&k);
		go(i,1,m) v[i].clear();
		go(i,1,k){
			scanf("%d%d",&x,&y);
			v[y].push_back(x);
		}
		go(i,1,m) sort(v[i].begin(),v[i].end(),cmp);
		memset(h,0,sizeof(h));
		ll res=0,ans=0;
		go(i,1,n){
			stack<pai>s; //前面的是深度,然后是位置。 
			s.push(make_pair(0,0));
			ans=0;
			 go(j,1,m){
			 	if (!v[j].empty()&&v[j].back()==i){
			 		v[j].pop_back();
			 		h[j]=0;
			 		while (!s.empty()) s.pop();
			 		s.push(make_pair(0,j));
			 		ans=0;
				 }
				 else{
				 	h[j]++;
				 	while(!s.empty()&&s.top().first>=h[j]){
				 		pai tmp=s.top();
				 		s.pop();
				 		ans-=(tmp.second-s.top().second)*tmp.first;
					 }
					 ans+=(j-s.top().second)*h[j];
					 //cout<<ans<<endl;
					 res+=ans;
					 s.push(make_pair(h[j],j));
				 }
			 }
		}
		printf("Case #%d: %lld\n",++ca,res);
	} 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值