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
 

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

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

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

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

/*
Problem : 4217 ( Data Structure? ) Judge Status : Accepted
RunId : 13881893 Language : C++ Author : 24862486
Timer : 998ms
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define lson rt << 1 , L , mid
#define rson rt << 1 | 1 , mid + 1 , R
const int maxn=262144+5;
int n,k,ki,T;
int sum[maxn<<2];
void pushup(int rt) {
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int rt,int L,int R) {
if(L==R) {
sum[rt]=1;
return;
}
int mid=(L+R)>>1;
build(lson);
build(rson);
pushup(rt);
} int query(int p,int rt,int L,int R) {
if(L==R) {
sum[rt]=0;
return R;
}
int mid=(L+R)>>1;
int res;
if(sum[rt<<1]>=p)res=query(p,lson);//向终点靠拢
else res=query(p-sum[rt<<1],rson);
pushup(rt);
return res;
}
int main() {
scanf("%d",&T);
for(int t=1; t<=T; t++) {
scanf("%d%d",&n,&k);
build(1,1,n);
long long res=0;
for(int i=0; i<k; i++) {
scanf("%d",&ki);
res+=query(ki,1,1,n);
}
printf("Case %d: %lld\n",t,res);
}
return 0;
}

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. cannot ignore cache if it is not cached [ArcGIS Catalog 10]

    由于我把缓存重命名,重启地图服务,再Caching中不再显示地图缓存了,我直接创建新的地图缓存,没想到出现了: 不知道怎么回事. 只好,把缓存 重新改名成原来的名字,然后,删除缓存,再建立缓存.

  2. Zindex和png

    Z轴在元素设置position为absolute或relative后被激活,起大小由z-index设置,z-index越大,元素位置越靠上.如果多个元素的z-index值相同,那么html标签中后出现 ...

  3. ASP.NET MVC2之Model Binder

    Model Binder在Asp.net MVC中非常简单.简单的说就是你控制器中的Action方法需要参数数据:而这些参数数据包含在HTTP请求中,包括表单上的Value和URL中的参 数等.而Mo ...

  4. 插件化 VirtualAPK 简介 体验 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  5. javascript运算符instanceof

    概述 instanceof 运算符可以用来判断某个构造函数的prototype属性是否存在另外一个要检测对象的原型链上. 语法 object instanceof constructor 参数 obj ...

  6. AI 也开源:50 大开源 AI 项目 (转)

    这些开源AI项目专注于机器学习.深度学习.神经网络及其他应用场合. 自IT界早期以来,研制出能像人类那样“思考”的机器一直是研究人员的一大目标.在过去几年,计算机科学家们在人工智能(AI)领域已取得了 ...

  7. 详解管理root用户权限的sudo服务程序

    在你想要使用超级权限临时运行一条命令时,sudo 命令非常方便,但是当它不能如你期望的工作时,你也会遇到一些麻烦.比如说你想在某些日志文件结尾添加一些重要的信息,你可能会尝试这样做: $ echo & ...

  8. webstorm和intellij idea下如何自动编译sass和scss文件

    webstorm和intellij idea下如何自动编译sass和scss文件 https://segmentfault.com/a/1190000008996504 https://www.jia ...

  9. 【math】梯度下降法(梯度下降法,牛顿法,高斯牛顿法,Levenberg-Marquardt算法)

    原文:http://blog.csdn.net/dsbatigol/article/details/12448627 何为梯度? 一般解释: f(x)在x0的梯度:就是f(x)变化最快的方向 举个例子 ...

  10. oauth2-server-php-docs 授权类型

    授权码 概观 在Authorization Code交付式时使用的客户端想要请求访问受保护资源代表其他用户(即第三方).这是最常与OAuth关联的授予类型. 详细了解授权码 用例 代表第三方来电 履行 ...