题干:
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
解题报告:
这题是暑假集训的时候的线段树的题,用线段树维护在某一个区间还剩下几个数。跟这题对比【HDU - 4006】The kth great number
AC代码:
//又忘了开longlong了吧。。。
#include<bits/stdc++.h>
using namespace std;
const int MAX =262144 + 100000;
int n,m;
struct TREE {
int l,r;
int val;
} tree[MAX*4];//数组要不要再大一点?
void pushup(int cur) {
tree[cur].val = tree[cur*2].val+ tree[cur*2+1].val;
}
void build(int l,int r,int cur) {
tree[cur].l = l;
tree[cur].r = r;
if(tree[cur].l == tree[cur].r) {
tree[cur].val = 1;return ;
}
int m = (l + r)/ 2;
build(l,m,cur*2);
build(m+1,r,cur*2+1);
pushup(cur);
}
int query(int k,int cur) {
if(tree[cur].l == tree[cur].r){
// tree[cur].val = 0;
return tree[cur].l;
}
if(k > tree[cur*2].val) return query(k-tree[cur*2].val,cur*2+1);
else return query(k,cur*2);
}
void update(int tar,int cur) {
if(tree[cur].l == tree[cur].r) {
tree[cur].val = 0;
return ;
}
if(tar <= tree[cur*2].r) update(tar,2*cur);
else update(tar,2*cur+1);
pushup(cur);
}
int main()
{
int t,tmp;
int k,iCase = 0;;
long long ans = 0;
cin>>t;
while(t--) {
//初始化
ans = 0;
scanf("%d%d",&n,&m);
build(1,n,1);
while(m--) {
scanf("%d",&k);
tmp = query(k,1);
update(tmp,1);
ans += tmp;
}
printf("Case %d: %lld\n",++iCase,ans);
}
return 0 ;
}