码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2217 Data Structure?

时间:2015-06-12 11:51:24      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:

C - Data Structure?
Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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. 
 

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
 
这道题目可以用线段树,或者是树状数组来解决
对于题意而言,他的意思是给你n个数字,分别为1...n然后是
给你询问,让你去除这个数,但是询问的内容是序列,比如说1,2,3如果我取出了2,那么当询问为2的时候就是3了,因为序列变为了1,3

然后是通过线段树的标记功能

/*
Problem : 4217 ( Data Structure? )     Judge Status : Accepted
RunId : 13881893    Language : C++    Author : 24862486
Timer : 998ms
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define lson rt << 1 , L , mid
#define rson rt << 1 | 1 , mid + 1 , R
const int maxn=262144+5;
int n,k,ki,T;
int sum[maxn<<2];
void pushup(int rt) {
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int rt,int L,int R) {
    if(L==R) {
        sum[rt]=1;
        return;
    }
    int mid=(L+R)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}

int query(int p,int rt,int L,int R) {
    if(L==R) {
        sum[rt]=0;
        return R;
    }
    int mid=(L+R)>>1;
    int res;
    if(sum[rt<<1]>=p)res=query(p,lson);//向终点靠拢
    else res=query(p-sum[rt<<1],rson);
    pushup(rt);
    return res;
}
int main() {
    scanf("%d",&T);
    for(int t=1; t<=T; t++) {
        scanf("%d%d",&n,&k);
        build(1,1,n);
        long long  res=0;
        for(int i=0; i<k; i++) {
            scanf("%d",&ki);
            res+=query(ki,1,1,n);
        }
        printf("Case %d: %lld\n",t,res);
    }
    return 0;
}


HDU 2217 Data Structure?

标签:

原文地址:http://blog.csdn.net/qq_18661257/article/details/46468459

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!