HDU 2665(主席树,无修改第k小)
Kth number
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10682 Accepted Submission(s): 3268
For
each test case, the first line contain two integer n and m (n, m <=
100000), indicates the number of integers in the sequence and the number
of the quaere.
The second line contains n integers, describe the sequence.
Each of following m lines contains three integers s, t, k.
[s, t] indicates the interval and k indicates the kth big number in interval [s, t]
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
#define ll long long
#define LL unsigned long long
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=0.0000000001;
const int N=+;
struct node{
int left,right;
int val;
}tree[N*];
int cnt;
int a[N],b[N];
int root[N];
int build(int l,int r){
int pos=++cnt;
tree[pos].val=;
if(l==r)return pos;
int mid=(l+r)>>;
tree[pos].left=build(l,mid);
tree[pos].right=build(mid+,r);
return pos;
}
void update(int pre,int &now,int x,int l,int r){
tree[++cnt]=tree[pre];
now=cnt;
tree[now].val++;
if(l==r)return;
int mid=(l+r)>>;
if(x<=mid){
update(tree[pre].left,tree[now].left,x,l,mid);
}
else{
update(tree[pre].right,tree[now].right,x,mid+,r);
} }
int query(int L,int R,int k,int l,int r){
if(l==r) return l;
int mid=(l+r)>>;
int sum=tree[tree[R].left].val-tree[tree[L].left].val;
if(k<=sum){
return query(tree[L].left,tree[R].left,k,l,mid);
}
else{
return query(tree[L].right,tree[R].right,k-sum,mid+,r);
} }
int main()
{
int t;
scanf("%d",&t);
while(t--){
memset(root,,sizeof(root));
memset(a,,sizeof(a));
memset(b,,sizeof(b));
int n,m;
scanf("%d%d",&n,&m);
cnt=;
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b++n);
int tt = unique(b+,b++n)-b-;
root[0]=build(1,tt);
for(int i=;i<=n;i++) {
a[i]=lower_bound(b+,b++tt,a[i])-b;//二分找到a[i]的位置
update(root[i-],root[i],a[i],,tt);//root[i-1]表示上一个版本的线段树
}
for(int i=;i<=m;i++) {
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
int ans=query(root[l-],root[r],k,,tt);
printf("%d\n", b[ans]);
}
}
return ;
}
HDU 2665(主席树,无修改第k小)的更多相关文章
- POJ 2104 HDU 2665 主席树 解决区间第K大
两道题都是区间第K大询问,数据规模基本相同. 解决这种问题, 可以采用平方划分(块状表)复杂度也可以接受,但是实际表现比主席树差得多. 这里大致讲一下我对主席树的理解. 首先,如果对于某个区间[L,R ...
- Super Mario HDU - 4417 (主席树询问区间比k小的个数)
Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory ...
- HDU 5919 - Sequence II (2016CCPC长春) 主席树 (区间第K小+区间不同值个数)
HDU 5919 题意: 动态处理一个序列的区间问题,对于一个给定序列,每次输入区间的左端点和右端点,输出这个区间中:每个数字第一次出现的位子留下, 输出这些位子中最中间的那个,就是(len+1)/2 ...
- 主席树--动态区间第k小
主席树--动态区间第\(k\)小 模板题在这里洛谷2617. 先对几个问题做一个总结: 阅读本文需要有主席树的基础,也就是通过区间kth的模板题. 静态整体kth: sort一下找第k小,时间复杂度\ ...
- A - 低阶入门膜法 - K-th Number (主席树查询区间第k小)
题目链接:https://cn.vjudge.net/contest/284294#problem/A 题目大意:主席树查询区间第k小. 具体思路:主席树入门. AC代码: #include<i ...
- SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)
COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...
- BZOJ.2588.Count on a tree(主席树 静态树上第k小)
题目链接 /* 序列上的主席树 某点是利用前一个点的根建树 同理 树上的主席树 某个节点可以利用其父节点(is unique)的根建树 排名可以利用树上前缀和求得: 对于(u,v),w=LCA(u,v ...
- SPOJ 10628 Count on a tree(Tarjan离线 | RMQ-ST在线求LCA+主席树求树上第K小)
COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...
- 主席树(区间第k小的数)
题目链接: https://www.luogu.org/problem/P3834 首先要离散化,然后主席树模板. 1 #include<cstdio> 2 #include<cst ...
随机推荐
- Recyclerview点击事件,更新item的UI+更新Recyclerview外的控件
项目中用到了Recyclerview,在第一行代码中学到了一种相对来说简单的点击事件方法,可是这种点击事件是在adapter中写的,没有教怎么更新item的ui和更新Recyclerview之外的控件 ...
- React Native组件间通信
React Native组件间通信 React Native组件的关系有:父子关系.无直接关系.组件间通信主要针对这两类来讨论. 一.父组件和子组件之间通信 父组件向子组件传递消息.数据通过对子组件的 ...
- 利用php生成验证码
<?php /** * php生成验证码 * @param $width 画布宽 * @param $height 画布高 * @param $vcodelen 验证码长度 * @param $ ...
- jQuery——表格添加数据
1.遮罩层宽高100%,position,不占位 2.注册a标签的删除事件,用on()方法,以方法可以动态添加,之前js需要利用冒泡属性(父标签注册事件,子标签冒泡,target===li触发事件) ...
- CSS——font
行高的量取方式: 1.第一行可设置margin-top值.然后将第一文字顶部到第二行文字顶部的值作为行高的值(要注意对齐方式) 2.将 3.电视上 font:12px/1.5//字体12px,行高1. ...
- 实验2 C++数组与指针
一.实验目的: 掌握一维数组和二维数组的定义.赋值和输入输出的方法. 掌握字符数组和字符串函数的使用. 通过实验进一步掌握指针的概念,会定义和使用指针变量. 能正确使用数组的指针和指向数组的指针变量. ...
- 【YOLO】只检测人
一.修改源代码 cfg/coco.data classes= #修改成1 train = /home/pjreddie/data/coco/trainvalno5k.txt valid = coco_ ...
- 12--c完数/最大公约数/最小公倍数/素数/回文数
完数/最大公约数/最小公倍数/素数/回文数 2015-04-08 10:33 296人阅读 评论(0) 收藏 举报 分类: C/C++(60) 哈尔滨工业大学(8) 版权声明:本文为博主原创文章 ...
- codeforces_734C_二分
C. Anton and Making Potions time limit per test 4 seconds memory limit per test 256 megabytes input ...
- eclipse Errors during build
eclipse在运行main方法或者运行ant里的clean方法时,总是会报下面的错,需要点击第二次才能正常运行 今天终于把这个问题解决了,解决方案如下 项目右键,点properties 点击buil ...