[codeforces 1354D] Multiset 线段树(由数值找节点位置,由排名找节点位置)

本文详细解析了Codeforces Round 87中问题D的解决方案,采用线段树数据结构处理多集操作,包括数值查找和排名查找。通过具体代码示例,展示了如何在比赛中高效解决此类问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Educational Codeforces Round 87 (Rated for Div. 2)  参与排名人数11499,比赛前遇到2次延时,17:05-17:15,17:15-17:20

[codeforces 1354D]   Multiset   线段树(由数值找节点位置,由排名找节点位置)

总目录详见https://blog.youkuaiyun.com/mrcrack/article/details/103564004

在线测评地址https://codeforces.com/contest/1354/problem/D

ProblemLangVerdictTimeMemory
D - Multiset GNU C++17Accepted702 ms15600 KB

思路:线段树(由数值找节点位置,由排名找节点位置)

样例数据生成过程,如下所示

Input:
6 2
1 1 1 2 3 4
5 6
Output:
6

AC代码如下

#include <stdio.h>
#define maxn 1000010
#define lson k<<1
#define rson k<<1|1
int tree[maxn<<2];//tree[]统计区间内,数的数量
void insert(int k,int l,int r,int x){//此处x表示数值
	if(l==r){
		tree[k]++;
		return;
	}
	int mid=(l+r)>>1;//mid分裂数值区间
	if(x<=mid)insert(lson,l,mid,x);
	else insert(rson,mid+1,r,x);
	tree[k]=tree[lson]+tree[rson];
}
int del(int k,int l,int r,int x){//此处x表示排名
	if(l==r){
		tree[k]--;
		return l;
	}
	int mid=(l+r)>>1,p;//mid分裂数量区间
	if(x<=tree[lson])p=del(lson,l,mid,x);
	else p=del(rson,mid+1,r,x-tree[lson]);
	tree[k]=tree[lson]+tree[rson];
	return p;
}
int main(){
	int n,q,i,x;
	scanf("%d%d",&n,&q);
	for(i=1;i<=n;i++)scanf("%d",&x),insert(1,1,n,x);
	while(q--){
		scanf("%d",&x);
		if(x>0)insert(1,1,n,x);
		else del(1,1,n,-x);
	}
	printf("%d\n",tree[1]?del(1,1,n,1):0);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值