博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4251 The Famous ICPC Team Again 主席树
阅读量:4585 次
发布时间:2019-06-09

本文共 4123 字,大约阅读时间需要 13 分钟。

The Famous ICPC Team Again

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.
 

 

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
 

 

题意:
  给你n个数,m个询问
  每次问你l,r这个区间内的 中位数是多少
题解
  离散化
  再套可持久化线段树即可
#include
using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")#define ls i<<1#define rs ls | 1#define mid ((ll+rr)>>1)#define pii pair
#define MP make_pairtypedef long long LL;const long long INF = 1e18;const double Pi = acos(-1.0);const int N = 1e5+10, M = 1e6+11, mod = 1e6+3, inf = 2e9;int n,a[N],q,san[N],fsan[N],b[N],c;struct cooltree{ int root[N],l[N*20],r[N*20],v[N*20]; int sz; void init() { memset(root,0,sizeof(root)); memset(l,0,sizeof(l)); memset(r,0,sizeof(r)); memset(v,0,sizeof(v)); sz = 0; } void update(int &k,int ll,int rr,int x) { ++sz; l[sz] = l[k]; r[sz] = r[k]; v[sz] = v[k] + 1; k = sz; if(ll == rr) return ; if(x <= mid) update(l[k],ll,mid,x); else update(r[k],mid+1,rr,x); } int ask(int x,int y,int k) { int ll = 1, rr = c; x = root[x-1], y = root[y]; while(ll != rr) { int md = (ll+rr)>>1, now = v[l[y]] - v[l[x]]; if(k <= now) x = l[x], y = l[y], rr = md; else x = r[x], y = r[y], ll = mid + 1,k-=now; } return b[ll]; }}T;int main() { int cas = 1; while(scanf("%d",&n)!=EOF) { T.init(); for(int i = 1; i <= n; ++i) scanf("%d",&a[i]), b[i] = a[i]; sort(b+1,b+n+1); c = unique(b+1,b+n+1) - b - 1; for(int i = 1; i <= n; ++i) san[i] = lower_bound(b+1,b+c+1,a[i]) - b; for(int i = 1; i <= n; ++i) T.update(T.root[i] = T.root[i-1],1,c,san[i]); scanf("%d",&q); printf("Case %d:\n",cas++); for(int i = 1; i <= q; ++i) { int x,y; scanf("%d%d",&x,&y); printf("%d\n",T.ask(x,y,(T.v[T.root[y]] - T.v[T.root[x-1]] +1)/2)); } } return 0;}

 

转载于:https://www.cnblogs.com/zxhl/p/5835405.html

你可能感兴趣的文章
php中curl的详细解说【转】
查看>>
Codeforces Round #281 (Div. 2) C. Vasya and Basketball 二分
查看>>
hdu 6069 Counting Divisors 筛法
查看>>
codeforces gym 100971 K Palindromization 思路
查看>>
各个控件说明
查看>>
鼠标事件(jQuery)
查看>>
delete指针时coredump的分析之旅
查看>>
openoffice+pdf2swf+FlexPaper在线显示office和pdf
查看>>
二十九、简谈设计模式
查看>>
js中数组的检测方法
查看>>
[译]GotW #6a: Const-Correctness, Part 1
查看>>
JAVA基础学习之 Map集合、集合框架工具类Collections,Arrays、可变参数、List和Set集合框架什么时候使用等(4)...
查看>>
用Python学分析 - 单因素方差分析
查看>>
2018个人年终总结
查看>>
[编辑排版]小技巧---markdown 转 richText
查看>>
JSON_UNESCAPED_UNICODE
查看>>
bug解决思路
查看>>
Oracle没有WM_CONCAT函数的解决办法
查看>>
消息中间件——RabbitMQ(四)命令行与管控台的基本操作!
查看>>
Eclipse 写代码是自动重启服务
查看>>