HDU–4217 — Data Structure? [線段樹]

來源:互聯網
上載者:User

Data Structure?


Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2133 Accepted Submission(s): 682

Problem DescriptionData 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.

InputThe 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

OutputFor 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

Code:

題意是每次取走第k個數,問最後共取走多大的數

在結構體體裡用一個變數表示這一段數位個數,取走一個就更新,-1

取數的時候如果k>左子樹的len,那麼應該轉向右子樹尋找, 否則就在左子樹尋找

知道找到一個區間lside==rside, 即為要取的數

尋找過程中就--len,順便更新

HDOJ不能用long long, 用__int64來表示大數, %I64d輸出

#include<stdio.h>#define M 300000struct node{    int l,r;    int len;}tree[M*4];void build(int p,int l,int r){    tree[p].l = l;    tree[p].r = r;    tree[p].len = r-l+1;    if(l==r) return;    int mid = (l+r)>>1;    int next = p<<1;    build(next,l,mid);    build(next+1,mid+1,r);}__int64 take(int p,int n){    tree[p].len--;    if(tree[p].l == tree[p].r){        return tree[p].l;    }     int next = p<<1;        if(tree[next].len < n)     take(next+1,n-tree[next].len);    else    take(next,n);    }int main(){    int t,ca,n,k,i,j;    __int64 sum;    scanf("%d",&t);    for(ca=1;ca<=t;ca++)    {        scanf("%d%d",&n,&k);        build(1,1,n);                sum = 0;        for(j=0;j<k;j++)        {            scanf("%d",&n);            sum += take(1,n);        }        printf("Case %d: %I64d\n",ca,sum);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.