Poj 2828 Buy Tickets [Single Tree – Single Update] [Data Structure] [Good Top]

Subject link: http://poj.org/problem?id=2828

————————————— —————-.
Buy Tickets
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 18998 Accepted: 9435
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 am 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 adjacen t 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. Proc eed 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

4
0 77
1 51
1 33
2 69
4
0 20523
1 19243
1 3890
0 31492
Sample Output

77 33 69 51
31492 20523 3890 19243
Hint

The figure below shows how the Little Cat found out the final order of people in the queue described in the first test case of the sample input.

Source

POJ Monthly–2006.05.28 , Zhu, Zeyuan
——————————————————.
The main idea:
is a bunch of people lining up to buy tickets, and then every time one person intervenes In a certain position, I now ask you, the order of the last ticket purchase

The idea of ​​solving the problem:
The first thing to be clear is that the maintenance must be reversed. If the order is reversed, it can be determined when you jump in the queue. The final position of

Take the first set of examples,
look in the reverse order, then you must be sure
69 is in the second position
33 is in the first position< br> 77 is at the 0th position

This is certain.

So at this time I am thinking about where 51 should be ranked ,
If you write this, it may be better to understand
77
77 51
77 33 51
77 33 69 51

Think of this as a tree. Inserting in reverse order can directly determine the position of the element. If inserting in positive order, it will also involve traversing all subsequent elements, which is too time-consuming.

Then how to judge the previous element at this position should be Where to put it?
People who think of this are really too witty (now I feel more and more that their IQ is not enough

Use the nodes of the line segment tree to store what this node contains There are still several positions in the interval,
In this case, just insert the xth position left in the tree every time you find it.
If you understand this, it will not be difficult to achieve.

If you don’t understand well, you can read the comments of the code

Attach the code of this question
—————————————.

 //#include #include #include #include #include #include #define abs(x) (((x)>0) ?(x):-(x))#define lalal puts("*********")#define Rep(a,b,c) for(int a=(b);a<=(c);a++)#define Req(a,b,c) for(int a=(b);a>=(c);a--)#define Rop (a,b,c) for(int a=(b);a<(c);a++)#define s1(a) scanf("%d" ,&a)typedef long long span> int LL;using namespace < span class="hljs-built_in">std;const int inf = 0x3f3f3f3f;const int MOD = 9901;/**************************** **********/const int N = 200000+10;str uct node{ int l,r;//interval int val;//Leaf node element int flag;//The number of empty positions in this interval int mid() {return (l+r)>>1; }}tree[N<<2];#define ll (rt<<1)#define rr (rt<<1|1)int a[N],b[N],cnt,ans;void build(int rt,int l,int r){ tree [rt].l=l,tree[rt].r=r; tree[rt].flag=r-l+1; if(l==r) return; int m = tree[rt].mid(); build(ll,l,m); build(rr,m+1 ,r);}void update(int rt,int pos,int val){ tree[rt].flag--;//because To insert a value, there will be one missing place if(tree[rt].l==tree[rt].r) {tree[rt] .val = val; return;} if(tree[ll].flag>=pos) update( ll,pos,val); else update(rr,pos-tree[ll].flag,val);// Because the right son is traversed this time, the pos-tree[ll].flag position is found in the right son}bool fl= 0;void dfs(int rt) //It is to traverse the leaf node output result{ if(tree[rt].l==tree[rt].r) {if(fl) printf(" "); printf("%d",tree[rt].val),fl=true;  return;} dfs(ll); dfs(rr);}int main(){ int n; while(~s1(n)) {fl=false,build(1,1,n); Rep(i,1,n) s1(a[i]),s1(b[i]); Req(i,n,1) update(1,a[i]+1,b[i]);//because The tree is 1~n, so the element position is shifted to the right by one dfs(1),puts("");} return 0;}

Topic link: http://poj.org/problem?id=2828< /p>

——————————————————-.
Buy Tickets
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 18998 Accepted : 9435
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 am 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 i n 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

4
0 77
1 51
1 33
2 69
4
0 20523
1 19243
1 3890
0 31492 < br> Sample Output

77 33 69 51
31492 20523 3890 19243
Hint

The figure below shows how the Little Cat found out the final order of people in the queue described in the first test case of the sample input.

Source

POJ Monthly–2006.05.28, Zhu, Zeyuan
—————— ————————————.
The main idea of ​​the topic:
is a bunch of people lining up to buy tickets, and then one person is inserted in a certain position every time, now I ask you, and finally buy the ticket The order of the question

The idea of ​​solving the problem:
The first thing to be clear is that it must be maintained in reverse order. If the order is reversed, the final position can be determined when jumping in the queue.

Take the first set of examples,
looking in the reverse order, you can be sure
69 is in the second position
33 is in the first position
77 In the 0th position

This is certain.

So at this time, I am thinking about where 51 should be ranked.
If you write this, you may understand some better
77
77 51
77 33 51
77 33 69 51

If you think of this as a tree, insert it in reverse order and you can directly determine the position of the element. If it is positive If you insert in order, it will also involve traversing all subsequent elements, which is too time-consuming.

So how do you judge where the element at this position should be placed before?
People who think of this really Is too witty (now I feel more and more that my IQ is not enough

Use the node of the line segment tree to store the interval contained in this node, and there are several positions,
In this case, as long as Every time you find the xth position left in the tree and insert it into it
Understand this is not difficult to achieve

If you don’t understand, you can look at the code comments

Attach this question code
—————————————.

//#include < bits/stdc++.h>#include #include #include #include  #include #define abs(x) (((x)>0)?(x):-(x))< span class="hljs-preprocessor">#define lalal puts("*********")#define Rep(a,b,c) for(int a=(b);a<=(c);a++)#define Req(a,b,c) for(int a=(b);a>=(c);a--)#define Rop(a,b,c) for(int a=(b);a<(c);a++)#define s1(a) scanf("%d ",&a)typedef long long< /span> int LL;using namespace std;const int inf = 0x3f3f3f3f;const int MOD = 9901;/*************************** ***********/const int N = 200000+10;struct node{ int l,r;//interval int  val;//Leaf node elements int flag;//The number of empty positions in this interval int mid() {return span> (l+r)>>1; }}tree[N<<2];< span class="hljs-preprocessor">#define ll (rt<<1)#define rr (rt<<1|1)int a[N],b[N],cnt,ans;void build(int rt,int l,int r){ tree[rt]. l=l,tree[rt].r=r; tree[rt].flag=r-l+1; if(l==r) return; int m = tree[rt].mid(); build(ll,l,m); build(rr,m+1< /span>,r);}void update(int rt,int pos,int val){ tree[rt].flag--;//Because Insert a value so there will be one less place if(tree[rt].l==tree[rt].r) {tree[rt]. val = val; return;} if(tree[ll].flag>=pos) update(ll ,pos,val); else update(rr,pos-tree[ll].flag,val);//because This time the right son is traversed, so the pos-tree[ll].flag position is found in the right son}bool fl= < span class="hljs-number">0;void dfs(int rt) //It is to traverse the leaf node output result{ if(tree[rt].l==tree[rt].r) {if(fl) printf(" "); printf("%d",tree[rt].val),fl=true; return;} dfs(ll); dfs(rr);}int main(){ int n ; while(~s1(n)) {fl=false,build(1,1,n); Rep(i,1,n ) s1(a[i]),s1(b[i]); Req(i,n,1) update(1,a[i]+1,b[i ]);//Because the creation of the tree is 1~n, the element position is shifted to the right by one dfs(1) ,puts("");} return 0;}

Leave a Comment

Your email address will not be published.