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 ...
随机推荐
- Html5 postMessage
解释: 跨文档消息传输Cross Document Messaging. 编写代码前注意判断浏览器是否支持Html5 实例: b页面向a页面发送消息. <!DOCTYPE> <htm ...
- Selenium WebDriver 处理cookie
在使用webdriver测试中,很多地方都使用登陆,cookie能够实现不必再次输入用户名密码进行登陆. 首先了解一下Java Cookie类的一些方法. 在jsp中处理cookie数据的常用方法: ...
- 关闭window 8.1 的skydrive
gpedit.msc-->计算机配置-->管理模板-->windows组件 -->skydrive-->阻止使用skydrive执行文件存储
- 头文件algorithm中的常用函数
非修改性序列操作(12个) 循环 对序列中的每个元素执行某操作 for_each() 查找 在序列中找出某个值的第一次出现的位置 fin ...
- C# 总复习
1.循环语句 四要素:初始条件.循环条件.循环体.状态改变 循环的最后一句:循环条件不再满足 2. ++ --int a = 5; //在赋值语句中,后++需要,先进性赋值,然后进行+1运算 ...
- UVA 156 Ananagrams ---map
题目链接 题意:输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另外一个单词.在判断是否满足条件时,字母不分大小写,但在输出时应保留输入中的大小写,按字典序进行排列( ...
- IOS - ARC改为非ARC
1.project -> Build settings -> Apple LLVM complier 3.0 - Language -> objective-C Automatic ...
- NodeVisitor的使用-遍历Geode节点下的Geometry并获取顶点、法向量等数据
struct Subset { std::vector<float> vertexs;//位置 std::vector<float> normals;//法向 std::vec ...
- osg 纹理访问器
#include<osgViewer/Viewer> #include<osg/Node>#include<osg/Geode>#include<osg/Gr ...
- balabalabala
[微分享]:种子不落在肥土而落在瓦砾中,有生命力的种子决不会悲观和叹气,因为有了阻力才有磨炼.