求區間第k大(小)的數
1175 區間中第K大的數
題目連結:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1175&judgeId=649231
主席樹與普通線段樹的建樹方法有點不同,他只儲存他的左兒子和右兒子,以及這條路徑覆蓋了多少次
比如要找
1 2 3 3 4 5
這6個數[2,4]中第3大的數
先離散化,就只有5個數了,然後建一顆空的線段樹,每條路徑都是0,然後依次把 1 2 3 3 4 5 加進去,每加一個數,就多加線段樹上的一條路徑
①這是空樹:
②:然後把1加進去
③然後把2加進去
④然後把3加進去
⑤再加一個3進去
⑥把4加進去
⑦把5加進去
假如要求[2,4]這個區間的第3大的數,那麼就是用加入了4個數的線段樹來減去,加入了兩個數的線段樹的前面一個線段樹(類似於字首和那種sum[R]-sum[L-1]),就能得到這段區間有多少數了,並且大小順序是有的,數量也是有的
第4個線段樹最下面一層就是:1 1 2 0 0
第1個線段樹最下面一層就是:1 0 0 0 0
於是減完就是:0 1 2 0 0
我們查詢就是用這個東西來查詢的,之前一直沒理解好~
那麼要求第k小就是求字首和,看多少能達到k,而求第k大就是求字尾和,看多少能達到k
就像這樣,從左邊開始,當框起來的數的和達到了k,那就找到了
同理第k大,就從右邊開始框,框起來的數達到了k,那麼這個位置的數就是第 k大了
#include"bits/stdc++.h"
using namespace std;
const int maxn=5e4+5;
int a[maxn];
int data[maxn];//原始資料
int N,n,tot,Q;
vector<int>Root;//用來儲存加進去的每個數的頭節點是多少
int tree[maxn*30],Ls[maxn*30],Rs[maxn*30];
int Build(int L,int R)//對離散後的n個數先建一個空數
{
int id=++tot;
tree[id]=0;
if(L==R)return id;
int mid=L+R>> 1;
Ls[id]=Build(L,mid);
Rs[id]=Build(mid+1,R);
return id;
}
void Update(int id1,int v)
{
v=lower_bound(a+1,a+1+n,v)-a; //找到這個數排在第幾
int id2=++tot; //新建一個root
tree[id2]=tree[id1]+1;
Root.push_back(id2);
int L=1,R=n;
while(L<R)
{
int mid=L+R>>1;
if(v<=mid) //說明這個數是在左邊
{
R=mid;
Ls[id2]=++tot;
Rs[id2]=Rs[id1];//繼承原來的右兒子
id2=tot;
id1=Ls[id1];//向左走
}
else
{
L=mid+1;
Rs[id2]=++tot;
Ls[id2]=Ls[id1];
id2=tot;
id1=Rs[id1];//向右走
}
tree[id2]=tree[id1]+1;//這條路徑上都加1
}
}
int query(int rt1,int rt2,int k)
{
int id1=Root[rt1];
int id2=Root[rt2];
int L=1,R=n;
while(L<R)
{
int mid=L+R>>1;
//找第k個大的就先忘右邊走,找第k個小的就先往左邊走
int cnt=tree[Rs[id2]]-tree[Rs[id1]];//找第k大的就看Rs的,找第k小的就看Ls的
if(cnt>=k)//右邊有>=k個數
{
L=mid+1;
id1=Rs[id1];
id2=Rs[id2];
}
else
{
k-=cnt;
R=mid;
id1=Ls[id1];
id2=Ls[id2];
}
}
return L;
}
int main()
{
while(cin>>N)
{
Root.resize(1);
tot=0;
for(int i=1; i<=N; i++)
{
cin>>data[i];
a[i]=data[i];
}
sort(a+1,a+1+N);
n=unique(a+1,a+1+N)-(a+1);//離散化
Root[0]=Build(1,n);
for(int i=1;i<=N;i++)Update(Root[i-1],data[i]);
cin>>Q;
for(int i=1;i<=Q;i++)
{
int L,R,K;
cin>>L>>R>>K;
L++,R++;
int pos=query(L-1,R,K);
cout<<a[pos]<<endl;
}
}
}
poj 2104 第k小
只有查詢那裡有點不同
#include"iostream"
#include"vector"
#include"algorithm"
#include"cstdio"
using namespace std;
const int maxn=1e5+5;
int a[maxn];
int data[maxn];//原始資料
int N,n,tot,Q;
vector<int>Root;//用來儲存加進去的每個數的頭節點是多少
int tree[maxn*30],Ls[maxn*30],Rs[maxn*30];
int Build(int L,int R)//對離散後的n個數先建一個空數
{
int id=++tot;
tree[id]=0;
if(L==R)return id;
int mid=L+R>>1;
Ls[id]=Build(L,mid);
Rs[id]=Build(mid+1,R);
return id;
}
void Update(int id1,int v)
{
v=lower_bound(a+1,a+1+n,v)-a; //找到這個數排在第幾
int id2=++tot; //新建一個root
tree[id2]=tree[id1]+1;
Root.push_back(id2);
int L=1,R=n;
while(L<R)
{
int mid=L+R>>1;
if(v<=mid) //說明這個數是在左邊
{
R=mid;
Ls[id2]=++tot;
Rs[id2]=Rs[id1];//繼承原來的右兒子
id2=tot;
id1=Ls[id1];//向左走
}
else
{
L=mid+1;
Rs[id2]=++tot;
Ls[id2]=Ls[id1];
id2=tot;
id1=Rs[id1];//向右走
}
tree[id2]=tree[id1]+1;//這條路徑上都加1
}
}
int query(int rt1,int rt2,int k)
{
int id1=Root[rt1];
int id2=Root[rt2];
int L=1,R=n;
while(L<R)
{
int mid=L+R>>1;
int cnt=tree[Ls[id2]]-tree[Ls[id1]];//找第k小就看左邊
if(cnt>=k)
{
R=mid;
id1=Ls[id1];
id2=Ls[id2];
}
else
{
k-=cnt;
L=mid+1;
id1=Rs[id1];
id2=Rs[id2];
}
}
return L;
}
int main()
{
while(cin>>N>>Q)
{
Root.resize(1);
tot=0;
for(int i=1; i<=N; i++)
{
cin>>data[i];
a[i]=data[i];
}
sort(a+1,a+1+N);
n=unique(a+1,a+1+N)-(a+1);//離散化
Root[0]=Build(1,n);
for(int i=1; i<=N; i++)Update(Root[i-1],data[i]);
for(int i=1; i<=Q; i++)
{
int L,R,K;
cin>>L>>R>>K;
int pos=query(L-1,R,K);
cout<<a[pos]<<endl;
}
}
}
劃分樹:
/*
劃分樹
*/
#include"iostream"
#include"algorithm"
using namespace std;
const int maxn=1e5+5;
int a[maxn];
int tree[30][maxn];
int num[30][maxn];
int N,M;
void print()
{
int NN=1;
while(NN<N)
{
NN<<=1;
for(int i=1;i<=N;i++)cout<<tree[NN-1][i]<<" ";
cout<<"\n";
}
cout<<"\n";
}
void Build(int dep,int L,int R)
{
if(L==R)return ;
int mid=(L+R)>>1;
int same=mid-L+1;
int ls=L,rs=mid+1;
for(int i=L;i<=R;i++)if(tree[dep][i]>a[mid])same--;
for(int i=L;i<=R;i++)
{
if(i==L)num[dep][i]=0;
else num[dep][i]=num[dep][i-1];
if(tree[dep][i]>a[mid])
{
num[dep][i]++;
tree[dep+1][ls++]=tree[dep][i];
}
else if(tree[dep][i]==a[mid]&&same)
{
same--;
num[dep][i]++;
tree[dep+1][ls++]=tree[dep][i];
}
else tree[dep+1][rs++]=tree[dep][i];
}
Build(dep+1,L,mid);
Build(dep+1,mid+1,R);
}
int Query(int dep,int ql,int qr,int L,int R,int K)
{
if(L==R)return tree[dep][L];
int mid=(L+R)>>1;
int c1,c2;
if(ql==L)
{
c1=0;
c2=num[dep][qr];
}
else
{
c1=num[dep][ql-1];
c2=num[dep][qr]-c1;
}
if(K<=c2)
{
ql=L+c1;
qr=L+c1+c2-1;//這兒為什麼減1 啊?
return Query(dep+1,ql,qr,L,mid,K);
}
else
{
ql+=mid-L+1 - c1;
qr=mid-L+1 +qr-c1-c2;
return Query(dep+1,ql,qr,mid+1,R,K-c2);
}
}
int main()
{
cin>>N;
for(int i=1;i<=N;i++)
{
cin>>a[i];
tree[0][i]=a[i];
}
sort(a+1,a+1+N,greater<int>());
Build(0,1,N);
cin>>M;
for(int i=1;i<=M;i++)
{
int t1,t2,K;
cin>>t1>>t2>>K;
t1++;
t2++;
cout<<Query(0,t1,t2,1,N,K)<<"\n";
}
}
相關推薦
求區間第k大(小)的數
1175 區間中第K大的數 題目連結:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1175&judgeId=649231 主席樹與普通線段樹的建樹方法有點不同,他只儲存他的
hdu2665 求區間第k大(小?)【主席樹or可持久化線段樹or函式式線段樹】
題目大意:感覺題目表述得不明不白的,給一堆不知道我也不知道什麼資料範圍的數,然後給你M個區間,輸出每個區間的第k大的數(這裡出現嚴重的問題!!!) 題目說得kth bigger 難道不是第k大?結果我WA了一堆之後,翻了幾篇別人的部落格程式碼,結果發現別人
靜態區間內第k大(小)的數
整體二分 //#include<bits/stdc++.h> #include<cstdio> #include<algorithm> #include<iostream> #include<cstring>
求兩個已排序的陣列中所有元素的第K大(小)
1.reference 2.解題思路 以下均假設A[0…m-1],B[0…n-1]; 1)O(n) 假設A,B均以降序排列,宣告兩個指標p,q。p指向A[0], q指向B[0]。再來一個count=0,用來表示當前已經到第count大了。然後指標
Permutation UVA - 11525(值域樹狀數組,樹狀數組區間第k大(離線),log方,log)
一次 跳過 += 數字 div ret num while printf Permutation UVA - 11525 看康托展開 題目給出的式子(n=s[1]*(k-1)!+s[2]*(k-2)!+...+s[k]*0!)非常像逆康托展開(將n個數的所有排列按字典序
區間第k大(靜態)——主席樹
Description 給定一個長度為n的序列,m個詢問,每個詢問的形式為:L,r,k表示在[L,r]間中的第k大元素。 Input 第1行:2個數,n,m表示序列的長度和詢問的個數 第2行:n個數,表示n個數的大小 第3-m+2行:每行3個數,L,r,k表示詢問在[L
POJ2104(主席樹求區間第K大)
模板題,模板來自B站UESTCACM #include <iostream> #include <algorithm> #include <queue> #in
bzoj 1901 動態區間第k大 (樹套樹)
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int Inf
靜態區間第k大(歸併樹)
POJ 2104為例 思想: 利用歸併排序的思想: 建樹過程和歸併排序類似,每個數列都是子樹序列的合併與排序。 查詢過程,如果所查詢區間完全包含在當前區間中,則直接返回當前區間內小於所求數的
區間第k大(4種求法)
線性區間求第k大是一個老生常談的問題,我們來總結下4種求解方法(當然遠不止這4種,老話說思想有多遠就能走多遠)。 這裡我們對每種方法的各種屬性進行一個簡單評級(1-5,沒有任何倍數關係) 1:主席樹 (實現難度:2 時間消耗:2 空間消耗:4 ) 主
HDU2665 求區間第K大 主席樹
題目連結 http://acm.hdu.edu.cn/showproblem.php?pid=2665 程式碼: //#include<bits/stdc++.h> #include<iostream> #include<cmath> #incl
POJ 2104 K-th Number 主席樹(求區間第k大)
主席書資料 題意:給出n個數,m次詢問,[x,y]內第k小的數時多少?n<=1e5,m<=5000 主席樹:對原序列的每個字首i都建立一個線段樹 維護值域[l,r]中的每個數,在字首i的
poj2104 (線段樹求區間第k大)
題目連結: 題意: 給n個數, 每次詢問一個區間, 讓你輸出區間中的第k大的數。 做了這道題,我對線段樹的能解決的問題得認識又有提升。 這個是讓求區間內的第k大, 詢問比較多,暴力顯然不行。 我們想用線段樹解題,那麼線段樹應該儲存什麼呢? 一開始更本沒想到, 看了別人的部落
hdu 5919--Sequence II(主席樹--求區間不同數個數+區間第k大)
positions minus -s ima date rst itl 主席樹 技術 題目鏈接 Problem Description Mr. Frog has an integer sequence of length n, which can be denot
求區間第k小(主席樹)
區間第k小 題目描述 如題,給定NNN個正整數構成的序列,將對於指定的閉區間查詢其區間內的第KKK小值。 輸入格式 第一行包含兩個正整數NNN、MMM,分別表示序列的長度和查詢的個數。 第二行包含NN
主席樹入門詳解一(學習筆記)(例題POJ-2104 求區間第k小)
學習主席樹,在網上搜了很多教程(都好簡短啊,直接就是幾行字就上程式碼,看不懂啊有木有~~),最後才很艱難的學會了最基礎的部分。下面就是我在學習的過程中的產生的疑惑和解決的辦法。 學習主席樹需要的前置技能:線段樹。 參考資料 1. B站上的視訊講解(話說B站真的啥都有啊)
(splay)區間第k大
https://www.luogu.org/problemnew/show/P1801 #include<bits/stdc++.h> using namespace std; const int inf = 2e9 + 50; class node { public:
(區間第k大)黑匣子_NOI導刊2010提高(06)
https://www.luogu.org/problemnew/show/P1801 可以用multiset水過,發現有超多資料結構可以處理這道題。== 線段樹:https://blog.csdn.net/weishengmingerfendou/article/details/47144
區間第k大值(主席樹入門)
區間第k大值(主席樹入門) K-th Number POJ - 2104 You are working for Macrohard company in data structures department. After failing your previous t
POJ 2104 K-th Number(主席樹,區間第K大的數)
Description You are working for Macrohard company in data structures department. After failing your previous task about key insertion you