POJ 2828 Buy Tickets 線段樹入門(建樹稍微有點抽象)
一開始看的時候真的不知道為什麼是線段樹的題目。後來看到了網上他們的講解,發現原來也是線段樹。
先說一下題意:就是買票插隊的問題,有n個人插隊,他們都是插道a的後面,所以他的當前位置是a+1,然後也肯能會有別的人插到他的前面去,所以他的最終位置會發生變化,要你說出最終隊伍的順序。
做的方法是:倒過來更新線段樹,找到每個人應該存在的位置,然後記錄下來,輸出就行了。
解釋一下演算法:
這裡建樹儲存的值是以每個區間內有多少位置是空的個數。然後建立一個陣列儲存,每個葉子儲存的是自己那個位置上有沒有被佔。根節點上表示是區間上有多少個空的位置。但是要注意的是每個點要插入的位置p,因該滿足之前有p個空位,否則他是插進去也會向後移動的。因為那個位置上一定是已經有人存在過了,相反如果沒人存在的話,他就不會插在那個位置的後面了啊。理解這個位置的問題就好做多了啊。注意在找p的位置的時候如果左子樹所存的空位置的個數,少於你需要的個數的話,你就p - num[site<<1],然後再在有子樹裡面找。
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 11467 | Accepted: 5621 |
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 a.m. 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 in 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
Sample Output
77 33 69 51 31492 20523 3890 19243
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 10001000
//#define LL __int64
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
const int maxn = 2000100;
using namespace std;
int f[maxn*2];
int star[maxn], p[maxn];
int num[maxn];
int t;
void Bulid(int l, int r, int site)
{
f[site] = r-l+1;
if(l == r)
return;
int mid = (l+r)>>1;
Bulid(l, mid, site<<1);
Bulid(mid+1, r, site<<1|1);
}
void Search(int p, int l, int r, int site)
{
int mid = (l+r)>>1;
f[site]--;
if(l == r)
{
t = l;
return;
}
if(f[site<<1] >= p)
{
Search(p, l, mid, site<<1);
}
else
{
p -= f[site<<1];//減去左子樹中已經包括的空位置
Search(p, mid+1, r, site<<1|1);
}
}
int main()
{
int n;
while(scanf("%d",&n) != EOF)
{
Bulid(1, n, 1);
for(int i = 1; i <= n; i++)
scanf("%d %d",&star[i], &num[i]);
for(int i = n; i >= 1; i--)
{
Search(star[i]+1, 1, n, 1);
p[t] = num[i];
}
for(int i = 1; i <= n-1; i++)
printf("%d ",p[i]);
printf("%d\n",p[n]);
}
return 0;
}
相關文章
- 線段樹入門
- POJ 3667 Hotel 線段樹
- poj 2667 hotel 線段樹
- 線段樹入門(Segment Tree)
- 線段樹模版:從入門到入墳
- POJ 2777 Count Color (線段樹+狀態壓縮)
- TZOJ 8472 : Tree (重鏈剖分+線段樹) POJ 3237
- POJ 3468 A Simple Problem with Integers (線段樹 區間共加)
- 動態開點線段樹
- 線~段~樹
- 線段樹
- 線段樹最佳化建圖
- 線段樹(1)建樹、單點修改、單點查詢、區間查詢和例題
- POJ 3468 【區間修改+區間查詢 樹狀陣列 | 線段樹 | 分塊】陣列
- babel外掛入門-AST(抽象語法樹)BabelAST抽象語法樹
- 線段樹模板
- 線段樹--RMQMQ
- 01 線段樹
- 線段樹 hate it
- 【模版】線段樹
- POJ 2528 Mayor's posters (線段樹 區間更新+離散化)
- 動態開點線段樹說明
- 資料結構之樹( 線段樹,字典樹)資料結構
- 13 個示例快速入門 JS 抽象語法樹JS抽象語法樹
- babel 修改抽象語法樹——入門與實踐Babel抽象語法樹
- [LeetCode] 2073. Time Needed to Buy TicketsLeetCode
- 題解:P10688 Buy Tickets
- 權值線段樹
- 線段樹筆記筆記
- Segment Tree(線段樹)
- 李超線段樹
- 線段樹進階
- HDU 3074 Multiply game(線段樹 單點更新)GAM
- 線段樹也能是 Trie 樹 題解
- 洛谷題單指南-線段樹-P3373 【模板】線段樹 2
- 【知識點】淺入線段樹與區間最值問題
- 線段樹擴充套件套件
- 第二課——線段樹
- 線段樹簡單思路