hdu-4217-线段树-链式存储实现
来源:互联网 发布:淘宝 一键转让 编辑:程序博客网 时间:2024/06/07 13:34
Data Structure?
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2106 Accepted Submission(s): 671
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.
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
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
23 21 110 33 9 1
Sample Output
Case 1: 3Case 2: 14一开始链式实现超内存,后来加了一个空间释放函数,AC,给大家分享一下吧。。。#include<stdio.h>#include<stdlib.h>typedef struct Node{ struct Node *lc,*rc;//左右孩子指针 int left,right;//左右区间边界 int len;//区间整点个数 }Tire,*Interval_tire;//定义线段树结构void Build(Interval_tire &T,int left,int right){ int mid; T=(Interval_tire)malloc(sizeof(Tire)); T->lc=NULL; T->rc=NULL; T->left=left; T->right=right; T->len=right-left+1; if(left==right)return;//递归出口 mid=(left+right)>>1;//左移一位,相当于除以2 Build(T->lc,left,mid); Build(T->rc,mid+1,right);} int Search(Interval_tire T,int key){ T->len--; if(T->left==T->right) return T->left;//如果左右端点值相等返回 if(T->lc->len>=key)//左孩子区间长度不小于key表示做区间可容纳至少key个数 return Search(T->lc,key); return Search(T->rc,key-T->lc->len);}void Ftree(Interval_tire &T){ if(T!=NULL) { if(T->lc!=NULL) Ftree(T->lc); if(T->rc!=NULL) Ftree(T->rc); free(T); } return ;} int main(){ int t,n,k,m=1; scanf("%d",&t); while(t--) { int i,p; __int64 sum=0; Interval_tire T; scanf("%d %d",&n,&k); Build(T,1,n);//构建从1到n的线段树 for(i=1;i<=k;i++) { scanf("%d",&p); sum+=Search(T,p);//查找第p小的数并累加到sum } printf("Case %d: %I64d\n",m++,sum); Ftree(T); }//while // system("pause"); return 0;}
以下是非链式存储的实现方式,果断AC,利用结构体数组模拟线段二叉树的构建,关键要搞明白当前结构体数组的下标乘以2代表的是其对应的左孩子,再加1以后代表的是其又孩子#include<stdio.h>#include<stdlib.h>#define N 262144struct{ int left,right;//左右区间边界 int len;//区间整点个数 }Tire[3*N];void Build(int p,int left,int right){ int mid,w; Tire[p].left=left; Tire[p].right=right; Tire[p].len=right-left+1; if(left==right)return;//递归出口 mid=(left+right)>>1;//左移一位,相当于除以2 w=p<<1;//右移一位相当于乘以2,此时代表左孩子节点位置!!!!!!!! Build(w,left,mid); Build(w+1,mid+1,right);} int Search(int p,int key){ int w; Tire[p].len--; if(Tire[p].left==Tire[p].right) return Tire[p].left;//如果左右端点值相等返回 w=p<<1; if(Tire[w].len>=key)//左孩子区间长度不小于key表示做区间可容纳至少key个数 return Search(w,key); return Search(w+1,key-Tire[w].len);} int main(){ int t,n,k,m=1; scanf("%d",&t); while(t--) { int i,p; __int64 sum=0; scanf("%d %d",&n,&k); Build(1,1,n);//构建从1到n的线段树 for(i=1;i<=k;i++) { scanf("%d",&p); sum+=Search(1,p);//查找第p小的数并累加到sum } printf("Case %d: %I64d\n",m++,sum); }//while // system("pause"); return 0;}
- hdu-4217-线段树-链式存储实现
- 线段树,实例,代码实现,区间最值,区间求和,顺序存储,链式存储
- java实现树(链式存储)
- 二叉树链式存储的实现
- 二叉树链式存储的实现
- 数据结构:二叉树的链式存储实现
- java实现树(链式存储)
- 二叉树链式存储的C实现
- 二叉树的链式存储实现
- 二叉树的链式存储实现
- 栈---链式存储实现
- 队列链式存储实现
- 栈的链式存储实现
- 栈的链式存储实现
- c语言链式存储实现
- 栈的链式存储实现
- Queue队列 链式存储实现
- 集合----链式存储的实现
- Activity---startActivityForResult()、onActivityResult()和setResult()使用
- mongodb_数据类型(null/字符串/数字/日期/内嵌文档/数组等)
- SoundPool使用
- cl.exe 介绍
- 炎热夏季 本本散热板必不可少【lpxt】
- hdu-4217-线段树-链式存储实现
- JNI 使用
- 深入浅出Spring(三) AOP详解
- AS3.0实现视频播放进度控制条
- 去掉一个view的background并没有还原成设置之前的情况,setBackgroundResource(0),谷歌给我们挖的坑
- Windows环境下Android 源码模块下载
- 小光棍数
- MYSQL数据库管理之权限管理
- 【C++沉思录】strtok的实现