首页 > 其他分享 >poj 2985(并查集+线段树求K大数)

poj 2985(并查集+线段树求K大数)

时间:2023-05-29 22:38:07浏览次数:39  
标签:return int 查集 tree 2985 树求 maxn ans include


解题思路:这道题并查集很容易,合并时找到父节点就直接加上去就ok了。关键是如何求K大数,我一直在想用线段树怎么写,一开始想如果直接记录数的大小那肯定是没戏了,借鉴了一下别人的思路:区间[a,b]记录的是所有的数里面,等于a,a+1,a+2,......,b-1,b的个数。看到这里就应该明白了,这里线段树的用法是把它看做是一个1-n的数轴。到时候要修改某一个数,就直接在数轴上修改它。至于记录[a,b]的个数,是为了找到K大数。


参考博客:http://blog.sina.com.cn/s/blog_6fd8e0fe0100v89n.html



#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn = 200005;
struct Segment
{
	int l,r;
	int sum;
}tree[maxn<<2];
int n,m,fa[maxn],tot[maxn];

int find(int x)
{
	if(fa[x] == x) return x;
	return fa[x] = find(fa[x]);
}

void build(int rt,int l,int r)
{
	tree[rt].l = l, tree[rt].r = r;
	if(tree[rt].l == 1) tree[rt].sum = n;
	else tree[rt].sum = 0;
	if(l == r) return;
	int mid = (l + r) >> 1;
	build(rt<<1,l,mid);
	build(rt<<1|1,mid+1,r);
}

void update(int rt,int pos,int val)
{
	tree[rt].sum += val;
	if(tree[rt].l == tree[rt].r) return;
	int mid = (tree[rt].l + tree[rt].r) >> 1;
	if(pos <= mid) update(rt<<1,pos,val);
	else update(rt<<1|1,pos,val);
}

int query(int rt,int k)
{
	if(tree[rt].l == tree[rt].r) return tree[rt].l;
	int mid = (tree[rt].l + tree[rt].r) >> 1;
	if(tree[rt<<1|1].sum >= k) return query(rt<<1|1,k);
	else return query(rt<<1,k - tree[rt<<1|1].sum);
}

int main()
{
	int op,a,b,x,y;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(int i = 1; i <= n; i++)
			fa[i] = i, tot[i] = 1;
		build(1,1,n);
		for(int i = 1; i <= m; i++)
		{
			scanf("%d",&op);
			if(op)
			{
				scanf("%d",&a);
				printf("%d\n",query(1,a));
			}
			else
			{
				scanf("%d%d",&a,&b);
				x = find(a);
				y = find(b);
				if(x == y) continue;
				fa[y] = x;
				update(1,tot[x],-1);
				update(1,tot[y],-1);
				update(1,tot[x]+tot[y],1);
				tot[x] += tot[y];
				tot[y] = 0;
			}
		}
	}
	return 0;
}




用树状数组求k大数:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn = 200005;
struct Tree
{
	int n,c[maxn];

	void init(int n)
	{
		this->n = n;
		memset(c,0,sizeof(c));
	}
	int lowbit(int x)
	{
		return x & -x;
	}
	void update(int x,int d)
	{
		while(x <= n)
		{
			c[x] += d;
			x += lowbit(x);
		}
	}
	int sum(int x)
	{
		int ans = 0;
		while(x > 0)
		{
			ans += c[x];
			x -= lowbit(x);
		}
		return ans;
	}
}tree;
int n,m,fa[maxn],cnt[maxn];

int find(int x)
{
	if(fa[x] == x) return x;
	return fa[x] = find(fa[x]);
}

int main()
{
	int op;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		tree.init(n);
		tree.update(1,n);
		for(int i = 1; i <= n; i++) fa[i] = i,cnt[i] = 1;
		while(m--)
		{
			scanf("%d",&op);
			if(op == 0)
			{
				int i,j;
				scanf("%d%d",&i,&j);
				int f1 = find(i);
				int f2 = find(j);
				if(f1 != f2)
				{
					tree.update(cnt[f1],-1);
					tree.update(cnt[f2],-1);
					fa[f2] = f1;
					cnt[f1] += cnt[f2];
					tree.update(cnt[f1],1);
				}
			}
			else
			{
				int k,l = 1,r = n,mid,ans;
				scanf("%d",&k);
				while(l <= r)
				{ 
					mid = (l + r) >> 1;
					int tmp = tree.sum(n) - tree.sum(mid-1);
					if(tmp >= k)
					{
						ans = mid;
						l = mid + 1;
					}
					else r = mid - 1;
				}
				printf("%d\n",ans);
			}
		}
	}
	return 0;
}




标签:return,int,查集,tree,2985,树求,maxn,ans,include
From: https://blog.51cto.com/u_16143128/6374301

相关文章

  • hdu 2473(并查集+删除操作)
    解题思路:这道题有并查集的删除操作,如果直接对这一棵树进行删除节点操作肯定是很困难的。所以可以建立虚拟节点,只要有一个节点要被删除,就直接把它投影到虚拟节点上,即用这个虚拟节点来代替我们要删除的节点。这样我们就不需要用对已有的树形结构进行改动了。这个想法我在DragonBalls......
  • hdu 3635(并查集+路径压缩变形)
    解题思路:这道题想了我好久,因为我把城市的编号一起考虑进去了,结果想了好久都没A,最后看了别人的题解居然都没有考虑到城市的编号,不考虑城市编号的问题的话就是一个很水的并查集了。#include<iostream>#include<cstdio>#include<cstring>usingnamespacestd;constintMAXN=1000......
  • poj 1988(并查集)
    题意:进行m次操作,M x y 将包含x的集合移动到y上面,C x, 计算x下面有几个元素。解题思路:这道题很容易想到用并查集,但是这里有点绕;最开始我想到的是建立一个num[x],表示x以下的节点数,但这样会有一个问题,要更新num[x]时,必须要枚举哪些节点的父节点为p,由于节点数太多,所以TLE是难免的......
  • hdu 3172(并查集+hash)
    解题思路:典型的并查集,只是每个人的名字要转换成数字,可以用map,也可以用字典树,我最开始用的字典树结果爆内存了。。爆内存:#include<iostream>#include<cstdio>#include<cstring>usingnamespacestd;constintmaxn=200000;intn,fa[maxn],trie[maxn][52],cnt,id[2000000],nu......
  • 【蓝桥杯 2019 省 A】修改数组【并查集】
    链接https://www.luogu.com.cn/problem/P8686题意给你\(n\)个数a[1...n],从\(a_2\)开始,如果和之前的某个数具有相等的值,就一直让\(a_i=a_i+1\),直到前面的任何一个数都和它不相等\(1\leqn\leq10^5\),\(1\leqa_i\leq10^6\)问最后的序列是什么思路暴力做法......
  • C++文件流结构体序列化,并查集,LRU缓存
    c语言中的文件操作中用fprintf将数据写入到文件中,用fscanf将文件读入内存中,而c++中也有ostream和istream作为键盘流输入,屏幕流输出,对于文件也有ofstream/istream来进行相关的操作.如图:图中表示将一个结构体的的数据输入到文件中,并从文件中读取数据,并用得到的数据初始化一......
  • 并查集
    6-1并查集///find函数可能有点难理解,自己尝试画下图随便理解好吧///M是合并,Q是询问是否在一个树中#include<bits/stdc++.h>usingnamespacestd;constintN=10010;intn,m;intq[N];intfind(intx){if(q[x]!=x)q[x]=find(q[x]));///如果x不是树的头结点,向上......
  • 并查集
    并查集并查集是一种用于处理集合合并和查询的数据结构,常用于连通性问题。它可以动态地添加和合并集合,并快速地查询两个元素是否在同一个集合中。————chatGPT并查集被很多OIer认为是最简洁而优雅的数据结构之一,主要用于解决一些元素分组的问题......
  • 【CF1012E】【LOJ2818】Cycle Sort(并查集)
    Description给定一个⻓为nn的数列,你可以多次进行如下操作:选定kk个不同的下标i1,i2…iki1,i2......
  • 并查集概述
      并查集基础一、概念及其介绍并查集是一种树型的数据结构,用于处理一些不相交集合的合并及查询问题。并查集的思想是用一个数组表示了整片森林(parent),树的根节点唯一标识了一个集合,我们只要找到了某个元素的的树根,就能确定它在哪个集合里。二、适用说明并查集用在一些有......