BZOJ2957 线段树

#include
using namespace std;
typedef long long ll;
const int N=1e5+10;
struct SegmentTree{
	int l,r,ans;double val;
	#define l(x) Tree[x].l
	#define r(x) Tree[x].r
	#define ans(x) Tree[x].ans
	#define val(x) Tree[x].val
}Tree[N*4];
void build(int p,int l,int r){
	l(p)=l,r(p)=r;
	if(l==r)return;
	int mid=(l+r)/2;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
}
int Cal(int p,double val){
	if(l(p)==r(p))return val(p)>val;
	if(val(p*2)<=val)return Cal(p*2+1,val);
	else return ans(p)-ans(p*2)+Cal(p*2,val);
}
void change(int p,int x,double val){
	if(l(p)==r(p)){
		ans(p)=1;
		val(p)=val;
		return;
	}
	int mid=(l(p)+r(p))/2;
	if(x<=mid)change(p*2,x,val);
	else change(p*2+1,x,val);
	val(p)=max(val(p*2),val(p*2+1));
	ans(p)=ans(p*2)+Cal(p*2+1,val(p*2));
}
int main(){
	int n,m,x,y;
	cin>>n>>m;
	build(1,1,n);
	while(m--){
		cin>>x>>y;
		change(1,x,y*1.0/x);
		printf("%d\n",ans(1));
	}
}

 

你可能感兴趣的:(数据结构)