poj 1769 Minimizing maximizer 单点更新线段树

//poj 1769
//sep9
#include 
using namespace std;
const int MAXN=100012;
const int MAXX=9999999;
int n,m;
int minv[MAXN*4];

void build(int l,int r,int k)  
{  
    minv[k]=MAXX;
	if(l==r) 
        return ;  
    int m=(l+r)/2; 
    build(l,m,2*k);  
    build(m+1,r,2*k+1);    
} 

void update(int i,int v,int k,int l,int r)
{
	if(minv[k]>v) minv[k]=v;
	if(l==r){
		return ;
	}
	int m=(l+r)/2;;
	if(i<=m)
		update(i,v,2*k,l,m);
	else
		update(i,v,2*k+1,m+1,r);
}

int query(int from,int to,int k,int l,int r)
{
	if(from<=l&&r<=to)
		return minv[k];
	int m=(l+r)/2;;
	if(to<=m)
		return query(from,to,2*k,l,m);
	else if(from>m)
		return query(from,to,2*k+1,m+1,r);
	else 	
		return min(query(from,to,2*k,l,m),query(from,to,2*k+1,m+1,r));
}


int main()
{
	scanf("%d%d",&n,&m);
	build(1,n,1);
	update(1,0,1,1,n);
	while(m--){
		int s,t;
		scanf("%d%d",&s,&t);
		if(s>=t) continue;
		int end=n;//n = t-1,t,...n all acceptable 
		int v=query(s,end,1,1,n);
		update(t,v+1,1,1,n);		
	}
	printf("%d\n",query(n,n,1,1,n));		
}

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