首页 > 代码库 > 【BZOJ2957】楼房重建 分块水题

【BZOJ2957】楼房重建 分块水题

#include <stdio.h>
int main()
{
	puts("转载请注明出处谢谢");
	puts("http://blog.csdn.net/vmurder/article/details/43406043");
}


题解:

分块水题。

不懂看代码:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 101000
#define P 2050
#define eps 1e-10
#define max(a,b) ((a)>(b)?(a):(b))
using namespace std;
int n,m;
double h[N];
int belong[N],size,blocks;
struct Block
{
	double stk[P];
	int top,begin,end;
	void keep(double now=0.0)
	{
		top=0;
		for(int i=begin;i<=end;i++)
			if(h[i]>now)stk[++top]=now=h[i];
	}
	int ask(double now=0.0)
	{
		int pos=(upper_bound(stk+1,stk+top+1,now+eps)-stk);
		return top-pos+1;
	}
}block[P];
inline int ask(double now=0.0)
{
	int ans=0,i;
	for(i=1;i<=blocks;i++)
	{
		ans+=block[i].ask(now);
		now=max(now,block[i].stk[block[i].top]);
	}
	return ans;
}
int main()
{
	int i,a,b;
	scanf("%d%d",&n,&m);
	size=sqrt(n);
	for(i=1;i<=n;i++)
	{
		belong[i]=(i-1)/size+1;
		if(!block[belong[i]].begin)block[belong[i]].begin=i;
		block[belong[i]].end=i;
	}
	blocks=belong[n];
	while(m--)
	{
		scanf("%d%d",&a,&b);
		h[a]=(double)b/a;
		block[belong[a]].keep();
		printf("%d\n",ask());
	}
	return 0;
}


【BZOJ2957】楼房重建 分块水题