Good Bye 2014E greedy monotone stack + line segment Tree New year Domino at least increase the number of high length dominoes can push x y__ the question bank-CF

Source: Internet
Author: User
New year Domino time limit/test 2 seconds memory limit per test 256 megabytes input standard input output standard out Put

Celebrating the New year, many people post videos of falling dominoes; Here ' s a list of Them:https://www.youtube.com/results?search_query=new+years+dominos

User Ainta, who lives in a 2D world, are going to post a video as.

There are n dominoes on a 2D Cartesian plane. I-th Domino (1≤i≤n) can be represented as a line segment which are parallel to the y-axis and whose length is li. The lower point of the ' Domino is ' on the x-axis. Let ' s denote the x-coordinate of the i-th Domino as Pi. Dominoes are placed one after another, so P1 < P2 < ... < pn-1 < PN holds.

User Ainta wants to take a video of falling dominoes. To make dominoes fall, he can push a single domino to the right. Then, the domino would fall down drawing a circle-shaped orbit until the line segment totally overlaps with the x-axis.

Also, if the s-th Domino touches the t-th Domino while falling down, the t-th Domino would Also fall down towards the right , following the same procedure above. Domino s touches Domino t if and only if the segment representing S and T intersects.

The picture above. If He pushes the "leftmost domino to" right, it falls down, touching dominoes (A), (B) and (C). As a result, dominoes (a), (B), (C) would also fall towards the right. However, Domino (D) won ' t be affected by pushing the leftmost domino, but eventually it'll fall because it is touched by Domino (C) for the.

The picture above was an example of falling dominoes. Each red circle denotes a touch of two dominoes.

User Ainta has q plans of posting the video. J-th of them starts with pushing the xj-th Domino, and lasts the until Domino yj-th. But Sometimes, it could is impossible to achieve such, and so he has to lengthen some. It costs one dollar to increase the length of a single domino by 1. User Ainta wants to know, for each plan, the minimum cost needed to achieve it. Plans are processed independently, E. If Domino ' s length is increased in some, it doesn ' t affect its length in oth ER plans. Set of dominos that would fall except xj-th Domino and yj-th Domino doesn ' t matter, but the initial push should is on Domin o XJ. Input

The contains an integer n (2≤n≤2x105)-the number of dominoes.

Next n lines describe the dominoes. The i-th Line (1≤i≤n) contains two space-separated integers pi, li (1≤pi, li≤109)-the x-coordinate and the length of the i-th Domino. It is guaranteed that P1 < P2 < ... < pn-1 < PN.

The next line contains a integer q (1≤q≤2x105)-the number of plans.

Next Q lines describe the plans. The j-th Line (1≤J≤Q) contains two space-separated integers xj, YJ (1≤xj < yj≤n). It means the j-th plan are, to push the xj-th Domino, and shoot a video until the yj-th Domino Falls. Output

For each of the, print a line containing the minimum cost needed to achieve it. If No is needed, print 0. Examples input

6
1 5
3 3 4 4 9 2
ten
1 1 4-1 2 2 4 2-5 2
Output
0
1
1
2
Note

Consider the example. The dominoes are set like the below.

Let ' s take a look at the 4th plan. To make the 6th Domino fall by pushing the 2nd Domino, the length of the 3rd Domino (whose x-coordinate are 4) should be in creased by 1, and the 5th Domino (whose x-coordinate are 9) should be increased from 1 (option is to increase 4th Domin o instead of 5th also by 1). Then, the dominoes would fall like in the "picture below." Each cross denotes a touch between two dominoes.


#include <stdio.h> #include <iostream> #include <string.h> #include <string> #include < ctype.h> #include <math.h> #include <set> #include <map> #include <vector> #include <queue
> #include <bitset> #include <algorithm> #include <time.h> using namespace std; void Fre () {freopen ("c://test//input.in", "R", stdin); Freopen ("C://test//output.out", "w", stdout);} #define MS (x,y) Me Mset (x,y,sizeof (x)) #define MC (x,y) memcpy (x,y,sizeof (x)) #define MP (x,y) Make_pair (x,y) #define Lson O<<1,l,mid #
Define Rson o<<1|1,mid+1,r #define LS o<<1 #define RS o<<1|1 typedef long LL;
typedef unsigned long UL;
typedef unsigned int UI; Template <class T1, class t2>inline void Gmax (T1 &a, T2 b) {if (b>a) a = b;} template <class T1, class T2
>inline void Gmin (T1 &a, T2 b) {if (b<a) a = b;} const int N = 2e5+10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n, m;
int p[n], l[n]; vector< Pair<int, int> >query[N];
int ans[n];
int cost[1 << 19]; int sta[n];
int top;
int L, R, V, P;
		void pushdown (int o) {if (Cost[o]) {Cost[ls] + = Cost[o];
		Cost[rs] + = Cost[o];
	Cost[o] = 0;
		} void Add (int o, int l, int r) {if (L >= l&&r <= R) {Cost[o] = V;
	Return
	} pushdown (o);
	int mid = (L + r) >> 1;
	if (L <= mid) Add (Lson);
if (R > Mid) Add (Rson);
	int check (int o, int l, int r) {if (L = = r) return Cost[o];
	int mid = (L + r) >> 1;
	Pushdown (o);
	if (P <= mid) return check (Lson);
else return check (Rson); int main () {while (~SCANF ("%d", &n)) {for (int i = 1; I <= n; ++i) {scanf ("%d%d", &p[i], &l[i]
			);
			L[i] + = P[i];
		Query[i].clear ();
		} scanf ("%d", &m);
			for (int i = 1; I <= m; ++i) {int x, y; scanf ("%d%d", &x, &y);
		Query[y].push_back (MP (x,i)); MS (cost, 0); Sta[0] = 0;
		top = 0; for (int i = 1; I <= n; ++i) {while (top&&l[sta[top]] < P[i]) {L = sta[top-1] + 1;
				R = Sta[top];
				V = P[i]-l[sta[top]];
				Add (1, 1, N);
			--top;
				for (int j = query[i].size ()-1; ~j;--j) {P = Query[i][j].first;
			Ans[query[i][j].second] = Check (1, 1, N);
			while (Top&&l[sta[top]] <= l[i])--top;
		Sta[++top] = i;
	for (int i = 1; I <= m ++i) printf ("%d\n", ans[i));
return 0;
	}/* "trick&& spit" 1, do not see geometry-related problem expression immediately IQ line into a fool.
Maybe it has nothing to do with the computational geometry of half a dime.

2, when thinking is blocked, think about whether the direction of thinking can be changed and seek a breakthrough.
Give you N (2E5) block dominoes.
Each domino I has its placement p[i] and height l[i] (1<=p[i],l[i]<=1e9).

We guarantee that P[1]<p[2]<...<p[n] has Q inquiry, for each inquiry (x,y) asks you, assuming we push down the X block dominoes, at least how much cost to hit the Block y dominoes.
The cost here is defined as--if we don't adjust the domino, we can push x down Y, then the cost is 0.
Otherwise, we can select any of the Dominoes + arbitrary heights and make the push x pour y.
The final cost is the sum of the heights we add.

Inquiries are independent of each other.
"Type" monotone stack + segment tree "Analysis" How to do this problem.
First of all, a greedy is, if we push a domino from one position, there is no domino that pushes to position J.
We will definitely choose the position J before I, P[i]+l[i] the largest one, to do a height increase.

And once elevated, certainly as long as the increase makes p[]+l[] touch to p[j] can, superfluous increase is unnecessary. The first idea is to process queries offline.
Thought for a while blew up. The second idea is preprocessing.

But thought for a while or felt blown up. However, both of these ideas, I think, are processed from left to right, asking the same thing about the left.Unified treatment.

So why not consider the right side of the same question and put it together for unified treatment.
So, along the way, we scan from left to right, enumerate to I, and handle all inquiries asking the right side of I.

In this way, we have taken the left boundary as [1,i] into consideration before dealing with the question of the right boundary as I.
One of the properties we find is that if a point is p[]+l[]<= before it, and the point has been determined to be pushed down, then the point has no meaning.

So, we maintain a monotonous stack, so that the bottom of the stack to the top of the p[]+l[] is monotonically decreasing.
Let's say we've maintained the cost of all the dominoes trying to tear down the i-1 dominoes.

Now, we want to tear down the first domino, and for the question of the right I, the starting point of the push is the left boundary, which may be any of the [1,i-1].
We found--if the top element of the stack x is p[x]+l[x]>=p[i], then, because the previous point can be determined to pull down X, it must also be able to push down I, no extra cost increases. And if the top element x of the stack is p[x]+l[x]<p[i], then, suppose that sta[top-1] is Y, then there are: [Y+1,x] The number of ranges that add a p[i]-(P[x]+l[x]) cost.

At the same time in order to calculate the cost of the elements before the stack, while maintaining a monotonous stack, it is necessary to retreat.
This involves updating the operation of the interval answer, which we can implement with the segment tree. Each point into the stack out of the stack once, the number of inquiries Mlogn, modify the number of Nlogn, can be AC "Time complexity && Optimization" O ((m+n) logn) * *


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.