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

  1. 2
  2. 3 2
  3. 1 1
  4. 10 3
  5. 3 9 1
 

Sample Output

  1. Case 1: 3
  2. Case 2: 14
 

这道题目能够用线段树,或者是树状数组来解决

对于题意而言,他的意思是给你n个数字,分别为1...n然后是

给你询问,让你去除这个数,可是询问的内容是序列,比方说1,2,3假设我取出了2,那么当询问为2的时候就是3了。由于序列变为了1,3

然后是通过线段树的标记功能

  1. /*
  2. Problem : 4217 ( Data Structure?
  3.  
  4. ) Judge Status : Accepted
  5. RunId : 13881893 Language : C++ Author : 24862486
  6. Timer : 998ms
  7. Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
  8. */
  9. #include <cstdio>
  10. #include <cstring>
  11. #include <algorithm>
  12. using namespace std;
  13. #define lson rt << 1 , L , mid
  14. #define rson rt << 1 | 1 , mid + 1 , R
  15. const int maxn=262144+5;
  16. int n,k,ki,T;
  17. int sum[maxn<<2];
  18. void pushup(int rt) {
  19. sum[rt]=sum[rt<<1]+sum[rt<<1|1];
  20. }
  21. void build(int rt,int L,int R) {
  22. if(L==R) {
  23. sum[rt]=1;
  24. return;
  25. }
  26. int mid=(L+R)>>1;
  27. build(lson);
  28. build(rson);
  29. pushup(rt);
  30. }
  31.  
  32. int query(int p,int rt,int L,int R) {
  33. if(L==R) {
  34. sum[rt]=0;
  35. return R;
  36. }
  37. int mid=(L+R)>>1;
  38. int res;
  39. if(sum[rt<<1]>=p)res=query(p,lson);//向终点靠拢
  40. else res=query(p-sum[rt<<1],rson);
  41. pushup(rt);
  42. return res;
  43. }
  44. int main() {
  45. scanf("%d",&T);
  46. for(int t=1; t<=T; t++) {
  47. scanf("%d%d",&n,&k);
  48. build(1,1,n);
  49. long long res=0;
  50. for(int i=0; i<k; i++) {
  51. scanf("%d",&ki);
  52. res+=query(ki,1,1,n);
  53. }
  54. printf("Case %d: %lld\n",t,res);
  55. }
  56. return 0;
  57. }

HDU 2217 Data Structure?的更多相关文章

  1. hdu 4217 Data Structure? 树状数组求第K小

    Data Structure? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. hdu 4217 Data Structure?/treap

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4217 可用线段树写,效率要高点. 这道题以前用c语言写的treap水过了.. 现在接触了c++重写一遍 ...

  3. HDU 6649 Data Structure Problem(凸包+平衡树)

    首先可以证明,点积最值的点对都是都是在凸包上,套用题解的证明:假设里两个点都不在凸包上, 考虑把一个点换成凸包上的点(不动的那个点), 不管你是要点积最大还是最小, 你都可以把那个不动的点跟原点拉一条 ...

  4. HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  5. HDU 5929 Basic Data Structure 模拟

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  6. Basic Data Structure HDU - 5929 (这个模拟我要报警了)

    Mr. Frog learned a basic data structure recently, which is called stack.There are some basic operati ...

  7. HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...

  8. [LeetCode] All O`one Data Structure 全O(1)的数据结构

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  9. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

随机推荐

  1. @Dubbo概述

    Dubbo是什么 Dubbo是一个开源分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案. 有三个核心部分包括: 远程通讯:提供对多种基于长连接的NIO框架抽象封 ...

  2. 迭代器模式(Iterator)

    @@@模式定义: 提供一种方法顺序访问一个聚合对象中的各个元素,而又不需暴露该对象的内部表示. @@@练习示例:  工资表数据的整合 @@@示例代码: \pattern\PayModel.java ~ ...

  3. C++ stringstream格式化输出输入探索

    - 最近在笔试时经常遇见各种输入问题,于是细心总结一波: - 首先string str; cin>>str;遇到空格结束: - 于是乎产生了getline(),可与得到一行字符串:空格自动 ...

  4. 《OpenGL® ES™ 3.0 Programming Guide》读书笔记1 ----总览

    OpenGL ES 3.0 Graphics Pipeline OpenGL ES 3.0 Vertex Shader Transform feedback: Additionally, OpenGL ...

  5. windows下修改memcached服务的端口号

    虽然memcached装载linux下的人比较多,但是还是有人要装载windows上,比如像我们公司. 虽然memcached默认的端口号挺好的,可是还是有人想该改变其端口号的,比如像我. 如果不是作 ...

  6. php tools 破解

    default.aspx <%@ Page Language="C#" %><% string selfKey = "<RSAKeyValue&g ...

  7. [Spring Boot] Complex Scope Scenarios of a Spring Bean - Mix Prototype and Singleton, ScopeProxy

    We have the following example: @SpringBootApplication public class In28minutesScopeApplication { pri ...

  8. NAS 网络附属存储

    本文内容 NAS NAS 功能 SAN 与 NAS 区别 NAS 底层协议 NAS 全球主要厂商 参考资料   NAS NAS(Network Attached Storage,网络附属存储)一种特殊 ...

  9. 阿里云构建Kafka单机集群环境

    简介 在一台ECS阿里云服务器上构建Kafa单个集群环境需要如下的几个步骤: 服务器环境 JDK的安装 ZooKeeper的安装 Kafka的安装 1. 服务器环境 CPU: 1核 内存: 2048 ...

  10. FastDFS tracker storage 的工作原理及流程

    FastDFS tracker storage 的工作原理及流程 2013 年 3 月 11 日 – 09:22 | 1,409 views | 收藏  (No Ratings Yet) FastDF ...