poj 2828 線段樹 單點更新

2021-07-12 05:28:12 字數 814 閱讀 5891

題意:有n個人排隊,每乙個人都有乙個val來對應,每乙個後來人都會插入當前隊伍的某乙個位置pos後面。要求把隊伍最後的狀態輸出。

題解:一看到這種題目如果之前做過類似的題目很容易就可以想到要倒序完成 因為這樣每個數插入的位置在當前都是可以確定的,sum記錄當前線段的空位數

#include#includeusing namespace std;

#define lson l,mid,rt<<1

#define rson mid+1,r,rt<<1|1

const int n=200000;

int x[n],y[n],res[n],sum[n<<2];

void build(int l,int r,int rt)

sum[rt]=r-l+1;

int mid=(l+r)>>1;

build(lson);

build(rson);

}int query(int p,int l,int r,int rt)

int pos,mid=(l+r)>>1;

if(sum[rt<<1]>=p) pos=query(p,lson);

else pos=query(p-sum[rt<<1],rson);

sum[rt]=sum[rt<<1]+sum[rt<<1|1];

return pos;

}int main()

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

printf("%d%c",res[i],i==n?'\n':' ');

}return 0;

}

poj2828 線段樹單點更新

題目意思 有n個人,乙個乙個的插到隊伍中去,要你求最後的隊伍。解題思路 最後乙個人想站在 就站在 所以最後乙個人的位置是固定。後面的根據前的可以確定位置。對於位置其實是前面已經有pos個人了,只是我們是逆排,所以是按空位排。排到空位子區間下標為pos的地方。include include defin...

poj2828之線段樹單點更新

include include include include include include include include include define inf 99999999 using namespace std const int max 200000 10 int sum max 2 ...

poj 2828(線段樹的單點更新)

poj 2828 1 思路 一開始想用鍊錶做,但是鍊錶無法處理第幾個這個問題。後來參考大佬的文章,發現先進入的人之前的空格數是確定的就是id 1,所以我們可以建立線段樹,找到這個點的位置,然後更新這個點。2 注意 更新時,一定先比較左區間,否則在右區間,但是要id 1變為 id 1 tree rt ...