劃分樹模板

來源:互聯網
上載者:User

#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;#define MAXN 100001#define L(u) (u<<1)#define R(u) (u<<1|1)#define MID(l, r) ((l+r)>>1)struct SegTree{    int l, r;} node[MAXN*4];int sortA[MAXN];int toLeft[20][MAXN]; //toLeft[i]表示[node[u].l, i]地區裡有多少個數分到左子樹中int val[20][MAXN];void build(int u, int l, int r, int h){node[u].l = l;node[u].r = r;if(node[u].l == node[u].r)return;int mid = MID(l, r);int eqlToLeft = mid - l + 1;  //eqlToLeft統計區間[l,r]中與中位元相等且分到左子樹中的數的個數for(int i = l; i <= r ; i++)if(val[h][i] < sortA[mid])eqlToLeft--;  //先假設左子樹中的(mid-l+1)個數都等於中位元,然後把實際上小於中位元的減去int lpos = l;int rpos = mid + 1;int cnt = 0;  //統計已經進入左子樹的數個數(對於所有等於中位元的數)for(int i = l ; i <= r ; i++)    {if(i == l)toLeft[h][i] = 0;elsetoLeft[h][i] = toLeft[h][i-1];if(val[h][i] < sortA[mid]){toLeft[h][i]++;val[h+1][lpos++] = val[h][i];}        else if(val[h][i] > sortA[mid])val[h+1][rpos++] = val[h][i];else        {            //對於等於中位元的數,一部分分到左子樹,一部分分到右子樹if(cnt < eqlToLeft)            {cnt++;toLeft[h][i]++;val[h+1][lpos++] = val[h][i];}            elseval[h+1][rpos++] = val[h][i];}}build(L(u), l, mid, h + 1);build(R(u), mid + 1, r, h + 1);}int query(int u, int l, int r, int h, int k){if(l == r) return val[h][l];int cnt1;  //cnt1表示[node[u].l, l-1]有多少個數分到左子樹中int cnt2;  //cnt2表示[l,r]有多少個數分到當前區間的左子樹中//[node[u].l, l-1] + [l,r] = [node[u].l, r]if(l == node[u].l)    {        cnt1 = 0;cnt2 = toLeft[h][r];}else    {        cnt1 = toLeft[h][l-1];cnt2 = toLeft[h][r] - toLeft[h][l-1];}if(cnt2 >= k) //[l,r]區間上有多於k個分到左邊,顯然去左子樹找第k個    {        //計算出新的映射區間,注意:劃分樹上保證下標的順序不變int newl = node[u].l + cnt1; //[node[u].l, l-1]int newr = node[u].l + cnt1 + cnt2 - 1; //[l,r]return query(L(u), newl, newr, h + 1, k);}else    {int mid = MID(node[u].l, node[u].r);int cnt3 = l - node[u].l - cnt1;  //cnt3記錄node[u].l, l-1]有多少個分到右子樹中int cnt4 = r - l + 1 - cnt2;  //cnt4記錄[l,r]有多少個分到右子樹中int newl = mid + cnt3 + 1;int newr = mid + cnt3 + cnt4;return query(R(u), newl, newr, h+1, k - cnt2);}}int main(){    int n, m;    while(scanf("%d%d",&n,&m) != EOF)    {        for(int i = 1; i <= n; i++)        {            scanf("%d",&val[0][i]);            sortA[i] = val[0][i];        }        sort(sortA + 1, sortA + 1 + n);        build(1, 1, n, 0);        int l, r, k;        while(m--)        {            scanf("%d%d%d",&l,&r,&k);            int ret = query(1, l, r, 0, k);            printf("%d\n",ret);        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.