洛谷——P1456 Monkey King

题目描述

Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its own way and none of them knows each other. But monkeys can’t avoid quarrelling, and it only happens between two monkeys who does not know each other. And when it happens, both the two monkeys will invite the strongest friend of them, and duel. Of course, after the duel, the two monkeys and all of there friends knows each other, and the quarrel above will no longer happens between these monkeys even if they have ever conflicted.

Assume that every money has a strongness value, which will be reduced to only half of the original after a duel(that is, 10 will be reduced to 5 and 5 will be reduced to 2).

And we also assume that every monkey knows himself. That is, when he is the strongest one in all of his friends, he himself will go to duel.

一开始有n只孤独的猴子,然后他们要打m次架,每次打架呢,都会拉上自己朋友最牛叉的出来跟别人打,打完之后战斗力就会减半,每次打完架就会成为朋友(正所谓不打不相识o(∩_∩)o )。问每次打完架之后那俩猴子最牛叉的朋友战斗力还有多少,若朋友打架就输出-1.

请注意,如果 AA 和 BB 是朋友,BB 和 CC 是朋友,那么我们认为 AA 和 CC 也是朋友。

输入格式

There are several test cases, and each case consists of two parts.

First part: The first line contains an integer N(N<=100,000), which indicates the number of monkeys. And then N lines follows. There is one number on each line, indicating the strongness value of ith monkey(<=32768).

Second part: The first line contains an integer M(M<=100,000), which indicates there are M conflicts happened. And then M lines follows, each line of which contains two integers x and y, indicating that there is a conflict between the Xth monkey and Yth.

有多组数据

输出格式

For each of the conflict, output -1 if the two monkeys know each other, otherwise output the strength value of the strongest monkey among all of its friends after the duel.

输入输出样例

输入 #1复制
5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5
输出 #1复制
8
5
5
-1
10

说明/提示

题目可能有多组数据

并查集+线段树动态开点+线段树合并
起初每个猴子建一棵线段树
两个猴子打架,先找到其父亲结点,然后在他父亲的这棵树上找到最大值,然后该点权值减一,该点的值除二以后再加入到这棵树上,最后二者合并。
线段树记录的是武力值是否出现,由于武力值的范围不大,所以可以用线段树维护。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+500;
const int maxn=40000;
int fa[N];
struct segmenttree {
	int lc,rc,dat;
} tr[N*100];
int root[N],tot;

void init(int n) {
	tot=0;
	for(int i=1; i<=n; ++i) {
		fa[i]=i;
		root[i]=0;
	}
}

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

int build() {
	tot++;
	tr[tot].lc=tr[tot].rc=tr[tot].dat=0;
	return tot;
}

void insert(int p,int l,int r,int x,int val) {
	if(l==r) {
		tr[p].dat+=val;
		return;
	}
	int mid=(l+r)/2;
	if(x<=mid) {
		if(!tr[p].lc)tr[p].lc=build();
		insert(tr[p].lc,l,mid,x,val);
	} else {
		if(!tr[p].rc)tr[p].rc=build();
		insert(tr[p].rc,mid+1,r,x,val);
	}
	tr[p].dat=tr[tr[p].lc].dat+tr[tr[p].rc].dat;
}

int ask(int p,int l,int r) {
	if(l==r)return l;
	int mid=(l+r)/2;
	if(tr[tr[p].rc].dat) {
		ask(tr[p].rc,mid+1,r);
	} else {
		ask(tr[p].lc,l,mid);
	}
}

int merge(int p,int q,int l,int r) {
	if(!p)return q;
	if(!q)return p;
	if(l==r) {
		tr[p].dat+=tr[q].dat;
		return p;
	}
	int mid=(l+r)/2;
	tr[p].lc=merge(tr[p].lc,tr[q].lc,l,mid);
	tr[p].rc=merge(tr[p].rc,tr[q].rc,mid+1,r);
	tr[p].dat=tr[tr[p].lc].dat+tr[tr[p].rc].dat;
	return p;
}

int main() {
	int n;
	while(~scanf("%d",&n)) {
		init(n);
		for(int i=1; i<=n; ++i) {
			int x;
			scanf("%d",&x);
			root[i]=build();
			insert(root[i],1,maxn,x,1);
		}
		int m;
		scanf("%d",&m);
		while(m--) {
			int x,y;
			scanf("%d %d",&x,&y);
			int xx=find(x);
			int yy=find(y);
			if(xx==yy) {
				printf("-1\n");
				continue;
			}
			x=ask(root[xx],1,maxn);
			y=ask(root[yy],1,maxn);
			insert(root[xx],1,maxn,x,-1);
			insert(root[yy],1,maxn,y,-1);
			x/=2;
			y/=2;
			insert(root[xx],1,maxn,x,1);
			insert(root[yy],1,maxn,y,1);
			root[xx]=merge(root[xx],root[yy],1,maxn);
			printf("%d\n",ask(root[xx],1,maxn));
			fa[yy]=xx;
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值