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(归并树)的更多相关文章

  1. POJ 2014.K-th Number 区间第k小 (归并树)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 57543   Accepted: 19893 Ca ...

  2. 静态区间第k大(归并树)

    POJ 2104为例 思想: 利用归并排序的思想: 建树过程和归并排序类似,每个数列都是子树序列的合并与排序. 查询过程,如果所查询区间完全包含在当前区间中,则直接返回当前区间内小于所求数的元素个数, ...

  3. 求第区间第k大数 TLE归并树

    题 给定N个正整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入: 第一行包含两个正整数N.M,分别表示序列的长度和查询的个数. 第二行包含N个正整数,表示这个序列各项的数字. 接下来M ...

  4. Poj 2104区间第k大(归并树)

    题目链接 K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 36890 Accepted: 11860 C ...

  5. 归并树 划分树 可持久化线段树(主席树) 入门题 hdu 2665

    如果题目给出1e5的数据范围,,以前只会用n*log(n)的方法去想 今天学了一下两三种n*n*log(n)的数据结构 他们就是大名鼎鼎的 归并树 划分树 主席树,,,, 首先来说两个问题,,区间第k ...

  6. POJ 2104 K-th Number(区间第k大数)(平方切割,归并树,划分树)

    题目链接: http://poj.org/problem? id=2104 解题思路: 由于查询的个数m非常大.朴素的求法无法在规定时间内求解. 因此应该选用合理的方式维护数据来做到高效地查询. 假设 ...

  7. [poj2104]kth-number(归并树求区间第k大)

    复杂度:$O(nlog^3n)$ #include<cstdio> #include<cstring> #include<algorithm> #include&l ...

  8. POJ2104 K-th Number(归并树)

    平方分割一直TLE,最后用归并树处理过了,使用STL会比较慢. #include<cstdio> #include<iostream> #include<cstdlib& ...

  9. K-th Number 线段树(归并树)+二分查找

    K-th Number 题意:给定一个包含n个不同数的数列a1, a2, ..., an 和m个三元组表示的查询.对于每个查询(i, j, k), 输出ai, ai+1, ... ,aj的升序排列中第 ...

随机推荐

  1. Raft算法详解

    一致性算法Raft详解 背景 熟悉或了解分布性系统的开发者都知道一致性算法的重要性,Paxos一致性算法从90年提出到现在已经有二十几年了,而Paxos流程太过于繁杂实现起来也比较复杂,可能也是以为过 ...

  2. 打印报表以显示具有给定责任的用户-FNDSCRUR责任用户

    select --&p_hint         distinct         user_name,         decode (            greatest (u.sta ...

  3. 【spring data jpa】报错如下:Caused by: javax.persistence.EntityNotFoundException: Unable to find com.rollong.chinatower.server.persistence.entity.staff.Department with id 0

    报错如下: org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.rollong.chi ...

  4. c++引用和const 用法 数组 指针

    非const引用,只能用object为其赋值: <c++primer>P52 而const引用则可以用临时变量为其赋值: 如: const int &r = 32://可以 int ...

  5. 上篇:es5、es6、es7中的异步写法

    本作品采用知识共享署名 4.0 国际许可协议进行许可.转载联系作者并保留声明头部与原文链接https://luzeshu.com/blog/es-async 本博客同步在http://www.cnbl ...

  6. 紫书p199 八数码(BFS,hash)

    八数码问题  紫书上的简单搜索  渣渣好久才弄懂 #include<cstdio> #include<cstring> using namespace std; const i ...

  7. C语言之基本算法34—分解质因数(方法一)

    //矩阵基础 /* ================================================================== 题目:输入一个正整数.将其分解为质因式,如:6 ...

  8. [Python-MATLAB] 在Python中调用MATLAB的API

    可以参考官方的说明文档: http://cn.mathworks.com/help/matlab/matlab_external/get-started-with-matlab-engine-for- ...

  9. jquery事件手冊

    方法 描写叙述 bind() 向匹配元素附加一个或很多其它事件处理器 blur() 触发.或将函数绑定到指定元素的 blur 事件 change() 触发.或将函数绑定到指定元素的 change 事件 ...

  10. (转载)解决MySql 数据库 提示:1045 access denied for user 'root'@'localhost' using password yes

    今天想用用MySQL 数据库  谁知道老提示 1045 access denied for user 'root'@'localhost' using password yes 最后在csdn 上找到 ...