杭電hdu 4217 Data Structure? 線段樹

http://acm.hdu.edu.cn/showproblem.php?pid=4217

線段樹,每個節點記錄當前子樹中還有多少個數。

//線段樹
#include <stdio.h>

#define MAX 262145

int tmp;

typedef struct _tree
{
	int left;
	int right;
	int count;
}tree;

tree t[MAX*3];

void build_tree(int c, int left, int right)
{
	int mid;
	t[c].left = left;
	t[c].right = right;
	t[c].count = right - left + 1;
	if(left == right)return ;
	mid = (left + right)>>1;
	build_tree(2*c, left, mid);
	build_tree(2*c+1, mid+1, right);
}

void del(int c, int ki)
{
	if(t[c].left == t[c].right){
		tmp = t[c].right;
		t[c].count --;
		return;
	}
	if(ki <= t[2*c].count){	//if the left tree'count is less than ki then the ki-th min number must be at the left tree
		del(2*c, ki);
	}
	else {	//else it is at the right tree
		del(2*c+1, ki - t[2*c].count);
	}
	t[c].count --;
}

int main()
{
	int t, n, k, cas, ki;
	__int64 res;
	scanf("%d", &t);
	for(cas = 1; cas <= t; cas ++){
		scanf("%d%d", &n, &k);
		build_tree(1, 1, n);
		res = 0;
		while(k --){
			scanf("%d", &ki);
			del(1, ki);
			res += tmp;
		}
		printf("Case %d: %I64d\n", cas, res);
	}
	return 0;
}


發佈了79 篇原創文章 · 獲贊 20 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章