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-block; position:relative; padding:0px; margin-right:0.1em; vertical-align:middle; overflow:visible; text-decoration:none; font-family:Verdana,Arial,sans-serif; font-size:1em; border:1px solid rgb(211,211,211); color:rgb(85,85,85)">HDU
4217

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;
}
时间: 2024-12-28 12:55:58

HDU 2217 Data Structure?的相关文章

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

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 5929 Basic Data Structure

题目:Basic Data Structure 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5929 题意:t个测试数据,每个数据给出一个m表示操作数量,操作分为4种:PUSH x(x:0或1),POP,REVERSE(翻转栈里面的数),QUERY(假使栈内的数一个个出栈,并按出栈顺序将他们与非起来,问最终结果(注意,并不是真的出栈)) 思路: 做题的时候忘记了与非是按出栈顺序来的,一直按栈底到栈顶的顺序来,WA到死... 根据与非的性质,1.0与非

HDU 5929 Basic Data Structure 模拟

Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Mr. Frog learned a basic data structure recently, which is called stack.There are some basic operations of stack: ? PUSH x: p

What is “passive data structure” in Android/Java?

From the Android developer web link: http://developer.android.com/reference/android/content/Intent.html, you can find that it says "It (Intent) is basically a passive data structure holding an abstract description of an action to be performed."

[LeetCode] 211. Add and Search Word - Data structure design Java

题目: Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one le

170. Two Sum III - Data structure design

Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure.find - Find if there exists any pair of numbers which sum is equal to the value. For example, add(1); ad

LeetCode OJ:Add and Search Word - Data structure design(增加以及搜索单词)

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter

[leedcode 211] Add and Search Word - Data structure design

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter