【MD-80】【hdu】Kth number

Kth number

Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12220 Accepted Submission(s): 3709

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

Source
HDU男生專場公開賽——趕在女生之前先過節(From WHU)

Recommend
zty | We have carefully selected several similar problems for you: 2660 2662 1166 1754 2667

題解

在主席生日後不久,我們就收到了主席樹的作業

google翻譯:給你一個序列,並問你第k個大數字的一個。
題意:從小到大排序後第k個數
SMG!!!
然後主席樹對於每一個操作都會新開一棵值域線段樹樹,並把沒有改變的結點連到前一棵樹上去
這樣算來空間複雜度O(n*logINF)
轟!
所以要離散化(喵喵喵)
於是變爲O(nlogn)
ps:把多組數據去掉可以交poj2104

ac代碼

#include<cstdio>
#include<iostream>
#include<algorithm>
#define N 100010
#define INF 2147483647
using namespace std;
int t[N*20],num,lc[N*20],rc[N*20],n,m,p,q,o,T,rt[N],b[N];
struct Node{
    int v,id;
}a[N];
bool cmp(Node p,Node q){
    return p.v<q.v;
}
void Insert(int& x,int y,int l,int r){
    x=++num;
    t[x]=t[y]+1;
    lc[x]=lc[y];
    rc[x]=rc[y];
    if(l==r)return ;
    int mid=(l+r)>>1;
    if(p<=mid)Insert(lc[x],lc[y],l,mid);
    else Insert(rc[x],rc[y],mid+1,r);
}
int Query(int x,int y,int l,int r,int k){
    if(l==r)return a[l].v;
    int mid=(l+r)>>1;
    int d=t[lc[x]]-t[lc[y]];
    if(k<=d)return Query(lc[x],lc[y],l,mid,k);
    else return Query(rc[x],rc[y],mid+1,r,k-d);
}
int main(){
    freopen("data.txt","r",stdin);
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        num=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i].v);
            a[i].id=i;
        }
        sort(a+1,a+n+1,cmp);
        for(int i=1;i<=n;i++)b[a[i].id]=i;
        for(int i=1;i<=n;i++){
            p=b[i];
            Insert(rt[i],rt[i-1],1,n);
        }
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&p,&q,&o);
            printf("%d\n",Query(rt[q],rt[p-1],1,n,o));
        }
    }
}

對拍

#include<cstdio>
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#define MOD 100010
using namespace std;
int n,a[MOD+10],m,p,q,T;
int main(){
    freopen("data.txt","w",stdout);
    srand((unsigned)time(NULL));
    T=n=rand()%MOD+1;
    printf("%d\n",T);
    while(T--){
        n=rand()%MOD+1;
        m=rand()%MOD+1;
        printf("%d %d\n",n,m);
        for(int i=1;i<=n;i++)a[i]=rand()%MOD+1;
        for(int i=1;i<=n;i++)printf("%d ",a[i]);
        printf("\n");
        for(int i=1;i<=m;i++){
            while((p=rand()%n+1)>(q=rand()%n+1));
            printf("%d %d %d\n",p,q,rand()%(q-p+1)+1);
        }
    }
}

暴力

sort大暴力

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define N 200010
using namespace std;
int a[N],n,m,p,q,o,b[N],T;
bool cmp(int p,int q){
    return p>q;
}
int main(){
    freopen("data.txt","r",stdin);
    freopen("2.txt","w",stdout);
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        memcpy(b,a,sizeof(a));
        for(int i=1;i<=m;i++){
            memcpy(b,a,sizeof(a));
            scanf("%d%d%d",&p,&q,&o);
            sort(b+p,b+q+1,cmp);
            printf("%d\n",b[p+o-1]);
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章