HDU2665_Kth number

给一个数组,求区间[l,r]中第k大的数。

今天被各种数据结构虐爆了,自己还是需要学习一下函数式线段树的,这个东西好像还挺常用。

函数式线段树的思想是这样的,对于每个时间状态,我们都建立一颗线段树,查询两个状态在某个区间的差的话,我们只要找到两个状态分别对应的点相减即可。

由于每次我使用线段树更新的时候,一路向下,所以我所涉及的更新的节点数量也只有log个,为了不改变原来的状态,可以选择新建这些节点。

这样所有的节点数量也不会超过n*log()个了。

对于此题,按照数组的顺序从左到右依次加入到线段树中,对于每个数组的位置都建立了一颗线段树,那么查找对于区间[l,r]的数字个数,我们只需要沿着两树的根节点一直往下面判断就可以了,每次判断两颗数的左二子数量相差是否大于K即可,也就是对于当前选择左走还是右走了,最终到达的点就是要找的那个第K大值了。

第一次使用 unique()和lower_bound(),内牛满面啊。 T_T !!!!!

召唤代码君:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define maxn 22222222
using namespace std;

int L[maxn],R[maxn],sum[maxn];
int N,n,m,T;
int a[maxn],lisan[maxn],b[maxn],cnt;

void build(int l,int r,int& p)
{
    p=++N; sum[p]=0;
    if (l==r) return;
    int mid=(l+r)>>1;
    build(l,mid,L[p]);
    build(mid+1,r,R[p]);
}

void update(int pre,int& p,int l,int r,int x)
{
    p=++N;
    L[p]=L[pre],R[p]=R[pre],sum[p]=sum[pre]+1;
    if (l==r) return;
    int mid=(l+r)>>1;
    if (x<=mid) update(L[pre],L[p],l,mid,x);
        else update(R[pre],R[p],mid+1,r,x);
}

int query(int u,int v,int l,int r,int k)
{
    if (l==r) return l;
    int mid=(l+r)>>1,num=sum[L[v]]-sum[L[u]];
    if (num>=k) return query(L[u],L[v],l,mid,k);
        else return query(R[u],R[v],mid+1,r,k-num);
}

int main()
{
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d%d",&n,&m);
        N=0;
        for (int i=1; i<=n; i++) scanf("%d",&a[i]),lisan[i]=a[i];
        sort(lisan+1,lisan+1+n);
        cnt=unique(lisan+1,lisan+1+n)-lisan-1;
        build(1,cnt,b[0]);
        for (int i=1; i<=n; i++)
        {
            int tmp=lower_bound(lisan+1,lisan+1+cnt,a[i])-lisan;
            update(b[i-1],b[i],1,cnt,tmp);
        }
        while (m--)
        {
            int l,r,k;
            scanf("%d%d%d",&l,&r,&k);
            int pos=query(b[l-1],b[r],1,cnt,k);
            printf("%d\n",lisan[pos]);
        }
    }
    return 0;
}

HDU2665_Kth number

时间: 2025-01-02 13:46:50

HDU2665_Kth number的相关文章

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

17. Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

实现一个函数clone,使JavaScript中的5种主要的数据类型(包括Number、String、Object、Array、Boolean)进行值复制

实现一个函数clone,可以对JavaScript中的5种主要的数据类型(包括Number.String.Object.Array.Boolean)进行值复制. 1 /** 对象克隆 2 * 支持基本数据类型及对象 3 * 递归方法 */ 4 function clone(obj) { 5 var o; 6 switch (typeof obj) { 7 case "undefined": 8 break; 9 case "string": o = obj + &q

解决sqoop报错Invalid number; item = ITEM_UNICODE

报错栈: java.sql.SQLException: Invalid number; item = ITEM_UNICODE at com.intersys.jdbc.SysList.getInt(SysList.java:1735) at com.intersys.jdbc.CacheResultSet.getInt(CacheResultSet.java:247) at org.apache.sqoop.lib.JdbcWritableBridge.readInteger(JdbcWrit

1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

Minimum Inversion Number 【线段数】

Problem DescriptionThe inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of

171. Excel Sheet Column Number

Excel Sheet Column Number Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 static publi

hdu 5898 odd-even number 数位DP

odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 716    Accepted Submission(s): 385 Problem Description For a number,if the length of continuous odd digits is even and the length

Leetcode 202. Happy Number

202. Happy Number Total Accepted: 78171 Total Submissions: 208635 Difficulty: Easy Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace t