POJ 2827 Buy Tickets (queuing problem, line segment tree application), poj2827

Source: Internet
Author: User

POJ 2827 Buy Tickets (queuing problem, line segment tree application), poj2827
POJ 2827 Buy Tickets (queuing problem, line segment tree Application)

ACM

Address: POJ 2827 Buy Tickets

Question:
Line up when buying tickets.
A number pair is given to represent the position Pos_ I and Val_ I of a person respectively, and the val sequence of the final queue is obtained.

Analysis:
It is also a clever question.
At first, I thought it would be nice to use sort. After wa's run, I found that I was wrong...
Originally, we could use a line segment tree. Because a person wants to insert a posi position, he will insert it in front of the posi position, but other people may insert it in front of him, his position will become [behind him and inserted in his position and the previous number of people] + posi.
If this is the case, you can use the line segment tree. This is the same as the reverse order number pair.
However, we can think about it. As long as we start to stand behind and assume that all the people behind us are already in the correct position, when we get there, now there are all the people behind it. As long as there are spaces in posi, the position of the person's station can be determined. After confirmation, you can find the next one. Therefore, both the premise and conclusion are true.

So we only need to stand up from the people behind and count posi spaces.
The line segment tree is the same as the summation line segment tree. It is initialized to 1 during initialization, and can be searched in binary mode to find the desired position. For details, see the code, although the Code is frustrated.
The Code uses an input/output plug-in to increase the speed. You can skip this step without adding a plug-in.

Code:

/** Author: zhangz <iw.zen [at] gmail.com> * Blog: http://blog.csdn.net/hcbbt* File: 2828. cpp * Create Date: 20:16:28 * Descripton: */# include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace std; # define repf (I, a, B) for (int I = (a); I <= (B); I ++) # define lson (x) <1) # define rson (x) <1 | 1) typedef long ll; const int N = 200000; const int ROOT = 1; // B Elow is sement point updated versionstruct seg {ll w ;}; struct segment_tree {seg node [N <2]; void update (int pos) {node [pos]. w = node [lson (pos)]. w + node [rson (pos)]. w;} void build (int l, int r, int pos) {if (l = r) {node [pos]. w = 1; return;} int m = (l + r)> 1; build (l, m, lson (pos); build (m + 1, r, rson (pos); update (pos);} int remove (int l, int r, int pos, ll x) {// Delete and query if (l = r) {node [pos]. w = 0; return l;} int m = (l + r)> 1; int res; if (x <node [lson (pos)]. w) // search for res = remove (l, m, lson (pos), x); elseres = remove (m + 1, r, rson (pos ), x-node [lson (pos)]. w); update (pos); return res ;}} sgm; int Scan () {int res = 0, ch, flag = 0; if (ch = getchar ()) = '-') flag = 1; else if (ch> = '0' & ch <= '9') res = ch-'0 '; while (ch = getchar ()> = '0' & ch <= '9') res = res * 10 + ch-'0'; return Flag? -Res: res;} void Out (int a) {if (a> 9) Out (a/10); putchar (a % 10 + '0 ');} int a [2] [N], n; int ans [N]; int main () {while (~ Scanf ("% d", & n) {repf (I, 0, n-1) {a [0] [I] = Scan (); a [1] [I] = Scan ();} sgm. build (1, n, ROOT); for (int I = n-1; I> = 0; I --) {ans [sgm. remove (1, n, ROOT, a [0] [I])] = a [1] [I];} repf (I, 1, n) {if (I! = 1) printf (""); Out (ans [I]);} printf ("\ n");} return 0 ;}



POJ2777 line segment tree dyeing, incorrect answer? Urgent

Sorry! I don't understand this problem. Please try again!
I can help you. After you set my best answer, I will teach you Baidu Hii.

For the poj2777 question, I will think about the line segment tree and lazy, but I don't understand what it is like to use bits to record colors, or combine colors with values.

This is lazytag ....
Each line segment Tree node records the following information: sum, p, q, tag, pre, suf
There are sum different colors in p and q respectively. tag indicates lazy mark,-1 indicates no mark, and others indicate to dye this interval into tag color; pre indicates the color of the Left endpoint of the interval, and suf indicates the right endpoint.

When updating and querying, first upload a flag... This is the same as other lazy labels.
When the flag is passed down, clear the tag directly, set pre and suf to the tag color, sum = 1, and mark it to the Son (if any)

Then, update the node (note that this node is u, the left son is l, and the right son is r)
First, Mark l and r, and then
U. pre = l. pre;
U. suf = r. suf;
U. sum = l. sum + r. sum-(l. suf = r. pre );
Describe the last
First, set u. sum to l. sum + r. sum,
However, when the color of the left-son suffix is the same as that of the right-son prefix, the two colors are actually the same, so we calculate it once, and subtract
Nothing else.

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.