HDU 4217 Data Structure?(线段树 or 树状数组啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4217

Problem 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

Author

[email protected]

Source

首届华中区程序设计邀请赛暨第十届武汉大学程序设计大赛

题意:

n个数为 1 → n,一共有 k 次操作,每次取出第 ki 小的数。

问所有取出数字之和。

(树状数组)

代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define maxn 262147
#define LL __int64
int c[maxn], a[maxn];
int n, k;
int Lowbit(int x)  // 2^k
{
    return x&(-x);
}
void update(int i, int x)//i点增量为x
{
    while(i <= n)
    {
        c[i] += x;
        i += Lowbit(i);
    }
}

int sum(int x)//区间求和 [1,x]
{
    int sum=0;
    while(x>0)
    {
        sum+=c[x];
        x-=Lowbit(x);
    }
    return sum;
}

int er_find(int kk)
{
    int L = kk, R = n;
    int tmp = kk;
    while(L <= R)
    {
        int mid = L+(R-L)/2;
        int tt = sum(mid);
        if(tt == kk)
        {
            tmp = mid;
            R = mid-1;
        }
        else if(tt < kk)
        {
            L = mid+1;
        }
        else if(tt > kk)
        {
            R = mid-1;
        }
    }
    return tmp;
}
int main()
{
    int t;
    int cas = 0;
    scanf("%d",&t);
    while(t--)
    {
        memset(c,0,sizeof(c));
        scanf("%d%d",&n,&k);
        for(int i = 1; i <= n; i++)
        {
            update(i,1);
        }
        int ki;
        LL ans = 0;
        for(int i = 1; i <= k; i++)
        {
            scanf("%d",&ki);
            int tmp = er_find(ki);

            ans += tmp;
            update(tmp, -1);//减一变为0
        }
        printf("Case %d: %I64d\n",++cas,ans);
    }
    return 0;
}

/*
99
3 2
1 1
10 3
3 9 1
10 5
1 2 3 4 5
10 4
1 2 3 4
10 5
5 4 3 2 1
10 5
1 5 7 4 2
*/

贴一发别人的线段树:http://www.cnblogs.com/xiaohongmao/archive/2012/04/29/2476452.html

#include <stdio.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 300000;
int tree[maxn<<2];
int temp;
void build(int l,int r,int rt)
{
    tree[rt]=r-l+1;
    if(l==r)
        return;
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}

void dele(int del,int l,int r,int rt)
{
    tree[rt]--;//0 已取
    if(l==r)
    {
        temp=l;
        return;
    }
    int m=(l+r)>>1;
    if(del<=tree[rt<<1])
        dele(del,lson);
    else
    {
        del-=tree[rt<<1];
        dele(del,rson);
    }
}
int main()
{
    int t,n,k,ki,i;
    int nCase=1;
    __int64 sum;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&k);
        build(1,n,1);
        sum=0;
        for(i=0; i<k; i++)
        {
            scanf("%d",&ki);
            dele(ki,1,n,1);
            sum+=temp;
        }
        printf("Case %d: %I64d\n",nCase++,sum);
    }
    return 0;
}
时间: 2024-11-04 21:46:37

HDU 4217 Data Structure?(线段树 or 树状数组啊)的相关文章

hdu 4217 Data Structure?/treap

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4217 可用线段树写,效率要高点. 这道题以前用c语言写的treap水过了.. 现在接触了c++重写一遍... 不带重复元素的插入删除第k大带垃圾回收,具体如下: 1 #include<cstdio> 2 #include<cstdlib> 3 #include<iostream> 4 #include<algorithm> 5 typedef long long

HDU 2217 Data Structure?

C - Data Structure? Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice id=27724" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="display:inline-

LeetCode Add and Search Word - Data structure design (trie树)

题意:实现添加单词和查找单词的作用,即实现字典功能. 思路:'.' 可以代表一个任何小写字母,可能是".abc"或者"a.bc"或者"abc.",能应对这三种就没有问题了.在每个单词的尾字母上标上tag=1,代表从树根到此节点有一个单词.暂时想不到更快的办法. 1 class WordDictionary { 2 public: 3 WordDictionary(){ 4 tree=create(); 5 } 6 void addWord(str

多校训练hdu --Nice boat(线段树,都是泪)

Nice boat Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 47 Accepted Submission(s): 10 Problem Description There is an old country and the king fell in love with a devil. The devil always ask

HDU 2852 KiKi&#39;s K-Number(线段树+树状数组)

KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2603    Accepted Submission(s): 1202 Problem Description For the k-th number, we all should be very familiar with it. Of course,t

HDU 4902 Nice boat(线段树)

HDU Nice boat 题目链接 题意:给定一个序列,两种操作,把一段变成x,把一段每个数字,如果他大于x,就变成他和x的gcd,求变换完后,最后的序列. 思路:线段树,每个结点多一个cover表示该位置以下区间是否数字全相同,然后每次延迟操作,最后输出的时候单点查询即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 1

hdu 2795 Billboard(线段树)

Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10890    Accepted Submission(s): 4827 Problem Description At the entrance to the university, there is a huge rectangular billboard of

hdu 3016 Man Down (线段树 + dp)

题目大意: 是男人就下一般层...没什么可以多说的吧. 注意只能垂直下落. 思路分析: 后面求最大值的过程很容易想到是一个dp的过程 . 因为每一个plane 都只能从左边 从右边下两种状态. 然后我们所需要处理的问题就是 ,你如何能快速知道往左边下到哪里,往右边下到哪里. 这就是线段树的预处理. 讲线段按照高度排序. 然后按照高度从小到大加入到树中. 然后去寻找左端点 和 右端点最近覆盖的线段的编号. #include <cstdio> #include <iostream> #

HDU 4417 Super Mario (树状数组/线段树)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble agai