在一个含有1-n的序列中,每次找到第Ki小的数,并把它删除(线段树)

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. 

在一个含有1-n的序列中,每次找到第Ki小的数,并把它删除。InputThe 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 
第一个数T,表示测试数据的组数。
每组测试数据,第一行2个整数n和m,m表示操作的轮数。
接下来m行,每行一个整数k,表示要找出第k小的数,并把它删除。OutputFor 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
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
struct node{
	ll l,r,sum;
}a[1200000];
void update(ll k){
	a[k].sum=a[k<<1].sum+a[k<<1|1].sum;
	return ;
}
//建树 
void build(ll k,ll l,ll r){
	a[k].l=l;
	a[k].r=r;
	if(l==r){
		a[k].sum=1;//每个叶节点起初长度都为1 
		return ;
	}
	ll mid=(a[k].l+a[k].r)>>1;
	build(k<<1,l,mid);
	build(k<<1|1,mid+1,r);
	update(k);
	return ;
}
ll ans;
void query(ll k,ll x){
	if(a[k].l==a[k].r){
		a[k].sum=0;
		ans+=a[k].l;
		return ;
	}
	ll mid=(a[k].l+a[k].r)>>1;
	//首先在左孩子中找 
	if(x<=a[k<<1].sum){
		query(k<<1,x);
	}
	else {
		//在右孩子中能够找记得变化 
		query(k<<1|1,x-a[k<<1].sum);
	}
	update(k);
	return ;
}


int main(){
	ll t,cnt=1;
	cin>>t;
	while(t--){
		ll n,c;
		cin>>n>>c;
		build(1,1,n);
		ll x;
		ans=0;
		for(ll i=1;i<=c;i++){
			cin>>x;
			query(1,x);
		}
		cout<<"Case "<<cnt++<<": "<<ans<<endl;
	}
	return 0;
}
//            /\       |  /  |**、
//			 /  \      | /   |   \
//			/    \     |/    |   /  _____                      ____   |  /
//		   /------\    |\    |__/  /     \  \      /\      /  /    \  | /
//		  /        \   | \   |    /       \  \    /  \    /  /______\ |/
//		 /          \  |  \  |    \       /   \  /    \  /   \        |
//      /            \ |   \ |     \_____/     \/      \/     \_____  |
/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
// warm heart, wagging tail,and a smile just for you!
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \|     |//  `.
//                     /  \|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \\  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//

  

转载于:https://www.cnblogs.com/akpower/p/11325366.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值