Problem Description
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

这题是简单的插空问题,只要维护每条线段还剩多少空就行,坑点是要用__int64,wa了两次。。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
__int64 sum;
struct node{
int l,r,num;
}b[8*300000]; void build(int l,int r,int i)
{
int mid;
b[i].l=l;b[i].r=r;b[i].num=r-l+1;
if(l==r)return;
mid=(l+r)/2;
build(l,mid,i*2);
build(mid+1,r,i*2+1);
} void question(int index,int i)
{
int mid;
if(b[i].l==b[i].r){
sum+=b[i].l;
b[i].num=0;return;
}
if(b[i*2].num>=index)question(index,i*2);
else question(index-b[i*2].num,i*2+1);
b[i].num=b[i*2].num+b[i*2+1].num;
} int main()
{
int n,m,i,j,T,num1=0,c;
scanf("%d",&T);
while(T--)
{
num1++;
//printf("\n",num1);
scanf("%d%d",&n,&m);
build(1,n,1);
sum=0;
for(i=1;i<=m;i++){
scanf("%d",&c);
question(c,1);
}
printf("Case %d: %I64d\n",num1,sum);
}
return 0;
}

最新文章

  1. 单例模式&mdash;&mdash;创建型模式01
  2. Python:XXX missing X required positional argument: &#39;self&#39;
  3. Problem K 栈
  4. 前端开发者应该知道的 CSS 小技巧
  5. Morgan Stanley telephone interview
  6. SpringAOP导致@Autowired依赖注入失败
  7. URL和URL比较
  8. PowerShell工作流学习-5-自定义活动
  9. Mybatis常考面试题汇总(附答案)
  10. SQLSERVER CTE表 row_number()字段 BUG
  11. SkylineGlobe 如何二次开发获取三维模型的BBOX和设置Tint属性
  12. 洛谷 P5206: bzoj 5475: LOJ 2983: [WC2019] 数树
  13. webapi框架搭建-安全机制(一)
  14. java笔记知识点总结
  15. Unity各平台内置宏定义
  16. Linux下使用DD命令测试磁盘读写速度
  17. python-json.loads()保持原json字符串的顺序
  18. 对JSON的理解
  19. linux 下 进程和线程的区别
  20. ASP.NET RemoteAttribute远程验证更新问题

热门文章

  1. Linux调整lvm逻辑分区大小
  2. 探索微软开源Python自动化神器Playwright
  3. 如何在K8s,Docker-Compose注入镜像Tag
  4. Django-html文件实例
  5. 处理 K8S Orphaned pod found - but volume paths are still present on disk 孤儿pod
  6. Redis 实战 —— 10. 实现内容搜索、定向广告和职位搜索
  7. loj10087
  8. Web漏洞扫描-AppScan
  9. MySql(五)SQL优化-优化SQL语句的一般步骤
  10. Java——Character类