Line Segment tree endpoint update poj 2828 buy tickets

Source: Internet
Author: User

 

Question link:

Http://poj.org/problem? Id = 2828

 

Question meaning:

There are n people waiting in queue, each person has a pos value, and a value, which means that he can insert it behind the POs individual and output the final formation sequence.

 

Solution:

If you follow the insert operation, you need to move the following queue. If you use a linked list, it takes a long time to locate the inserted position.

If you think about it, it is very easy to insert the current person into the smallest position with exactly the POs space.

Use the line segment tree to maintain the number of vacancies in the interval. If the required number of empty digits is greater than the number of empty digits in the left interval, subtract the number of empty digits on the left and search for the number on the right.

 

Summary: it is important to abstract the space for maintaining the egress tree.

 

Code:

// If the push is reversed, you do not need to move many elements. This is the key. // use the line segment tree to maintain the empty digits of the interval. If the left empty digits are not enough, select the right (fill the left ), it is critical to find the inserted position // extract the maintenance space from the line segment tree # include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # include <stack> # include <list> # include <queue> # define EPS 1e-6 # define Inf (1 <30) # define PI ACOs (-1.0) using namespace STD; # define lson L, M, RT <1 # define rson m + 1, R, RT <1 | 1 # define maxn 210000int sum [maxn * 4]; int POS [maxn], value [maxn], ANS [maxn], n; int temp; /* freopen ("data. in "," r ", stdin); freopen (" data. out "," W ", stdout); */void build (int l, int R, int RT) {sum [RT] = r-L + 1; // If (r = L) return; int M = (L + r)> 1; build (lson); Build (rson );} void Update (INT target, int L, int R, int RT) {sum [RT] --; // where it is located, the number of null digits is reduced by one, because if (L = r) {temp = L; // You must insert the IF (L = r) {temp = L; // locate the empty position in a certain interval, remember to insert return;} Int M = (L + r)> 1; if (target <= sum [RT <1]) // If the empty digits in the left interval are sufficient, update (target, lson) in the left interval; else Update (Target-sum [RT <1], rson); // If the left interval is insufficient, the left interval must be filled first, enter return;} int main () {While (scanf ("% d", & N) in the right range )! = EOF) {for (INT I = 1; I <= N; I ++) scanf ("% d", & Pos [I], & value [I]); Build (1, n, 1); For (INT I = N; I> = 1; I --) {Update (Pos [I] +, N, 1); ans [temp] = value [I]; // locate and insert} printf ("% d ", ans [1]); For (INT I = 2; I <= N; I ++) printf ("% d", ANS [I]); putchar ('\ n');} return 0 ;}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.