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的升序排列中第 ...
随机推荐
- HDU - 5584 LCM Walk (数论 GCD)
A frog has just learned some number theory, and can't wait to show his ability to his girlfriend. No ...
- codevs——2370 小机房的树
2370 小机房的树 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 小机房有棵焕狗种的树,树上有N个 ...
- synchronized初识
作用域: 1.对象实例内--->People jack = new Jack(); ①此作用域内的synchronized锁 ,可以防止多个线程同时访问这个对象的synchronized方法 ② ...
- Java教程收集
极客学院Wiki离线教程-Java类: 官网:http://wiki.jikexueyuan.com/list/java/ 离线版本:链接:http://pan.baidu.com/s/1pKD2oH ...
- Android ZXing 二维码、条形码扫描介绍
本帖最后由 Shims 于 2013-11-9 12:39 编辑 最近公司的Android项目需要用到摄像头做条码或二维码的扫描,Google一下,发现一个开源的 ZXing项目.它提供二维码和条形码 ...
- Mysql数据库中CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP区别
如图所示,mysql数据库中,当字段类型为timestamp时,如果默认值取CURRENT_TIMESTAMP,则在insert一条记录时,end_time的值自动设置为系统当前时间,如果勾选了 ON ...
- evaluate-reverse-polish-notation——栈
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are+,-,*, ...
- LOCAL_CFLAGS参数说明
1.-Wall 是打开警告开关 2.-O 代表默认优化,可选:-O0不优化,-O1低级优化,-O2中级优化,-O3高级优化,-Os代码空间优化 3.-g 是生成调试信息,生成的可执行文件具有和源代码关 ...
- 线程相关函数(POSIX线程):
创建单个线程 #include <pthread.h> // 若成功返回0,出错返回正的Exxx值 int pthread_create(pthread_t *tid, // 每个线程在进 ...
- DRF框架
1.RESTful规范 1.1 REST风格:表属性状态转移 1.1.1资源:在web中凡是有被引用的必要的都叫资源 1.1.2 URI:统一资源标识符 URI包含URL 1.1.3 URL:统 ...