Beauty of Segments
All submissions for this problem are available.### Read problem statements in [Hindi](http://www.codechef.com/download/translated/LTIME75/hindi/BOFS.pdf), [Bengali](http://www.codechef.com/download/translated/LTIME75/bengali/BOFS.pdf), [Mandarin Chinese](http://www.codechef.com/download/translated/LTIME75/mandarin/BOFS.pdf), [Russian](http://www.codechef.com/download/translated/LTIME75/russian/BOFS.pdf), and [Vietnamese](http://www.codechef.com/download/translated/LTIME75/vietnamese/BOFS.pdf) as well. You are given $N$ segments $(l_1, r_1), (l_2, r_2), (l_N, r_N)$ and two integers $A$ and $B$. Let's define *playing a game* with two parameters $(x, y)$, where $0 \le x \le N$, as the following recursive process: - If $x = 0$, the game ends immediately. - Otherwise, if $y \le l_x$ or $r_x \le y$, end this game and play a game with $(x-1, y)$. - Otherwise (if $l_x \lt y \lt r_x$), you must choose one of the following options: - Pay $A$ coins, end this game and play a game with $(x-1, y)$. - Pay $B$ coins, end this game and play two independent games — a game with $(x-1, l_x)$ and a game with $(x-1, r_x)$. You should answer $Q$ independent queries. In each query, you are given two integers $x$ and $y$ and you should start by playing a game with parameters $(x, y)$. Your goal is to reach a state when you should not play any games, since you have successfully ended every game you should have played. Find the minimum number of coins required for reaching this state. ### Input - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains four space-separated integers $N$, $Q$, $A$ and $B$. - For each $i$ ($1 \le i \le N$), the $i$-th of the next $N$ lines contains two space-separated integers $l_i$ and $r_i$. - $Q$ lines follow. Each of these lines contains two space-separated integers $x$ and $y$ describing a query. ### Output For each query, print a single line containing one integer — the minimum number of coins. ### Constraints - $1 \le T \le 100$ - $1 \le N, Q \le 2 \cdot 10^5$ - $1 \le l_i \le r_i \le 2 \cdot 10^5$ for each valid $i$ - $1 \le x \le N$ - $1 \le y \le 2 \cdot 10^5$ - $1 \le A, B \le 2 \cdot 10^5$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ - the sum of $Q$ over all test cases does not exceed $5 \cdot 10^5$ ### Subtasks **Subtask #1 (40 points):** $Q, N \le 10^3$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^3$ - the sum of $Q$ over all test cases does not exceed $5 \cdot 10^3$ **Subtask #2 (60 points):** original constraints ### Example Input ``` 1 4 3 3 4 3 5 3 5 1 3 2 5 4 4 4 5 1 4 ``` ### Example Output ``` 7 0 3 ``` ### Explanation **Example case 1:** One of the strategies that use the minimum number of coins is: - Pay $3$ coins and play a game with $(3, 4)$. - In the game with $(3, 4)$, you just play a game with $(2, 4)$, since $4$ is outside the $3$-rd segment. - In the game with $(2, 4)$, pay $4$ coins and play games with $(1, 3)$ and $(1, 5)$. - Both of these games end without paying any more coins. The only thing left is playing games with $(0, 3)$ and $(0, 5)$, which end immediately, also without paying any coins. The total number of coins used in this strategy is $7$.
|Tags||farhod_farmon, lazypropagation, ltime75, medium, segment-tree, taran_1407|
|Time Limit:||2 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, kotlin, PERL6, TEXT, SCM chicken, PYP3, CLOJ, R, COB, FS|
Fetching successful submissions
If you are still having problems, see a sample solution here.