不带修改的主席树

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<map>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=2e5*19;
int vis[maxn];
int a[maxn],b[maxn];
struct node1
{
    int l,r,sum;
}T[maxn];
int cnt;vector<int>v;
int root[300000];
int get(int x)
{
    return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
}
 
void update(int l,int r,int& x,int y,int pos)
{
    T[++cnt]=T[y];T[cnt].sum++;x=cnt;
    if(l==r) return;
    long long mid=(l+r)>>1;
    if(mid>=pos) update(l,mid,T[x].l,T[y].l,pos);
    else update(mid+1,r,T[x].r,T[y].r,pos);
}
 
int query(int l,int r,int x,int y,int pos)
{
    if(l==r) return l;
    long long mid=(r+l)>>1;
    long long sum=T[T[y].l].sum-T[T[x].l].sum;
    if(sum>=pos) return query(l,mid,T[x].l,T[y].l,pos);
    else return query(mid+1,r,T[x].r,T[y].r,pos-sum);
}
int main()
{
    int n,q;
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        v.push_back(a[i]);
    }
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
 
    for(int i=1;i<=n;i++)
    {
        update(1,n,root[i],root[i-1],get(a[i]));
    }
    while(q--)
    {
        int x,y,k;
        scanf("%d%d%d",&x,&y,&k);
        printf("%d\n",v[query(1,n,root[x-1],root[y],k)-1]);
    }
}

  

posted @   Heilce  阅读(162)  评论(0编辑  收藏  举报
编辑推荐:
· Brainfly: 用 C# 类型系统构建 Brainfuck 编译器
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
阅读排行:
· Tinyfox 发生重大改版
· DeepSeek 全面指南,95% 的人都不知道的9个技巧(建议收藏)
· 对比使用DeepSeek与文新一言,了解DeepSeek的关键技术论文
· Brainfly: 用 C# 类型系统构建 Brainfuck 编译器
· DeepSeekV3+Roo Code,智能编码好助手
点击右上角即可分享
微信分享提示