数据结构训练之二

https://www.luogu.org/problem/P4198

动态维护斜率单调递增

代码很清楚,就不讲解了

code by wzxbeliever

#include<bits/stdc++.h>
#define ll long long
#define ri register int
#define il inline
#define lowbit(x) x&(-x)
using namespace std;
const int maxn=100005;
int n,m;
int num[maxn<<2];
double maxx[maxn<<2];
il double maxl(double a,double b){if(a>b)return a;return b;}
il int query(int rt,int l,int r,double x){
	int mid=l+r>>1;
	if(maxx[rt]>x)return 0;
	if(l==r)return maxx[rt]>x;
	else if(maxx[rt<<1]<=x)return query(rt<<1|1,mid+1,r,x);
	return query(rt<<1,l,mid,x)+num[rt]-num[rt<<1];
}
il void upd(int rt,int l,int r,double x,int pos){
	if(l==r&&l==pos){
		num[rt]=1;maxx[rt]=x;return;
	}
	int mid=l+r>>1;
	if(pos<=mid)upd(rt<<1,l,mid,x,pos);
	else upd(rt<<1|1,mid+1,r,x,pos);
	maxx[rt]=maxl(maxx[rt<<1],maxx[rt<<1|1]);
	num[rt]=num[rt<<1]+query(rt<<1|1,mid+1,r,maxx[rt<<1]);
}
int main(){
    scanf("%d%d",&n,&m);
    for(ri i=1,l,r;i<=m;i++)scanf("%d%d",&l,&r),upd(1,1,n,(double)r/l,l),printf("%d\n",num[1]);
	return 0;
}

posted @ 2019-10-10 21:12  wzx_believer  阅读(81)  评论(0编辑  收藏  举报