HDU 4217 Data Structure?(线段树的查找和更新)
2016-04-12 13:04
211 查看
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
本题是简单的线段树的更新和查找题目;
具体的步骤在代码中打上了注释。
下面是AC代码:
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
本题是简单的线段树的更新和查找题目;
具体的步骤在代码中打上了注释。
下面是AC代码:
#include<cstdio> #include<cstring> #include<algorithm> #define ll long long using namespace std; struct node { ll right,left,val; }c[600005]; void build_tree(ll root,ll l,ll r)//建树 { c[root].left=l; c[root].right=r; c[root].val=r-l+1;//表示这个节点有多少个数 if(c[root].left==c[root].right) { return ; } ll mid=(l+r)/2; build_tree(root*2,l,mid); build_tree(root*2+1,mid+1,r); } int find_tree(ll root,ll k)//查找第k小的数 { c[root].val--;//每查找一次,这个节点上的数就少一个 if(c[root].left==c[root].right) { return c[root].left; } if(c[root*2].val>=k)//如果左子节点上的数的个数大于k,那么只需要搜索左子节点 find_tree(root*2,k); else//否则的话搜索右子节点 { find_tree(root*2+1,k-c[root*2].val);//至于这里为什么用k来减,原因很明确,就是左子节点有c[root*2].val个值 } } int main() { int t; ll n,m; int kase=0; scanf("%d",&t); while(t--) { kase++; scanf("%lld%lld",&n,&m); build_tree(1,1,n); ll num,re=0; for(int i=0;i<m;i++) { scanf("%lld",&num); re+=find_tree(1,num); } printf("Case %d: %lld\n",kase,re); } return 0; }
相关文章推荐
- 将图像转换为特征向量Transforming Images to Feature Vectors
- Claris and XOR (数位dp)
- hdu 3555 Bomb【数位dp~吖!!!!!】
- PHP常量详解:define和const的区别
- RNN语言模型中的在线表示学习Online Representation Learning in Recurrent Neural language models
- 提取重复代码不应该只从代码角度,可以从业务角度看看(转)
- MyBatis在Oracle中插入数据并返回主键的问题解决
- UVA846
- kafka & zookeeper 4/12/2016
- 【5】mysql数据操作语言DML(Data Manipulation Language)
- IOS TextField设置大全
- 关于新建Android Studio项目时默认的编译sdk版本导致的兼容问题
- 描述Cookie和Session的作用,区别和各自的应用范围,cookie、Session工作原理
- 深度学习Theano教程Theano tutorial
- 我眼中的领域驱动设计(转)
- Rancher docker0: iptables: No chain/target/match
- HDOJ 1215 七夕节
- 图片预加载与懒加载
- 隐马尔科夫模型(HMM)及其扩展
- 《构建之法》阅读笔记4