日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【HDU - 4217 】Data Structure? (线段树求第k小数)

發(fā)布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【HDU - 4217 】Data Structure? (线段树求第k小数) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題干:

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

解題報告:

? ? ? 這題是暑假集訓(xùn)的時候的線段樹的題,用線段樹維護(hù)在某一個區(qū)間還剩下幾個數(shù)。跟這題對比【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];//數(shù)組要不要再大一點(diǎn)?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 ; }

?

總結(jié)

以上是生活随笔為你收集整理的【HDU - 4217 】Data Structure? (线段树求第k小数)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。