POJ 2828 Buy Tickets (line tree/queue jump)

Source: Internet
Author: User

Test Instructions n Personal queue Everyone has a property value enter N pos[i] val[i] indicates that I am a person directly inserted into the current pos[i] after the individual, his property value is Val[i] requires the final output of the team's individual property values

From the beginning to the end, the queue is dynamic and cannot be manipulated, but in turn, pos[i] can indicate how many individuals are in front of the first person and then think of a line tree to make it simple. Segment tree maintenance corresponding interval and how many empty slots each time I put to the front just pos[i] empty position on the line Body look at the code

#include <cstdio> #define LC p<<1, S, mid#define RC p<<1|1, mid+1, E#define Mid ((s+e) >>1) using Nam Espace std;const int N = 2e5 + 5;int tot[n * 4], Ans[n];//tot maintain the corresponding interval can also put how many people int pos[n], Val[n];//pos[i] Save the first I enter the team in front of how many people//val[ I] Save the personal valvoid pushup (int p) {Tot[p] = tot[p << 1] + tot[p << 1 | 1];}        void build (int p, int s, int e) {//tot maintains the corresponding interval how many people can be put if (s = = e) {Tot[p] = 1;    Return    } build (LC);    Build (RC); Pushup (P);}        The first person inserts void update (int p, int s, int e, int i) {if (s = = e) {tot[p] = 0;        Ans[e] = Val[i];    Return  } if (tot[p << 1] > Pos[i]) Update (LC, I);        The left interval is empty enough for else {pos[i]-= tot[p << 1];    Update (RC, i); } pushup (P);}    int main () {int n;        while (~SCANF ("%d", &n)) {build (1, 1, N);        for (int i = 1; I <= n; ++i) scanf ("%d%d", &pos[i], &val[i]);        for (int i = n; i > 0; i)//backwards update    Update (1, 1, n, i);        for (int i = 1; i < n; ++i) printf ("%d", ans[i]);    printf ("%d\n", Ans[n]); } return 0;} Last modified:2015-07-13 11:13


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2828 Buy Tickets (line tree/queue jump)

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.