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的升序排列中第 ...
随机推荐
- grafana 安装 和 Nginx 、EL 联调
https://blog.csdn.net/u010735147/article/details/80943593
- pandaboard串口通信调试
1.在PC上的pyserial程序,到pandaboard后报错,读取和写入会报错 2.使用的是pandaboard的ttyO2串口 3.ls -l /dev/ttyO2,发现是tty,而不是dial ...
- 2018.11.6 PION 模拟赛
期望:100 + 40 + 50 = 190 实际:60 + 10 + 50 = 120 考得好炸啊!!T1数组开小了炸掉40,T2用 int 读入 long long ,int存储 long lon ...
- java基础之IO流(二)之字符流
java基础之IO流(二)之字符流 字符流,顾名思义,它是以字符为数据处理单元的流对象,那么字符流和字节流之间的关系又是如何呢? 字符流可以理解为是字节流+字符编码集额一种封装与抽象,专门设计用来读写 ...
- configure: error: Cannot find php_pdo_driver.h.
安装pdo_mysql cd /usr/local/src/php-5.4.0/ext/pdo_mysql/ /usr/local/php/bin/phpize # /usr/local/php为 ...
- 第三讲_图像特征与描述Image Feature Descriptor
第三讲_图像特征与描述Image Feature Descriptor 概要 特征提取方法 直方图 对图片数据/特征分布的一种统计:对不同量进行直方图统计:可以表示灰度,颜色,梯度,边缘,形状,纹理, ...
- linux批量解压和批量压缩
ls *.tar.gz | xargs -n1 tar xzvf //批量解压 ls | awk '{ print "tar zcvf "$0".tar.gz " ...
- python中pymysql使用
python中pymysql使用 https://blog.csdn.net/johline/article/details/69549131 import pymysql # 连接数据库 conne ...
- Override和Overload差别,Overloaded的方法能否够改变返回值的类型?
Overload是重载的意思, Override是覆盖的意思,也就是重写. 重载Overload表示同一个类中能够有多个名称同样的方法,但这些方法的參数列表各不同样(即參数个数或类型不同). 重写Ov ...
- 79.iOS 设备的UI规范和iOS各控件默认高度
iOS设备的UI 规范 iPhone界面尺寸 iPhone图标尺寸 iPad的设计尺寸 iPad图标尺寸 iPhone设备尺寸分辨率比例 iPhone各设备 launch image iOS 各种控件 ...