POJ 2828 線段樹應用

來源:互聯網
上載者:User

標籤:

這道題是之前一場比賽碰到的題目,當時看到題時以為是一道用鏈表最佳化的水題,交了幾遍一直逾時,簡直不能再感人(┬_┬)今天有空突然想起去查了下題解,是用線段樹做的。。。。。完全想不到啊有木有~思路大概就是每個節點存當前節點下還剩多少空位,然後倒序尋找該人所需要的位置,具體細節看代碼吧。

Buy Tickets
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 15801   Accepted: 7876

Description

Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue…

The Lunar New Year was approaching, but unluckily the Little Cat still had schedules going here and there. Now, he had to travel by train to Mianyang, Sichuan Province for the winter camp selection of the national team of Olympiad in Informatics.

It was one o’clock a.m. and dark outside. Chill wind from the northwest did not scare off the people in the queue. The cold night gave the Little Cat a shiver. Why not find a problem to think about? That was none the less better than freezing to death!

People kept jumping the queue. Since it was too dark around, such moves would not be discovered even by the people adjacent to the queue-jumpers. “If every person in the queue is assigned an integral value and all the information about those who have jumped the queue and where they stand after queue-jumping is given, can I find out the final order of people in the queue?” Thought the Little Cat.

Input

There will be several test cases in the input. Each test case consists of N + 1 lines where N (1 ≤ N ≤ 200,000) is given in the first line of the test case. The next N lines contain the pairs of values Posi and Vali in the increasing order of i (1 ≤ i ≤ N). For each i, the ranges and meanings of Posi and Vali are as follows:

  • Posi ∈ [0, i − 1] — The i-th person came to the queue and stood right behind the Posi-th person in the queue. The booking office was considered the 0th person and the person at the front of the queue was considered the first person in the queue.
  • Vali ∈ [0, 32767] — The i-th person was assigned the value Vali.

There no blank lines between test cases. Proceed to the end of input.

Output

For each test cases, output a single line of space-separated integers which are the values of people in the order they stand in the queue.

Sample Input

40 771 511 332 6940 205231 192431 38900 31492

Sample Output

77 33 69 5131492 20523 3890 19243
 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 #include<stack> 7 #include<string> 8 using namespace std; 9 #define N 10000510 #define inf 0x7fffffff11 int node[N*8],num[N*2];12 struct ss13 {14     int p,v;15 } pos[N*2];16 void buildtree(int root,int l,int r)17 {18     node[root]=r-l+1;19     if(l==r) return ;20     buildtree(root*2,l,(l+r)/2);21     buildtree(root*2+1,(l+r)/2+1,r);22 }23 int query(int p,int l,int r,int root)24 {25     node[root]--;26     if(l==r)return l;27     int m=(l+r)/2;28     if(node[root*2]>=p)29         return query(p,l,m,root*2);30     else31         return query(p-node[root*2],m+1,r,root*2+1);32 }33 int main()34 {35     int n,i,j;36     while(~scanf("%d",&n))37     {38         for(i=0;i<n;i++)39             scanf("%d%d",&pos[i].p,&pos[i].v);40         buildtree(1,1,n);41         for(i=n-1;i>=0;i--)42             num[query(pos[i].p+1,1,n,1)]=pos[i].v;43         for(i=1;i<n;i++)44             printf("%d ",num[i]);45         printf("%d\n",num[n]);46     }47     return 0;48 }
View Code

 

 

POJ 2828 線段樹應用

聯繫我們

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