Poj 2828 buy tickets

Source: Internet
Author: User

From the back to the front, you can find the position where the front is exactly set to k positions.

 

//============================================================================// Name        : F.cpp// Author      : L_Ecry// Version     :// Copyright   : Your copyright notice// Description : Hello World in C++, Ansi-style//============================================================================#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<cmath>#define N 200050using namespace std;int sum[N * 4];void build(int l, int r, int i) {    sum[i] = r - l + 1;    if (l != r) {        int mid = (l + r) >> 1;        build(l, mid, i << 1);        build(mid + 1, r, i << 1 | 1);    }}int update(int l, int r, int va, int i) {    sum[i]--;    if (l == r) {        return l;    }    int mid = (l + r) >> 1;    if (sum[i << 1] > va)        return update(l, mid, va, (i << 1));    else        return update(mid + 1, r, va - sum[i << 1], (i << 1 | 1));}int n;int ans[N];int x[N], y[N];int main() {    while (scanf("%d", &n) != EOF) {        build(1, n, 1);        for (int i = 0; i < n; ++i)            scanf("%d%d", &x[i], &y[i]);        for (int i = n-1; i >-1; --i) {            int tmp = update(1, n, x[i], 1);            ans[tmp] = y[i];        }        for (int i = 1; i < n; ++i) {            printf("%d ", ans[i]);        }        printf("%d\n",ans[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.