HDU 2217 Data Structure?

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 ",t,res); } return 0; }



原文地址:https://www.cnblogs.com/gcczhongduan/p/5370565.html