zoukankan      html  css  js  c++  java
  • P1110 [ZJOI2007]报表统计

    6个点ac

    #include<iostream>
    #include<algorithm>
    #include<cstdlib>
    #include<ctime>
    #include<string>
    using namespace std;
    struct node* nil;
    struct node
    {
    	int val;
    	int size;
    	int num;
    	int key;
    	node *ch[2];
    };
    void sum(node* &now)
    {
    	now->size=now->num+now->ch[0]->size+now->ch[1]->size;
    	return ;
    }
    void rotato(node* &now,int base)
    {
    	node* k=now->ch[base^1];
    	now->ch[base^1]=k->ch[base];
    	k->ch[base]=now;
    	sum(now);
    	sum(k);
    	now=k;
    }
    int cmp(node* &now,int value)
    {
    	if(now->val==value)
    		return -1;
    	return (now->val > value ? 0 : 1);
    }
    int cmpkth(node* &now,int kth)
    {
    	int s=now->ch[0]->size;
    	if(s<kth&&s+now->num<=kth)
    		return -1;
    	return (kth <= s ? 0 : 1 );
    }
    node* New(int val)
    {
    	node* res=new node;
    	res->ch[0]=res->ch[1]=nil;
    	res->size=res->num=1;
    	res->key=rand();
    	res->val=val;
    	return res;
    }
    void insert(node* &now,int val)
    {
    	if(now==nil)
    	{
    		now=New(val);
    		return ;
    	}
    	int d=cmp(now,val);
    	if(d==-1)
    	{
    		now->num+=1;
    		now->size+=1;
    		return ;
    	}
    	insert(now->ch[d],val);
    	if(now->key < now->ch[d]->key)
    		rotato(now,d^1);
    	sum(now);
    	return ;
    }
    void pre(node* &now,int val,int &ans)
    {
    	if(now->val<val)
    	{
    		ans=max(now->val,ans);
    		if(now->ch[1]!=nil)
    			pre(now->ch[1],val,ans);
    	}
    	if(now->val>=val&&now->ch[0]!=nil)
    		pre(now->ch[0],val,ans);
    }
    void nxt(node* &now,int val,int &ans)
    {
    	if(now->val>val)
    	{
    		ans=min(ans,now->val);
    		if(now->ch[0]!=nil)
    			nxt(now->ch[0],val,ans);
    	}
    	if(now->val<=val&&now->ch[1]!=nil)
    		nxt(now->ch[1],val,ans);
    }
    void del(node* &now,int val)
    {
    	if(now==nil)
    		return ;
    	int d=cmp(now,val);
    	if(d==-1)
    	{
    		if(now->num>1)
    		{
    			now->size-=1;
    			now->num-=1;	
    			return ;
    		}
    		node* u=now;
    		if(now->ch[0]!=nil&&now->ch[1]!=nil)
    		{
    			int d2=(now->ch[0]->key > now->ch[1]->key ? 0 : 1);
    			rotato(now,d2^1);
    			del(now->ch[d2^1],val);
    		}
    		else
    			if(now->ch[0]==nil)
    				now=now->ch[1];
    			else	
    				now=now->ch[0];
    		u=nil;
    	}
    	else
    		del(now->ch[d],val);
    	sum(now);
    }
    int Min(node* &now)
    {
    	if(now==nil)
    		return 0x7fffffff;
    	if(now->ch[0]==nil)
    		return now->val;
    	Min(now->ch[0]);
    }
    void visit(node* &now)
    {
    	if(now==nil)
    		return ;
    	visit(now->ch[0]);
    	cout<<now->val<<" ";
    	visit(now->ch[1]);
    }
    int find(node* &now,int value)
    {
    	if(now==nil)
    		return 0x7fffffff;
    	int d=cmp(now,value);
    	if(d==-1)
    		return now->ch[0]->size+1;
    	else
    		if(d==0)
    			return find(now->ch[0],value);
    		else
    			return now->ch[0]->size+now->num+find(now->ch[1],value);
    }
    int minn(node* &now)
    {
    	if(now==nil)
    		return 0x7ffffff;
    	if(now->ch[0]==nil)
    		return now->val;
    	minn(now->ch[0]);
    }
    node* root;
    node* root1;
    int beside[500010][2];
    void init()
    {
    	nil=new node;
    	root=root1=nil;
    	nil->ch[0]=nil->ch[1]=nil;
    	nil->key=nil->num=nil->size=nil->val=0;
    	return;
    }
    int main()
    {
    	init();
        cin.sync_with_stdio(false);
        srand(time(NULL));
        int n,m;
        cin>>n>>m;
        int a,b;
        int mod1=1e9,mod2=1e9;
        for(int i=1;i<=n;i++)
        {
        	cin>>a;
        	beside[i][0]=beside[i][1]=a;
        	insert(root,a);
        	if(i!=1)
        		insert(root1,abs(beside[i-1][1]-a));
        	int ha=1e9;
        	nxt(root,a,ha);
        	mod2=min(mod2,abs(ha-a));
        	ha=-1e9;
        	pre(root,a,ha);
        	mod2=min(mod2,abs(ha-a));
    	}
    	mod1=minn(root1);
    	string h;
    	bool falg=false;//huaji
    	for(int i=1;i<=m;i++)
    	{
    		cin>>h;
    		if(h=="INSERT")
    		{
    			cin>>a>>b;
    			if(find(root,b)>0)
    			{
    				falg=true;
    				mod2=0;
    			}
    			if(!falg)
    			{
    				insert(root,b);
    		    	int ha=1e9;
    		    	nxt(root,b,ha);
    		    	mod2=min(mod2,abs(ha-b));
    		    	ha=-1e9;
    		    	pre(root,b,ha);
    		    	mod2=min(mod2,abs(ha-b));
    			}
    			del(root1,abs(beside[a][1]-beside[a+1][0]));
    			insert(root1,abs(beside[a][1]-b));
    			beside[a][1]=b;
    			insert(root1,abs(beside[a][1]-beside[a+1][0]));
    			mod1=minn(root1);
    		}
    		if(h=="MIN_GAP")
    			cout<<mod1<<endl;
    		if(h=="MIN_SORT_GAP")
    			cout<<mod2<<endl;
    	}
    	/*int n,m;
    	cin>>n>>m;
    	int b;
    	for(int i=1;i<=n;i++)
    	{
    		cin>>b;
    		insert(root1,b);
    		visit(root1);
    		cout<<endl;
    		int ha=-0x7fffffff;
    		pre(root1,b,ha);
    		cout<<ha<<" ";
    		ha=0x7fffffff;
    		nxt(root1,b,ha);
    		cout<<ha<<endl;
    	}
    	for(int i=1;i<=m;i++)
    	{
    		cin>>b;
    		del(root1,b);
    		visit(root1);
    		cout<<endl;
    	}*/
    	return 0;
    	
    }
    /*
    3 5
    5 5 1
    INSERT 2 9
    MIN_SORT_GAP
    INSERT 2 6
    MIN_GAP
    MIN_SORT_GAP
    */
    
  • 相关阅读:
    代码质量管理平台SonarQube的安装、配置与使用
    IntelliJ IDEA优秀插件(编程通用)
    Helpers.parallel_bulk in Python not working?
    把mysql的数据导出成txt
    python从数据库获取全量数据的方法
    ylbtech-LanguageSamples-ExplicitInterface(显示接口)
    ylbtech-LanguageSamples-Events(事件)
    ylbtech-LanguageSamples-Delegates(委托)
    ylbtech-LanguageSamples-ConditionalMethods(条件方法)
    ylbtech-LanguageSamples-CommandLine(命令行参数)
  • 原文地址:https://www.cnblogs.com/Lance1ot/p/9048409.html
Copyright © 2011-2022 走看看