【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 1Sample 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小数)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2017中信信用卡还款方式 不知道的往这
- 下一篇: 【牛客 - 301哈尔滨理工大学软件与微