LA 5031 Graph and Queries Treap名次树

LA 5031

提议:有一张n个节点个m条边的无向图,每个点有权值,有若干操作: D X :删除ID为X的边,Q X K:计算与节点X联通的节点中,第K大的权值,不存在则当作0,C X V:把节点X的权值改为V。输出所有Q指令计算结果的平均值。

思路:白书名次树的例题,以前看到这题庞大的代码量就不敢学了,今天学了后发现比后缀数组容易理解多了,代码量大不一定就难懂,有个删除边的操作,因此直接用Treap不太好弄,所以可以把操作顺序反过来,首先建立好Treap,之前要删除边的,那么就不把这两个联通块连接就好了,之前需要改的点权值,就都改了,遍历倒序操作时,如果是删除边,就变成连接两个联通块,可用并查集连接,把容量小的Treap连接到容量大的Treap,省复杂度,如果是改权值,那就改回到之前的权值,然后就直接套用模板了。

#include<cstdio>
#include<cstring>
#include<vector>
#include<cstdlib>
using namespace std;
struct Node{
	Node *ch[2];
	int r;
	int v;
	int s;
	Node(int v):v(v){ch[0]=ch[1]=NULL;r=rand();s=1;}
	bool operator<(const Node& rhs)const{
		return r<rhs.r;
	}
	int cmp(int x)const{
		if(x==v)return -1;
		return x<v?0:1;
	}
	void maintain()
	{
		s=1;
		if(ch[0]!=NULL)s+=ch[0]->s;
		if(ch[1]!=NULL)s+=ch[1]->s;
	}
};
void rotate(Node* &o,int d)
{
	Node* k=o->ch[d^1];o->ch[d^1]=k->ch[d];k->ch[d]=o;
	o->maintain();k->maintain();o=k;
}
void insert(Node* &o,int x)
{
	if(o==NULL)o=new Node(x);
	else
	{
		int d=(x<o->v?0:1);
		insert(o->ch[d],x);
		if(o->ch[d]->r>o->r)rotate(o,d^1);
	}
	o->maintain();
}
void remove(Node* &o,int x)
{
	int d=o->cmp(x);
	if(d==-1)
	{
		Node* u=o;
		if(o->ch[0]!=NULL&&o->ch[1]!=NULL)
		{
			int d2=(o->ch[0]>o->ch[1]?1:0);
			rotate(o,d2);remove(o->ch[d2],x);
		}else{
			if(o->ch[0]==NULL)o=o->ch[1];else o=o->ch[0];
			delete u;
		}
	}
	else
	remove(o->ch[d],x);
	if(o!=NULL)
	o->maintain();
}

/*    Treap树模板    */
 
const int maxc=5e5+10;
struct Command{
	char type;
	int x,p;
}commands[maxc];
const int maxn=2e4+10;
const int maxm=6e4+10; 
int n,m,weight[maxn],from[maxm],to[maxm],removed[maxm];
int pa[maxn];
int findset(int x){return pa[x]!=x?pa[x]=findset(pa[x]):x;}
Node* root[maxn];
int kth(Node* o,int k)
{
	if(o==NULL||o->s<k||k<=0)return 0;
	int s=(o->ch[1]==NULL?0:o->ch[1]->s);
	if(k==s+1)return o->v;
	else if(k<=s)return kth(o->ch[1],k);
	else return kth(o->ch[0],k-s-1);
}
void mergeto(Node* &src,Node* &dest)
{
	if(src->ch[0]!=NULL)mergeto(src->ch[0],dest);
	if(src->ch[1]!=NULL)mergeto(src->ch[1],dest);
	insert(dest,src->v);
	delete src;
	src=NULL;
}
void removetree(Node* &o)
{
	if(o->ch[0]!=NULL)removetree(o->ch[0]);
	if(o->ch[1]!=NULL)removetree(o->ch[1]);
	delete o;
	o=NULL;
}
void add_edge(int x)
{
	int u=findset(from[x]),v=findset(to[x]);
	if(u!=v)
	{
		if(root[u]->s<root[v]->s){pa[u]=v;mergeto(root[u],root[v]);}
		else {pa[v]=u;mergeto(root[v],root[u]);};
	}
}
int query_cnt;
long long query_tot;
void query(int x,int k)
{
	query_cnt++;
	query_tot+=kth(root[findset(x)],k);
}
void change_weight(int x,int v)
{
	int u=findset(x);
	remove(root[u],weight[x]);
	insert(root[u],v);
	weight[x]=v;
}
int main()
{
	int kase=0;
	while(~scanf("%d%d",&n,&m)&&n)
	{
		for(int i=1;i<=n;i++)scanf("%d",&weight[i]);
		for(int i=1;i<=m;i++)scanf("%d%d",&from[i],&to[i]);
		memset(removed,0,sizeof(removed));
		int c=0;
		while(1)
		{
			char type;
			int x,p=0,v=0;
			scanf(" %c",&type);
			if(type=='E')break;
			scanf("%d",&x);
			if(type=='D')removed[x]=1;
			if(type=='Q')scanf("%d",&p);
			if(type=='C')
			{
				scanf("%d",&v);
				p=weight[x];
				weight[x]=v;
			}
			commands[c++]=(Command){type,x,p};
		}
		for(int i=1;i<=n;i++)
		{
			pa[i]=i;if(root[i]!=NULL)removetree(root[i]);
			root[i]=new Node(weight[i]);
		}
		for(int i=1;i<=m;i++)
		if(!removed[i])
		add_edge(i);
		query_cnt=query_tot=0;
		for(int i=c-1;i>=0;i--)
		{
			if(commands[i].type=='D')add_edge(commands[i].x);
			if(commands[i].type=='Q')query(commands[i].x,commands[i].p);
			if(commands[i].type=='C')change_weight(commands[i].x,commands[i].p);
		}
		printf("Case %d: %.6lf\n",++kase,query_tot/(double)query_cnt);
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Unity是一款强大的跨平台游戏引擎,也可以用于开发其他类型的应用程序。Unity提供了一个名为Unity Graph and Chart的工具,用于创建和展示图形和图表。 Unity Graph and Chart工具使用户能够在游戏或应用程序中轻松地创建和管理各种类型的图表,例如柱状图、饼状图、折线图等。它提供了一个直观的界面,使用户能够通过简单的拖拽和放置操作来生成图表。 使用Unity Graph and Chart,用户可以根据自己的需求自定义图表的样式和外观。它提供了丰富的样式选项,例如改变图表的颜色、调整坐标轴的格式、选择不同的图例样式等。用户还可以通过编程的方式动态地更新图表数据,使其能够实时反映应用程序中的变化。 除了基本的图表功能,Unity Graph and Chart还提供了一些额外的特性和工具来增强用户体验。例如,它可以支持多种不同的数据源,如数据库和网络数据。它还提供了一些交互式的功能,如放大、缩小、拖动等,使用户能够更好地探索图表数据。 最后,Unity Graph and Chart工具还具有良好的跨平台性能。无论是在PC、移动设备还是VR/AR设备上,都可以运行流畅,并且能够适应不同的屏幕分辨率和设备特性。 总之,Unity Graph and Chart是一个功能强大且易于使用的工具,可帮助用户在Unity引擎中创建和展示各种类型的图表。无论是用于游戏开发还是其他应用程序开发,都可以通过这个工具来有效地展示和分析数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长沙橘子猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值