【HDU - 4217】【Data Structure?】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4217

题目:

Data structure is one of the basic skills for Computer Science students, which is a particular way of storing and organizing data in a computer so that it can be used efficiently. Today let me introduce a data-structure-like problem for you. 
Original, there are N numbers, namely 1, 2, 3...N. Each round, iSea find out the Ki-th smallest number and take it away, your task is reporting him the total sum of the numbers he has taken away. 

Input

The first line contains a single integer T, indicating the number of test cases. 
Each test case includes two integers N, K, K indicates the round numbers. Then a line with K numbers following, indicating in i (1-based) round, iSea take away the Ki-th smallest away. 

Technical Specification 
1. 1 <= T <= 128 
2. 1 <= K <= N <= 262 144 
3. 1 <= Ki <= N - i + 1 

Output

For each test case, output the case number first, then the sum.

Sample Input

2
3 2
1 1
10 3
3 9 1

Sample Output

Case 1: 3
Case 2: 14

题意:给我们t个测试样例,然后每次一个n 一个m 再给m 个数a1 a2 a3....,将在n排序中 第a1 a2...小的数求和,输出和

解题思路:线段树的查询和更改,每次寻找第a1 小的数,找到后加和,并把a1那位设为0,往复进行/

ac代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define Maxn 16010000
#define maxn 800085
#define inf 0x3f3f3f3f
using namespace std;

struct node{
	int l,r;
	int id;
}tree[maxn];

void pushup(int cur)
{
	tree[cur].id=tree[cur<<1].id+tree[cur<<1|1].id;
}
void build(int l,int r,int cur)
{
	tree[cur].l=l;
	tree[cur].r=r;
	if(l==r)
	{
		tree[cur].id=1;
		return;	
	}
	int mid=(l+r)>>1;
	build(l,mid,cur<<1);
	build(mid+1,r,cur<<1|1);
	pushup(cur);
}
int query(int xx,int cur)
{
	int ans;
	if(tree[cur].l==tree[cur].r)
	{
		tree[cur].id=0;
		return tree[cur].l;
	}
	if(tree[cur<<1].id>=xx)
		ans=query(xx,cur<<1);
	else  ans=query(xx-tree[cur<<1].id,cur<<1|1);
	pushup(cur);
	return ans;
}
int nn[maxn];
int mm[maxn];
long long ans;
int main()
{
	int t;
	scanf("%d",&t);
	int n,m;
	for(int kase=1;kase<=t;kase++)
	{
		ans=0;
		scanf("%d%d",&n,&m);
		build(1,n,1);
		while(m--)
		{
			int t;
			scanf("%d",&t);
			ans+=query(t,1);
		}
		printf("Case %d: ",kase);
		printf("%lld\n",ans);
	} 
	return 0;
}

需注意的是,在向下区间寻找第k小的数字的时候,如果在左孩子区间,直接寻找就好,如果是在有孩子区间,那么需要减去左孩子的数目。

就酱。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值