标签:
Description
Input
Output
Sample Input
2 3 2 1 1 10 3 3 9 1
Sample Output
Case 1: 3 Case 2: 14
然后是通过线段树的标记功能
/* 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; }
标签:
原文地址:http://blog.csdn.net/qq_18661257/article/details/46468459