HDU4417(主席樹計數)

2021-09-13 09:30:33 字數 691 閱讀 7152

解題思路:如果說用排序,那麼複雜度就是o(mnlogn)會超時。用主席樹的空間複雜度是o(nlogn),時間複雜度是o(max(nlogn,mlogn).這道題稍微修改一下query函式即可。

#include#include#include#include#define n 100009

using namespace std;

struct node

s[n*20];

struct node2

a[n];

int rnk[n],rt[n],cnt,b[n];

bool cmp(node2 a,node2 b)

void insert(int num,int &x,int l,int r)

int query(int u,int v,int w,int l,int r)

int k=s[s[v].l].num-s[s[u].l].num;

int mid=(l+r)/2;

if(midelse }

int main()

sort(a+1,a+1+n,cmp);

for(int i=1;i<=n;i++)

cnt=1;

for(int i=1;i<=n;i++)

while(m--)

}return 0;

}

HDU 4417 水題 樹狀陣列

題目大意 給n個數字,m個詢問 每次詢問 l,r 區間的數字,比k小的數字有多少個。下標從0 n 1 直接離線後樹狀陣列,聽說劃分樹也能過 略微更新的我的破爛離散化小板子 include using namespace std define pr x cout x vectormp maxn voi...

hdu 4417 區間內比h小的數 劃分樹

二分查詢最近乙個比h小的數 1 include2 include3 include4 include5 include6 include7 using namespace std 8 define for0n for i 0 i9 define for1n for i 1 i n i 10 defi...

HDU 4417 (二分 區間第k大)

查詢區間 l,r 中有多少數 比給定的 h 小。我們可以這麼想,h 一定會比 區間的第 x 大 第 x 1 小。怎麼確定 x。這是乙個單調的問題,所以二分 區間第 k 大就可以確定了。include include include include using namespace std const ...