【HDU - 4217 】Data Structure? (线段树求第k小数)
题干:
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
解题报告:
这题是暑假集训的时候的线段树的题,用线段树维护在某一个区间还剩下几个数。跟这题对比【HDU - 4006】The kth great number
AC代码:
//又忘了开longlong了吧。。。
#include<bits/stdc++.h>using namespace std;
const int MAX =262144 + 100000;
int n,m;struct TREE {int l,r;int val;
} tree[MAX*4];//数组要不要再大一点?void pushup(int cur) {tree[cur].val = tree[cur*2].val+ tree[cur*2+1].val;
}
void build(int l,int r,int cur) {tree[cur].l = l;tree[cur].r = r;if(tree[cur].l == tree[cur].r) {tree[cur].val = 1;return ;}int m = (l + r)/ 2;build(l,m,cur*2);build(m+1,r,cur*2+1);pushup(cur);}
int query(int k,int cur) {if(tree[cur].l == tree[cur].r){// tree[cur].val = 0;return tree[cur].l;}if(k > tree[cur*2].val) return query(k-tree[cur*2].val,cur*2+1);else return query(k,cur*2);
}
void update(int tar,int cur) {if(tree[cur].l == tree[cur].r) {tree[cur].val = 0;return ;}if(tar <= tree[cur*2].r) update(tar,2*cur);else update(tar,2*cur+1);pushup(cur);
}int main()
{int t,tmp;int k,iCase = 0;;long long ans = 0;cin>>t;while(t--) {//初始化ans = 0;scanf("%d%d",&n,&m);build(1,n,1);while(m--) {scanf("%d",&k);tmp = query(k,1);update(tmp,1);ans += tmp;}printf("Case %d: %lld\n",++iCase,ans);}return 0 ;
}
【HDU - 4217 】Data Structure? (线段树求第k小数)相关推荐
- HDU - 6967 G I love data structure 线段树维护矩阵 + 细节
传送门 文章目录 题意: 思路: 题意: 给你两个长度为nnn的数组a,ba,ba,b,你需要完成如下四种操作: 思路: 思路还是比较简单的,首先建一颗线段树,线段树中维护a,b,a2,b2,aba, ...
- hdu4217 Data Structure? 线段树
题意:不解释了. 看了别人的博客才知道要这么用线段树,用的灵活. 线段树的节点s[k].n记录剩下节点的数目.当查找到s[k].l=s[k].r即叶子节点的时候,就取这个值,然后将s[k].n=0说明 ...
- HDU - 1255 覆盖的面积(线段树求矩形面积交 扫描线+离散化)
链接:线段树求矩形面积并 扫描线+离散化 1.给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. 2.看完线段树求矩形面积并 的方法后,再看这题,求的是矩形面积交,类同. 求面积时,用被覆 ...
- hdu 2461(线段树求面积并)
题意:给出N个矩形,M次询问 每次询问给出R个,问这R个矩形围成的面积 解题思路:对于每次询问,做一次线段树求面积的并操作. 每个节点保存的信息有l,r,cover,len分别表示该节点表示的区间[l ...
- poj 1151(线段树求面积并)
解题思路:线段树求面积并,水题 #include<iostream> #include<cstdio> #include<cstring> #include< ...
- HDU - 3333 Turing Tree(线段树+离线处理)
题目链接:点击查看 题目大意:给定一个长度为n的数列,依次求m个区间中不相同数字之和 题目分析:n给的是3e4,看到区间问题先要想到线段树或差分区间或动态规划,暴力是肯定不行滴,那么这个题已经知道是需 ...
- No Pain No Game HDU - 4630(gcd+线段树+离线处理)
Life is a game,and you lose it,so you suicide. But you can not kill yourself before you solve this p ...
- 线段树求逆序对(hdu1394Minimum Inversion Number)
说实话,线段树求逆序对我理解了半天诶,不知是否有人像我一样. 对于每个数来说,只有和已经出现过的.比它大的数才能形成逆序对,那么在给定的数列中,每给一个数就向前找比它大的数. 样例:10 1 3 6 ...
- 吉首大学2019年程序设计竞赛(重现赛) 干物妹小埋(线段树求最长上升子序列)
链接:https://ac.nowcoder.com/acm/contest/992/B 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...
最新文章
- java 循环读取文件_您如何用Java连续读取文件?
- Pandas数据处理实战:福布斯全球上市企业排行榜数据整理
- 深度学习核心技术精讲100篇(二十二)-深度解析数学建模核心知识之搜索与推荐模型中用户建模原理
- DL之RNN:基于RNN实现模仿贴吧留言
- System.img是如何打包的
- Problem H: tmk买礼物
- Docker学习总结(64)——快速理解 Docker 底层原理
- @程序员,为你揭开直播技术的神秘面纱!
- flask-session组件
- dto与java bean_POJO、JavaBean、DTO的区别
- 使用python读取excel
- 如何用计算机制作统计图,统计图制作_ai的环形统计图怎么制作
- Python 高等数学问题的符号解
- memory allocator php,LNMP的安装
- python3 面向对象_傻瓜式学Python3——面向对象
- 阿里云 企业邮箱域名解析(DNS)
- Python黑白转换程序
- Android R(11)为自定义HIDL接口添加DMFCM(六)
- java计算机毕业设计线上花店购物商城源码+lw文档+系统+数据库
- 转载 PCIe学习(三):PCIe DMA关键模块分析之二
热门文章
- 文档排序--相似度模型--VSM
- [Leetcode][第977题][JAVA][有序数组的平方][排序][双指针]
- [签名算法]DSA 算法
- PAT-1127. ZigZagging on a Tree (30)
- app推送以及提示音java,springboot 整合 Jpush 极光推送
- ext get id js_【翻译】Ext JS最新技巧——2015-8-11
- Java设计模式笔记(2)工厂方法模式
- php获取悉尼时间,php在使用澳大利亚/悉尼时区时给出错误答案
- mysql配置文件结构_MariaDB/MySQL配置文件my.cnf解读
- php鼠标悬停显示图片,鼠标滑过出现预览的大图提示效果