ACM基礎訓練題解4301 城市地平線

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   for   

遍曆線段樹   線段樹的插入和查詢

 1 //城市地平線(線段樹) 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<algorithm> 6 #include<cstdio> 7 using namespace std; 8 typedef __int64 LL; 9 struct building{10     LL x1;11     LL x2;12     LL height;13 }line[50000];14 LL node[100000];15 struct tree{16     LL left;17     LL right;18     LL hi;19 }treenode[1000000];20 bool cmp(building a,building b){21     return (a.height<b.height );22 }23 void clear(int n){24     memset(treenode,0,sizeof(treenode));25     memset(node,0,sizeof(node));26     memset(line,0,sizeof(line));27     for(int i=1;i<=n;i++){28         cin>>line[i].x1>>line[i].x2>>line[i].height;29         node[i*2-1]=line[i].x1;30         node[i*2]=line[i].x2;31     }32     sort(line+1,line+n+1,cmp);33     int j=1;34     sort(node+1,node+1+2*n);35     for(int i=1;i<=n*2;i++){36         if(node[j]!=node[i])37          node[++j]=node[i];38     }39     node[0]=j;40     return ;41 }42 void creatree(int ll,int rr,int p){43     treenode[p].left=node[ll];44     treenode[p].right=node[rr];45     if(ll+1==rr)46       return ;47     int mid=(ll+rr)/2;48     creatree(ll,mid,p*2);49     creatree(mid,rr,p*2+1);//***50     return ;51 }52 void insert(LL ileft,LL iright,LL value,int p){53     if(ileft<=treenode[p].left && treenode[p].right<=iright){54         treenode[p].hi=value;55         return ;56     }57     if(treenode[p].hi>0)58       treenode[p*2].hi=treenode[p*2+1].hi=treenode[p].hi;59     treenode[p].hi=-1;60     if(iright>treenode[p*2].right)61       insert(ileft,iright,value,p*2+1);62     if(ileft<treenode[p*2].right)63      insert(ileft,iright,value,p*2);64     return ;65 }66 LL search(int p){67     if(treenode[p].hi>0)68       return (treenode[p].hi*(treenode[p].right-treenode[p].left));69     if(treenode[p].hi==0)70       return 0;71     return (search(p*2)+search(p*2+1));72 }73 int main(){74     int n;75     cin>>n;76     clear(n);77     creatree(1,node[0],1);78     for(int i=1;i<=n;i++)79       insert(line[i].x1,line[i].x2,line[i].height,1);80       cout<<search(1)<<endl;81     return 0;82 }

聯繫我們

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