After hard work Igor decided to have some rest.
He decided to have a snail. He bought an aquarium with a slippery tree trunk in the center, and put a snail named Julia into the aquarium.
Igor noticed that sometimes Julia wants to climb onto the trunk, but can't do it because the trunk is too slippery. To help the snail Igor put some ropes on the tree, fixing the lower end of the i-th rope on the trunk on the height li above the ground, and the higher end on the height ri above the ground.
For some reason no two ropes share the same position of the higher end, i.e. all ri are distinct. Now Julia can move down at any place of the trunk, and also move up from the lower end of some rope to its higher end. Igor is proud of his work, and sometimes think about possible movements of the snail. Namely, he is interested in the following questions: «Suppose the snail is on the trunk at height x now. What is the highest position on the trunk the snail can get on if it would never be lower than x or higher than y?» Please note that Julia can't move from a rope to the trunk before it reaches the higher end of the rope, and Igor is interested in the highest position on the tree trunk.
Igor is interested in many questions, and not always can answer them. Help him, write a program that answers these questions.
The first line contains single integer n (1 ≤ n ≤ 100000) — the height of the trunk.
The second line contains single integer m (1 ≤ m ≤ 100000) — the number of ropes.
The next m lines contain information about the ropes.
The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n) — the heights on which the lower and the higher ends of the i-th rope are fixed, respectively. It is guaranteed that all ri are distinct.
The next line contains single integer q (1 ≤ q ≤ 100000) — the number of questions.
The next q lines contain information about the questions.
Each of these lines contain two integers x and y (1 ≤ x ≤ y ≤ n), where x is the height where Julia starts (and the height Julia can't get lower than), and y is the height Julia can't get higher than.
For each question print the maximum reachable for Julia height.
8 4 1 2 3 4 2 5 6 7 5 1 2 1 4 1 6 2 7 6 8
2 2 5 5 7
10 10 3 7 1 4 1 6 5 5 1 1 3 9 7 8 1 2 3 3 7 10 10 2 4 1 7 3 4 3 5 2 8 2 5 5 5 3 5 7 7 3 10
2 7 3 3 2 2 5 3 7 10 题目大意:一些询问,问一段区间里 被严格覆盖的子区间 的mex(什么玩意。。)
分块好题!
首先我们发现,因为以每个高度为终点的绳子只有一个,我们可以对每个点高度记录一个prv,表示以这个高度为终点的绳子起点在哪,这样我们就可以O(区间长度)的处理询问,具体做法是从区间开始到结束扫一遍,维护当前能从左端点走到的最右面的点,每当找到一个绳子,起点>=左边界,并且<=当前能走到的最右边的点,那么就把能走到的最右边的点更新为这个点。为什么这样是对的呢?因为这样按照终点从左到右扫描,如果一根绳子当前不能用,那么以后也永远用不到——以后能用的绳子都比它更优秀然后我们就可以有一个O(qn)的做法,但这样不够优秀,我们考虑优化。容易看出按照刚才那种做法,把一个区间往后延长一段是可做的,那我们不妨考虑这样一种做法。我们把整个树干分成 $ \sqrt{n} $ 块,预处理出每个起点到每个块终点的答案(能走到的最右边的点),然后我们就可以 $ \sqrt{n} $ 的回答询问了现在问题在于,怎么求这个东西。吐槽一下毛子写的英文题解,**不通,晦涩难懂假设一个块的大小是sz_b假设这个东西叫做ans[i][j],表示[j,i*sz_b-1]的答案(我是从0开始标号的)我们需要一个东西来求它,假设叫做f[i][j],表示所有以j为起点,终点<i*sz_b,的绳子中,终点最大的,可能有点绕,理解一下,原文更坑爹这个f我们可以直接two pointers求出来,因为对于每一个j,f[i][j] (j固定,i是变量) 都是单调的,对每个位置维护一个指针就好了然后我们有了f,要求ans了,我们从右往左做,显然ans[i][j] = max(ans[i][k]) j<=k<=f[i][j],这个式子还是比较好理解的吧然后看上去就可以用线段树维护了,但这样还是会T,我们还需要进一步优化考虑一个正在计算ans[i][j]的位置j,我们不妨假设从右往左扫描max,如果ans[i][j]被ans[i][p]和ans[i][q](p<q)都更新了一次,那说明了什么?说明ans[i][q]<ans[i][p]那么p的位置又比q靠前,能延伸到的位置还比q靠后,显然q这个位置没用啊然后如果用更新了j,ans[i][j]>=ans[i][p],i又比p靠前,p也没用了所以我们对每一趟求ans[*][j]维护一个单调栈,栈中元素是一个二元组(i,j),表示i这个位置的ans是j扫描到一个元素p时,把所有栈中p能够到(p+f[*][p]>=q)的元素吐出来并更新ans[*][p],然后把(p,ans[*][p])丢进栈里这样每一趟求ans也可以线性了然后就搞定啦!总复杂度 $ O((n+q)\sqrt{n}) $
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include