HDU 4217 Data Structure?
2012-09-12 13:00
363 查看
题意: 有 n 个数,从做到到右依次为1..n,有m 组操作,每次输入一个值k,讲序列中的第k大元素取出,问最后取到的数字的和为多少。
分析: 线段树,num[] 表示该区间有多少个数。
分析: 线段树,num[] 表示该区间有多少个数。
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define clr(x)memset(x,0,sizeof(x)) #define maxn 270000 int num[maxn<<3]; void creat(int l,int r,int rt) { if(l==r) { num[rt]=1; return; } int mid=(l+r)>>1; creat(l,mid,rt<<1); creat(mid+1,r,rt<<1|1); num[rt]=num[rt<<1]+num[rt<<1|1]; } int query(int k,int l,int r,int rt) { if(l==r) { num[rt]=0; return l; } int mid=(l+r)>>1; int res; if(num[rt<<1]<k) res=query(k-num[rt<<1],mid+1,r,rt<<1|1); else res=query(k,l,mid,rt<<1); num[rt]=num[rt<<1]+num[rt<<1|1]; return res; } int a[maxn]; int main() { int t,n,m,i,ca=1; __int64 ans; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); ans=0; creat(1,n,1); for(i=1;i<=n;i++) a[i]=i; while(m--) { int k; scanf("%d",&k); k=query(k,1,n,1); ans+=a[k]; } printf("Case %d: %I64d\n",ca++,ans); } return 0; }
相关文章推荐
- HDU--4217 -- Data Structure? [线段树]
- hdu 题目4217 Data Structure?(线段树,单点更新)
- HDU 4217 Data Structure?(树状数组+二分)
- Data Structure_hdu_4217(线段树).java
- HDU 4217 Data Structure?
- Hdu 4217 Data Structure?【二分+树状数组】
- hdu 4217 Data Structure?/SBT
- 线段树 HDU 4217 Data Structure? 单点更新 区间查询
- hdu 4217 Data Structure?
- HDU 4217 Data Structure?(线段树)
- hdu-4217-Data Structure? 线段树
- hdu 4217 Data Structure?(树状数组)
- hdu 4217 Data Structure? treap
- HDU 4217 Data Structure?(线段树 or 树状数组啊)
- 【HDU 4217】【经典题 树状数组求前k大】 Data Structure?【n个数1-n,k次操作,每次取出第ki小的数。问所有取出数字之和。】
- HDU 4217 Data Structure?(线段树版)
- {HDU}{4217}{Data Structure}{树状数组or线段树}
- HDU 4217 Data Structure?
- HDU 4217 Data Structure?
- hdu 4217 Data Structure? 树状数组求第K小