zoukankan      html  css  js  c++  java
  • Luogu3834 【模板】可持久化线段树 1(主席树)

    区间第k大裸题,开始的错误代码竟然A了

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
    #define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
    #define Max(a,b) ((a) > (b) ? (a) : (b))
    #define Min(a,b) ((a) < (b) ? (a) : (b))
    #define Fill(a,b) memset(a, b, sizeof(a))
    #define Abs(a) ((a) < 0 ? -(a) : (a))
    #define Swap(a,b) a^=b^=a^=b
    #define ll long long
    
    //#define ON_DEBUG
    
    #ifdef ON_DEBUG
    
    #define D_e_Line printf("
    
    ----------
    
    ")
    #define D_e(x)  cout << #x << " = " << x << endl
    #define Pause() system("pause")
    #define FileOpen() freopen("in.txt","r",stdin);
    
    #else
    
    #define D_e_Line ;
    #define D_e(x)  ;
    #define Pause() ;
    #define FileOpen() ;
    
    #endif
    
    struct ios{
        template<typename ATP>ios& operator >> (ATP &x){
            x = 0; int f = 1; char c;
            for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
            while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
            x*= f;
            return *this;
        }
    }io;
    using namespace std;
    
    const int N = 200007;
    
    int treeIndex;
    int sum[N<<5], T[N], L[N<<5], R[N<<5];
    
    inline void Build(int &rt, int l, int r){
        rt = ++treeIndex;
        if(l == r) return;
        int mid = (l + r) >> 1;
        Build(L[rt], l, mid), Build(R[rt], mid + 1, r);
    }
    
    inline void Updata(int &root, int rt, int l, int r, int x){
        root = ++treeIndex;
        L[root] = L[rt], R[root] = R[rt];
        sum[root] = sum[rt] + 1;
        if(l == r) return;
        int mid = (l + r) >> 1;
        if(x <= mid)
            Updata(L[root], L[rt], l, mid, x);
        else
            Updata(R[root], R[rt], mid + 1, r, x);
    }
    
    inline int Query(int u, int v, int l, int r, int K){
        if(l == r) return l;
        int mid = (l + r) >> 1, x = sum[L[v]] - sum[L[u]];
        if(x >= K)
            return Query(L[u], L[v], l, mid, K);
        else
            return Query(R[u], R[v], mid + 1, r, K - x);
    }
    
    int a[N], b[N];
    int main(){
        //FileOpen();
        
        int n, Ques;
        io >> n >> Ques;
        R(i,1,n){
        	io >> a[i];
        	b[i] = a[i];
        }
        
        sort(b + 1, b + n + 1);
        int m = unique(b + 1, b + n + 1) - b - 1;
        
        Build(T[0], 1, m);
        R(i,1,n){
        	int pos = lower_bound(b + 1, b + m + 1, a[i]) - b;
        	Updata(T[i], T[i - 1], 1, m, pos);
        }
        while(Ques--){
        	int l, r, K;
        	io >> l >> r >> K;
            printf("%d
    ", b[Query(T[l-1], T[r], 1, m, K)]);
        }
        return 0;
    }
    

  • 相关阅读:
    [MySQL] 联合索引最左前缀原则的原因
    [Go]字符串转int64数值型
    [日常] 浏览器前进后退与数据结构的思想
    [Go] go下实现md5加密
    [PHP] 判断两个数组是否相同
    [Go] goland开启自动格式化和开启go modules
    [PHP] 使用strace排查接口响应速度慢过程
    [Git] git version 2以上git add .和git add -A 一样
    [PHP] 504 Gateway Time-out处理流程
    [GO] go-fly客服系统0.2.2打包编译版下载
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11222978.html
Copyright © 2011-2022 走看看