HDU_2665 Kth number[可持续化线段树]

来源:互联网 发布:淘宝数字油画 编辑:程序博客网 时间:2024/05/17 08:54

传送门:HDU_2665

Kth number

Problem Description
Give you a sequence and ask you the kth big number of a inteval.
 

Input
The first line is the number of the test cases.
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]
 

Output
For each test case, output m lines. Each line contains the kth big number.
 

Sample Input
1 10 1 1 4 2 3 5 6 7 8 9 0 1 3 2
 

Sample Output
2

题意:求区间第K大树。

思路:可持续化线段树。


代码:

/************************************************* Author: Ac_sorry* File:* Create Date:* Motto: One heart One life* CSDN: http://blog.csdn.net/code_or_code*************************************************/#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<cstdlib>#include<vector>#include<string>#include<map>#include<utility>#include<queue>#include<stack>#define INF 0x3f3f3f3f#define MOD 1000000007#define seed_ 131#define eps 1e-8#define mem(a,b) memset(a,b,sizeof a)#define w(p) tree[p].w#define ls(p) tree[p].ls#define rs(p) tree[p].rsusing namespace std;typedef long long LL;const int N=100010;int a[N],b[N];int root[N],sz;struct node{    int ls,rs;    int w;    node(){ls=rs=w=0;}}tree[N*20];void update(int &i,int l,int r,int x){    tree[++sz]=tree[i];i=sz;    w(i)++;    if(l==r) return;    int m=(l+r)>>1;    if(x<=m) update(ls(i),l,m,x);    else update(rs(i),m+1,r,x);}int query(int i,int j,int l,int r,int k){    if(l==r) return l;    int t=w(ls(j))-w(ls(i));    int m=(l+r)>>1;    if(t>=k) return query(ls(i),ls(j),l,m,k);    else return query(rs(i),rs(j),m+1,r,k-t);}int main(){    int T_;scanf("%d",&T_);    //int AC=0;    while(T_--)    {        root[0]=0;        sz=0;        int n,m;        scanf("%d%d",&n,&m);        for(int i=1;i<=n;i++)        {            scanf("%d",a+i);            b[i]=a[i];        }        sort(b+1,b+1+n);        int cnt=unique(b+1,b+1+n)-(b+1);        int mpp;        for(int i=1;i<=n;i++)        {            root[i]=root[i-1];            mpp=lower_bound(b+1,b+1+cnt,a[i])-b;            update(root[i],1,cnt,mpp);        }        int l,r,k;        for(int i=0;i<m;i++)        {            scanf("%d%d%d",&l,&r,&k);            int pos=query(root[l-1],root[r],1,cnt,k);            printf("%d\n",b[pos]);        }    }    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 obs直播开摄像头吃鸡掉帧怎么办 陌陌收到的礼物怎么办 吃了油腻的东西恶心怎么办 主播工资不发怎么办 主播工资被欠怎么办 直播平台不发工资坑主播怎么办 主播公司不发工资怎么办 梦幻月卡用完了怎么办 网易星球实名认证通过不了怎么办 认证过荔枝主播怎么办 苹果手机相机不对焦怎么办 苹果手机摄像头不能对焦了怎么办 闪电邮里面邮件太多怎么办 苹果手机和助理打不开怎么办 苹果我的世界打不开怎么办 ps试用7天到期了怎么办 皮肤锁不住水份怎么办 硫酸弄到皮肤上怎么办 直播时图像反看怎么办 快手直播权限被收回怎么办 快手直播权限被收回了怎么办 腾讯手游助手玩游戏卡怎么办 电脑直播视频打不开了怎么办 平板进水开不了机怎么办 苹果平板进水开不了机怎么办 苹果平板电脑进水了怎么办 电脑换主机以前的文件怎么办 货车卖了没过户怎么办 微交易出金不了怎么办 直播时出现央视影音客户端怎么办 qq账号暂时无法登录怎么办 饿了吃东西胃疼怎么办 早上不吃饭胃疼怎么办 孕晚期胃疼呕吐怎么办 胃疼了一晚上怎么办 微信视频图像倒立怎么办 ps链接图层锁定怎么办 慕课过时间了怎么办 异地恋又要考研怎么办 阴阳师手机绑定上限了怎么办 高考口令卡丢了怎么办