poj2828 線段樹單點更新

2021-08-04 00:12:26 字數 767 閱讀 7107

題目意思:有n個人,乙個乙個的插到隊伍中去,要你求最後的隊伍。

解題思路:最後乙個人想站在**就站在**,所以最後乙個人的位置是固定。後面的根據前的可以確定位置。對於位置其實是前面已經有pos個人了,只是我們是逆排,所以是按空位排。排到空位子區間下標為pos的地方。

#include #include#define lson l,m,rt<<1

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

using namespace std;

const int maxn=2e5;

int sum[maxn<<2],ans[maxn];

typedef struct key

key;

void pushup(int rt)

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

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

build(lson);

build(rson);

pushup(rt);

}void update(int p,int v,int l,int r,int rt)

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

if(sum[rt<<1]>=p)update(p,v,lson); //如果有那麼多位置,進入左子樹

else update(p-sum[rt<<1],v,rson); //減去左子樹的空位置,進右子樹

pushup(rt);

return ;

}int main()

for(i=1;i

poj 2828 線段樹 單點更新

題意 有n個人排隊,每乙個人都有乙個val來對應,每乙個後來人都會插入當前隊伍的某乙個位置pos後面。要求把隊伍最後的狀態輸出。題解 一看到這種題目如果之前做過類似的題目很容易就可以想到要倒序完成 因為這樣每個數插入的位置在當前都是可以確定的,sum記錄當前線段的空位數 include includ...

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 ...