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<bits/stdc++.h>
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<int,int>
#define MP make_pair typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 1e5+, M = 1e6+, mod = 1e6+, inf = 2e9; int n,a[N],q,san[N],fsan[N],b[N],c;
struct cooltree{
int root[N],l[N*],r[N*],v[N*];
int sz;
void init()
{
memset(root,,sizeof(root));
memset(l,,sizeof(l));
memset(r,,sizeof(r));
memset(v,,sizeof(v));
sz = ;
}
void update(int &k,int ll,int rr,int x)
{
++sz;
l[sz] = l[k];
r[sz] = r[k];
v[sz] = v[k] + ;
k = sz;
if(ll == rr) return ;
if(x <= mid) update(l[k],ll,mid,x);
else update(r[k],mid+,rr,x);
}
int ask(int x,int y,int k)
{
int ll = , rr = c;
x = root[x-], y = root[y];
while(ll != rr)
{
int md = (ll+rr)>>, 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 + ,k-=now;
}
return b[ll];
}
}T;
int main() {
int cas = ;
while(scanf("%d",&n)!=EOF) {
T.init();
for(int i = ; i <= n; ++i) scanf("%d",&a[i]), b[i] = a[i];
sort(b+,b+n+);
c = unique(b+,b+n+) - b - ; for(int i = ; i <= n; ++i) san[i] = lower_bound(b+,b+c+,a[i]) - b;
for(int i = ; i <= n; ++i) T.update(T.root[i] = T.root[i-],,c,san[i]); scanf("%d",&q);
printf("Case %d:\n",cas++);
for(int i = ; 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-]] +)/));
}
}
return ;
}

HDU 4251 The Famous ICPC Team Again 主席树的更多相关文章

  1. HDU 4251 The Famous ICPC Team Again(划分树)

    The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  2. hdu 4251 The Famous ICPC Team Again划分树入门题

    The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  3. HDOJ 4251 The Famous ICPC Team Again

    划分树水题..... The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 3276 ...

  4. 【HDOJ】4251 The Famous ICPC Team Again

    划分树模板题目,主席树也可解.划分树. /* 4251 */ #include <iostream> #include <sstream> #include <strin ...

  5. HDU 4247 A Famous ICPC Team

    Problem Description Mr. B, Mr. G, Mr. M and their coach Professor S are planning their way to Warsaw ...

  6. HDU 4729 An Easy Problem for Elfness 主席树

    题意: 给出一棵树,每条边有一个容量. 有若干次询问:\(S \, T \, K \, A \, B\),求路径\(S \to T\)的最大流量. 有两种方法可以增大流量: 花费\(A\)可以新修一条 ...

  7. HDU - 6601 Keen On Everything But Triangle 主席树

    Keen On Everything But Triangle 感觉最近多校好多主席树的亚子,但是本人菜得很,还没学过主席树,看着队友写题就只能划水,\(WA\)了还不能帮忙\(debug\),所以深 ...

  8. HDU 6621"K-th Closest Distance"(二分+主席树)

    传送门 •题意 有 $m$ 次询问,每次询问求 $n$ 个数中, $[L,R]$ 区间距 $p$ 第 $k$ 近的数与 $p$ 差值的绝对值: •题解 二分答案,假设当前二分的答案为 $x$,那么如何 ...

  9. HDU4251-The Famous ICPC Team Again(划分树)

    Problem Description When Mr. B, Mr. G and Mr. M were preparing for the 2012 ACM-ICPC World Final Con ...

随机推荐

  1. mysql性能优化学习笔记-参数介绍及优化建议

    MySQL服务器参数介绍 mysql参数介绍(客户端中执行),尽量只修改session级别的参数. 全局参数(新连接的session才会生效,原有已经连接的session不生效) set global ...

  2. linux琐碎知识点

    1.awk的使用方式,pattern支持正则表达式 awk 'pattern{action}' {filenames} 其中 pattern 表示 AWK 在数据中查找的内容,而 action 是在找 ...

  3. android打电话的小程序

    主要使用了Intent以及uses-permission标签. call.rar 下载后直接导入项目

  4. ePass.CreateFile

    javascript和vbscript中没有结构体Struct,ePass的ActiveX对象中把各个参数都展开了,官方文档只给出了对应的代码,没有给出相应的数字,示例代码中却都是数字,其VC代码中有 ...

  5. Js 旋转木马 轮播

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. EF没有同步更新(转)

    不知道这算不算一个bug,当你新建一个从数据库生成的edmx时,他能正确的生成所有的tt文件,但是当你从数据库更新表结构时,他不能正确的更新tt文件,以建立Model1.edmx为例,在解决方案中展开 ...

  7. Android Shape自定义纯色圆角按钮

    版权声明:分享技术,传播快乐.如果本博客对你有帮助,请在我的博客首页为我打赏吧! 在Android开发中,为响应美化应用中控件的效果,使用Shape定义图形效果,可以解决图片过多的问题. 首先看一下效 ...

  8. (2016弱校联盟十一专场10.2) A.Nearest Neighbor Search

    题目链接 水题,算一下就行. #include <bits/stdc++.h> using namespace std; typedef long long ll; ll x[],y[], ...

  9. 汉企学习4个半月的target and plan

    我自从大学毕业以后,工作飘忽不定,其中也不乏有我自己的原因.IT是我向往的行业,几经波折,我来到了汉企. 9月4号,算是正式与汉企接触的第一天.在这里,我看到了学员的上进,老师的责任心,让我感受颇深. ...

  10. 第一次点击Div1显示Div2,再次点击Div1的时候又隐藏Div2

    要使用Jquery来实现,记得引用Jquery库哦,代码如下: $(document).ready(function(){ $("#ck1").click(function(){ ...