首页 > 代码库 > 划分树模板题
划分树模板题
原题http://acm.hdu.edu.cn/showproblem.php?pid=4251
The Famous ICPC Team Again
Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 822 Accepted Submission(s): 396
Problem Description
When Mr. B, Mr. G and Mr. M were preparing for the 2012 ACM-ICPC World Final Contest, Mr. B had collected a large set of contest problems for their daily training. When they decided to take training, Mr. B would choose one of them from the problem set. All the problems in the problem set had been sorted by their time of publish. Each time Prof. S, their coach, would tell them to choose one problem published within a particular time interval. That is to say, if problems had been sorted in a line, each time they would choose one of them from a specified segment of the line.
Moreover, when collecting the problems, Mr. B had also known an estimation of each problem’s difficultness. When he was asked to choose a problem, if he chose the easiest one, Mr. G would complain that “Hey, what a trivial problem!”; if he chose the hardest one, Mr. M would grumble that it took too much time to finish it. To address this dilemma, Mr. B decided to take the one with the medium difficulty. Therefore, he needed a way to know the median number in the given interval of the sequence.
Moreover, when collecting the problems, Mr. B had also known an estimation of each problem’s difficultness. When he was asked to choose a problem, if he chose the easiest one, Mr. G would complain that “Hey, what a trivial problem!”; if he chose the hardest one, Mr. M would grumble that it took too much time to finish it. To address this dilemma, Mr. B decided to take the one with the medium difficulty. Therefore, he needed a way to know the median number in the given interval of the sequence.
Input
For each test case, the first line contains a single integer n (1 <= n <= 100,000) indicating the total number of problems. The second line contains n integers xi (0 <= xi <= 1,000,000,000), separated by single space, denoting the difficultness of each problem, already sorted by publish time. The next line contains a single integer m (1 <= m <= 100,000), specifying number of queries. Then m lines follow, each line contains a pair of integers, A and B (1 <= A <= B <= n), denoting that Mr. B needed to choose a problem between positions A and B (inclusively, positions are counted from 1). It is guaranteed that the number of items between A and B is odd.
Output
For each query, output a single line containing an integer that denotes the difficultness of the problem that Mr. B should choose.
Sample Input
5 5 3 2 4 1 3 1 3 2 4 3 5 5 10 6 4 8 2 3 1 3 2 4 3 5
Sample Output
Case 1: 3 3 2 Case 2: 6 6 4
#include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <limits.h> #include <ctype.h> #include <algorithm> #include <iostream> #include <math.h> #include <string.h> #include <string> #include <stack> #include <queue> #include <deque> #include <vector> #include <set> #include <map> using namespace std; #define mid ((l+r)>>1) #define maxn 100010 __int64 t[20][maxn],sum[20][maxn]; __int64 a[maxn],as[maxn]; //以下为查找区间第k小划分树 void build(int p,int l,int r) { int lm=0,i,ls=l,rs=mid+1;//lm表示应被放入左子树且与中位数相等的数有多少个,ls为左子树的起始位置,rs为右子树的起始位置 for(i=mid; i>=l; i--) //求lm { if(as[i]==as[mid]) lm++; else break; } for(i=l; i<=r; i++) { if(i==l)//这里要特殊讨论 sum[p][i]=0; else sum[p][i]=sum[p][i-1]; if(t[p][i]==as[mid])//若与中位数相等则判断是否应该被放入左子树 { if(lm) { lm--; sum[p][i]++; t[p+1][ls++]=t[p][i]; } else t[p+1][rs++]=t[p][i]; } else if(t[p][i]<as[mid])//查找区间第K大即为> { sum[p][i]++; t[p+1][ls++]=t[p][i]; } else t[p+1][rs++]=t[p][i]; } if(l==r) return; build(p+1,l,mid); build(p+1,mid+1,r); } int query(int p,int l,int r,int ql,int qr,int k) { int s,ss;//s表示l到ql-1的区间内放入左子树的个数,ss表示区间[ql,qr]被放入左子树的个数 if(l==r)//找到所求的数 return t[p][l]; if(ql==l) s=0,ss=sum[p][qr]; else s=sum[p][ql-1],ss=sum[p][qr]-s; if(k<=ss)//要找的数在左子树中 return query(p+1,l,mid,l+s,l+sum[p][qr]-1,k); else//要找的数在右子树中 return query(p+1,mid+1,r,mid+1-l+ql-s,mid+1-l+qr-sum[p][qr],k-ss); } /*int main(){ int i,n,m,j; scanf("%d%d",&n,&m); for(i=1;i<=n;i++){ scanf("%d",&as[i]); t[0][i] = as[i]; } sort(as+1,as+1+n); build(0,1,n); while(m--){ int l,r,k;; scanf("%d%d%d",&l,&r,&k); int ans = query(0,1,n,l,r,k); printf("%d\n",ans); } return 0; }*/ int main(){ int n,m,i,cas=1; while(~scanf("%d",&n)){ for(i=1;i<=n;i++){ scanf("%I64d",&as[i]); t[0][i] = as[i]; } sort(as,as+1+n); build(0,1,n); scanf("%d",&m); printf("Case %d:\n",cas); while(m--){ int l,r,k; scanf("%d%d",&l,&r); k = (r-l)/2+1; __int64 ans = query(0,1,n,l,r,k); printf("%I64d\n",ans); } cas++; } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。