HDU 4251 The Famous ICPC Team Again 主席树
The Famous ICPC Team Again
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.
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
3
3
2
Case 2:
6
6
4
#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 主席树的更多相关文章
- HDU 4251 The Famous ICPC Team Again(划分树)
The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 4251 The Famous ICPC Team Again划分树入门题
The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDOJ 4251 The Famous ICPC Team Again
划分树水题..... The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 3276 ...
- 【HDOJ】4251 The Famous ICPC Team Again
划分树模板题目,主席树也可解.划分树. /* 4251 */ #include <iostream> #include <sstream> #include <strin ...
- 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 ...
- HDU 4729 An Easy Problem for Elfness 主席树
题意: 给出一棵树,每条边有一个容量. 有若干次询问:\(S \, T \, K \, A \, B\),求路径\(S \to T\)的最大流量. 有两种方法可以增大流量: 花费\(A\)可以新修一条 ...
- HDU - 6601 Keen On Everything But Triangle 主席树
Keen On Everything But Triangle 感觉最近多校好多主席树的亚子,但是本人菜得很,还没学过主席树,看着队友写题就只能划水,\(WA\)了还不能帮忙\(debug\),所以深 ...
- HDU 6621"K-th Closest Distance"(二分+主席树)
传送门 •题意 有 $m$ 次询问,每次询问求 $n$ 个数中, $[L,R]$ 区间距 $p$ 第 $k$ 近的数与 $p$ 差值的绝对值: •题解 二分答案,假设当前二分的答案为 $x$,那么如何 ...
- 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 ...
随机推荐
- struts2 插件使用
1.引用这个jar包 2.http://localhost:8080/strute2demo/config-browser/actionNames.action
- Python 包管理工具解惑
Python 包管理工具解惑 本文链接:http://zengrong.net/post/2169.htm python packaging 一.困惑 作为一个 Python 初学者,我在包管理上感到 ...
- spring + myBatis 常见错误:SQL语法错误
在程序运行时,有时候会出现如下错误: 这个错误通常是你的sqlmapper.xml中sql语句语法有错误.所以请仔细查看你sql语句是否正确,比如{#id}这样写就会报上述错误,其实应该#{id}这样 ...
- PHP带重试功能的curl
2016年1月13日 10:48:10 星期三 /** * @param string $url 访问链接 * @param string $target 需要重试的标准: 返回结果中是否包含$tar ...
- 日历插件My97DatePicker的使用
在开发过程中,我们会经常遇到让用户输入日期的表单,这类表单处理起来也不是太繁琐,就是简单的字符串和日期之间的转换.但是,如果用户不按照已设定的日期格式进行输入,必定会造成不必要的麻烦.为了更好的处理这 ...
- 复习sqldataread
public static DataTable ConvertDataReaderToDataTable(SqlDataReader dataReader) { ///定义DataTable Data ...
- 用Mybatis返回Map,List<Map>
返回Map,Mybatis配置如下 : <select id="getCountyHashMap" resultType="java.util.HashMap&qu ...
- 【Excel 4.0 函数】REGISTER
REGISTER.ID 返回指定的 DLL 或 代码资源注册过的函数 ID.如果 DLL 或 代码资源没有注册,这个函数将会注册它们,并返回 注册ID. REGISTER.ID 可以用于工作表(不同于 ...
- MFC 使用MFC EditBrowse Control控件选择文件或者文件夹
从工具箱中拖拽一个MFC EditBrowse Control到窗体中, 通过设置“Browse Mode”属性指定“文件浏览”还是“文件夹浏览” 可以通过添加对象的方式将其与一个CString se ...
- apache ab下载测试
http://httpd.apache.org/docs/2.0/programs/ab.html-->http://httpd.apache.org/docs/current/platform ...