SPOJ:K-Query Online(归并树)
Given a sequence of n numbers a1, a2, ..., an and a number of k-queries. A k-query is a triple (i, j, k) (1 ≤ i ≤ j ≤ n). For each k-query (i, j, k), you have to return the number of elements greater than k in the subsequence ai, ai+1, ..., aj.
Input
- Line 1: n (1 ≤ n ≤ 30000).
- Line 2: n numbers a1, a2, ..., an (1 ≤ ai ≤ 109).
- Line 3: q (1 ≤ q ≤ 200000), the number of k- queries.
- In the next q lines, each line contains 3 numbers a, b, c representing a k-query. You should do the following:
- i = a xor last_ans
- j = b xor last_ans
- k = c xor last_ans
After that 1 ≤ i ≤ j ≤ n, 1 ≤ k ≤ 109 holds.
Where last_ans = the answer to the last query (for the first query it's 0).
Output
For each k-query (i, j, k), print the number of elements greater than k in the subsequence ai, ai+1, ..., aj in a single line.
Example
Input:
6
8 9 3 5 1 9
5
2 3 5
3 3 7
0 0 11
0 0 2
3 7 4 Output:
1
1
0
0
2
[Edited by EB]
There are invalid queries. Assume the following:
- if i < 1: i = 1
- if j > n: j = n
- if i > j: ans = 0
题意:在线询问区间大于K的个数。
思路:线段树,每个节点加vector排序。
(第一次写归并树,AC on one go!
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
vector<int>v[maxn];
int a[maxn];
void build(int Now,int L,int R)
{
if(L==R) { v[Now].push_back(a[L]); return ;}
int Mid=(L+R)>>;
build(Now<<,L,Mid);
build(Now<<|,Mid+,R);
int i=,j=,L1=v[Now<<].size()-,L2=v[Now<<|].size()-;
while(i<=L1||j<=L2){
while(i<=L1&&j<=L2){
if(v[Now<<][i]<=v[Now<<|][j]) v[Now].push_back(v[Now<<][i++]);
else v[Now].push_back(v[Now<<|][j++]);
}
while(j>L2&&i<=L1) v[Now].push_back(v[Now<<][i++]);
while(i>L1&&j<=L2) v[Now].push_back(v[Now<<|][j++]);
}
}
int query(int Now,int L,int R,int l,int r,int k)
{
if(l<=L&&r>=R) {
int pos=lower_bound(v[Now].begin(),v[Now].end(),k)-v[Now].begin();
return v[Now].size()-pos;
}
int Mid=(L+R)>>,res=;
if(l<=Mid) res+=query(Now<<,L,Mid,l,r,k);
if(r>Mid) res+=query(Now<<|,Mid+,R,l,r,k);
return res;
}
int main()
{
int N,Q,ans=,i,j,k;
scanf("%d",&N);
for(i=;i<=N;i++) scanf("%d",&a[i]);
build(,,N);
scanf("%d",&Q);
while(Q--){
scanf("%d%d%d",&i,&j,&k);
i=i^ans; j=j^ans; k=k^ans;
if(i<) i=; if(j>N) j=N;
if(i>j) ans=;
else ans=query(,,N,i,j,k+);
printf("%d\n",ans);
}
return ;
}
SPOJ:K-Query Online(归并树)的更多相关文章
- POJ 2014.K-th Number 区间第k小 (归并树)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 57543 Accepted: 19893 Ca ...
- 静态区间第k大(归并树)
POJ 2104为例 思想: 利用归并排序的思想: 建树过程和归并排序类似,每个数列都是子树序列的合并与排序. 查询过程,如果所查询区间完全包含在当前区间中,则直接返回当前区间内小于所求数的元素个数, ...
- 求第区间第k大数 TLE归并树
题 给定N个正整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入: 第一行包含两个正整数N.M,分别表示序列的长度和查询的个数. 第二行包含N个正整数,表示这个序列各项的数字. 接下来M ...
- Poj 2104区间第k大(归并树)
题目链接 K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 36890 Accepted: 11860 C ...
- 归并树 划分树 可持久化线段树(主席树) 入门题 hdu 2665
如果题目给出1e5的数据范围,,以前只会用n*log(n)的方法去想 今天学了一下两三种n*n*log(n)的数据结构 他们就是大名鼎鼎的 归并树 划分树 主席树,,,, 首先来说两个问题,,区间第k ...
- POJ 2104 K-th Number(区间第k大数)(平方切割,归并树,划分树)
题目链接: http://poj.org/problem? id=2104 解题思路: 由于查询的个数m非常大.朴素的求法无法在规定时间内求解. 因此应该选用合理的方式维护数据来做到高效地查询. 假设 ...
- [poj2104]kth-number(归并树求区间第k大)
复杂度:$O(nlog^3n)$ #include<cstdio> #include<cstring> #include<algorithm> #include&l ...
- POJ2104 K-th Number(归并树)
平方分割一直TLE,最后用归并树处理过了,使用STL会比较慢. #include<cstdio> #include<iostream> #include<cstdlib& ...
- K-th Number 线段树(归并树)+二分查找
K-th Number 题意:给定一个包含n个不同数的数列a1, a2, ..., an 和m个三元组表示的查询.对于每个查询(i, j, k), 输出ai, ai+1, ... ,aj的升序排列中第 ...
随机推荐
- MySQL的LOOP, LEAVE 和ITERATE语句(类似Continue、Break的写法)
和REPEAT和while语句不同,LOOP.LEAVE.ITERATE更像其他编程语言中的goto语句. LOOP要设定一个label指定循环的开始位置,而LEAVE则像其他语言中的Break会离开 ...
- P1551 亲戚 洛谷
https://www.luogu.org/problem/show?pid=1551 题目背景 若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个 ...
- 大整数类BIGN的设计与实现 C++高精度模板
首先感谢刘汝佳所著的<算法竞赛入门经典>. 众所周知,C++中储存能力最大的unsigned long long 也是有着一个上限,如果我们想计算非常大的整数时,就不知所措了,所以,我写了 ...
- 多个Nginx如何实现集群(没具体方案,只是初步探究)
场景: Nginx+Web服务器可以实现负载均衡,但是一台Nginx也是有限的,如果并非量高的话,在他的上层如何实现负载均衡. 如果是DNS或者CDN的话,建多个机房,势必有多个机房数据同步的问题. ...
- Java服务器获取客户端的ip
原文:http://www.open-open.com/code/view/1454133120089 /** * 获取登录用户IP地址 * * @param request * @return */ ...
- scp操作实例
scp 可用于文件的上传与下载,默认端口号是22,通常我们为了安全起见会将默认端口号修改了,而不去直接使用默认的22端口,以下我们以8888端口为例 目标机器 A :192.168.10.30 目标机 ...
- 50 个 Bootstrap 插件
Bootstrap是快速开发Web应用程序的前端工具包.它是一个CSS和HTML的集合,它使用了最新的浏览器技术,给你的Web开发提供了时尚的版式,表单,buttons,表格,网格系统等等. 本文向你 ...
- chrome插件网站
chrome插件网站 http://chromecj.com/
- weex 引导页(guide)页面
slider 和 indicator 都是 weex 的内置组件,且 indicator 是 slider 的子组件. 1.报错处理 原因解析:indicator 样式页面渲染慢 解决方案:indic ...
- android-problem——remount of /system failed: Read-only file system
adb remount后仍旧不能对system进行读写.需要进行adb disable-verity 在Android6.0 (Android M)userdebug版本上(eng版本不存在该问题), ...