Logo
(追記) (追記ここまで)

33501번 - Median Heap 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
4 초 2048 MB36201954.286%

문제

Farmer John has a binary tree with $N$ nodes where the nodes are numbered from 1ドル$ to $N$ (1ドル \leq N < 2\cdot 10^5$ and $N$ is odd). For $i>1,ドル the parent of node $i$ is $\lfloor i/2\rfloor$. Each node has an initial integer value $a_i,ドル and a cost $c_i$ to change the initial value to any other integer value (0ドル\le a_i,c_i\le 10^9$).

He has been tasked by the Federal Bovine Intermediary (FBI) with finding an approximate median value within this tree, and has devised a clever algorithm to do so.

He starts at the last node $N$ and works his way backward. At every step of the algorithm, if a node would not be the median of it and its two children, he swaps the values of the current node and the child value that would be the median. At the end of this algorithm, the value at node 1ドル$ (the root) is the median approximation.

The FBI has also given Farmer John a list of $Q$ $(1 \leq Q \leq 2\cdot 10^5)$ independent queries each specified by a target value $m$ (0ドル\le m\le 10^9$). For each query, FJ will first change some of the node's initial values, and then execute the median approximation algorithm. For each query, determine the minimum possible total cost for FJ to make the output of the algorithm equal to $m$.

입력

The first line of input contains $N$.

The next $N$ lines each contain two integers $a_i$ and $c_i$.

The next line contains $Q$.

The next $Q$ lines each contain a target value $m$.

출력

Output $Q$ lines, the minimum possible total cost for each target value $m$.

제한

예제 입력 1

5
10 10000
30 1000
20 100
50 10
40 1
11
55
50
45
40
35
30
25
20
15
10
5

예제 출력 1

111
101
101
100
100
100
100
0
11
11
111

To make the median approximation equal 40ドル,ドル FJ can change the value at node 3 to 60ドル$. This costs $c_3=100$.

To make the median approximation equal 45ドル,ドル FJ can change the value at node 3 to 60ドル$ and the value at node 5 to 45ドル$. This costs $c_3+c_5=100+1=101$.

힌트

출처

Olympiad > USA Computing Olympiad > 2024-2025 Season > USACO 2025 January Contest > Gold 1번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /