http://poj.org/problem?id=2828線段樹 逆序

來源:互聯網
上載者:User

尼瑪線段樹終於又有點進展了,果斷的難,自己一直不知道在具體的問題中用線段樹記錄什麼內容,看來還是做過的題目太少了,見過的類型很少,以後要努力了,這次必須把線段樹專輯搞完在回家。。。。弱爆了。

這題剛開始看時完全不知道線段樹記錄什麼內容,如何用線段樹表示,更不知道如何下手了;後來看了別人的解題報告理解了好長時間,才明白,真心要努力了,多思考;

由於這題是插隊的問題,要等所有資訊輸入完才知道最終結果,用線段樹表示他所記錄的區間還有多少空位,然後從最後一個開始插入;

#include<cstdio>#include<algorithm>#include<cmath>#include<string.h>using namespace std;#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1 | 1const int N=200002;int num[N<<2];int res[N<<2];void PushUp(int rt){ num[rt]=num[rt<<1]+num[rt<<1 | 1];}void build(int l,int r,int rt){  res[rt]=0;   if(l==r){num[rt]=1;return;}   int mid=(l+r)>>1;  build(lson);  build(rson);   PushUp(rt);}void update(int p,int val,int l,int r,int rt){    if(l==r)    {num[rt]--;     res[l]=val;return;    }    int mid=(l+r)>>1;    if(num[rt<<1]>=p)update(p,val,lson);    else update(p-num[rt<<1],val,rson);   PushUp(rt);}pair<int,int>a[N];int main(){ int n,i,val;  while(~scanf("%d",&n))  {     build(1,n,1);     for(int i=0;i<n;i++)     {      scanf("%d%d",&a[i].first,&a[i].second);     } for(int i=n-1;i>=0;i--)        {         update(a[i].first+1,a[i].second,1,n,1);        }       printf("%d",res[1]);     for(int i=2;i<=n;i++)      { printf(" %d",res[i]);}      printf("\n");  } return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.