记得是上学期的一场比赛的题目。线段树的单点更新,查询的同时更新节点即可。注意数据范围使用 long long
Data Structure?
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1721 Accepted Submission(s): 538
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.
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
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
#include<iostream>
using namespace std;
#define lson l,m,root<<1
#define rson m+1,r,root<<1|1
#define MAXN 362144
int node[MAXN<<2],n,k;
void push_up(int root)
{
node[root]=node[root<<1]+node[root<<1|1];
}
void build(int l,int r ,int root)
{
if(l==r)
{
node[root]=1;
return;
}
int m=(l+r)>>1;
build(lson);
build(rson);
push_up(root);
}
int query(int p,int l,int r,int root)
{
if(l==r)
{
node[root]=0;
return l;
}
int m=(l+r)>>1,ans;
if(node[root<<1]>=p)
ans=query(p,lson);
else
ans=query(p-node[root<<1],rson);
push_up(root);
return ans;
}
int main()
{
int t;
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
scanf("%d%d",&n,&k);
build(1,n,1);
long long ans=0;
int p;
for(int j=1;j<=k;j++)
{
scanf("%d",&p);
ans+=query(p,1,n,1);
}
cout<<"Case "<<i<<": "<<ans<<endl;
}
return 0;
}