Codevs 3304 fruit elder sister Stroll Fruit Street Ⅰ the puzzle line tree

Source: Internet
Author: User

Topic Description Description

Fruit sister is in a good mood today, came to the fruit street.

Fruit Street has n fruit shop, is linear structure, numbered 1~n, each store can buy fruit can also sell fruit, and the same store to sell and buy the same price.

The fruit sister who studied Oi quickly found a way to make money: buy a fruit in a fruit store and sell it at another store to make a difference.

When the fruit elder sister steals a joy, Cgh suddenly appears, he in order to embarrass Fruit elder sister, give M a question, each question requires the fruit sister from X shop to the Y shop, on the way can only choose a shop to buy a fruit, and then choose a shop (can be the same shop, but not go back) sold out, Ask for the maximum amount of money you can earn in each question. Input description Input Description

The first line n, which means there are N stores

Down n A positive integer that represents the price of an apple per store.

Down an integer m, the expression down has m inquiry.

Down there are m lines, two integers x and y per line, representing the departure from the X shop to the Y shop. Output Description Description

There are M lines.

Each row corresponds to a query, an integer, indicating the face of CGH each inquiry, fruit sister can make how much money. Sample Input For example

10
2 8 15 1 10 5 19 19 3 5
4
6 6
2 8
2 2
6 3 Sample Output

0
18
0
14 range and hint data Size & Hint

0<= Apple's price <=10^8

0<n,m<=200000

This can be solved with a line-tree. Each node in the segment tree records the maximum and minimum values in the current interval and the maximum amount of money earned from left to right ans1 (=max{w[r]-w[l]| L<=l<=r<=r}), from left to right to go to the maximum amount of money ans1.

In the achievement can be ans1, ans1 find, see Pushup (POS) function.

In the query, take the example from left to right to return the largest of these three cases:

1. The maximum value on the right, the minimum on the left, directly to the left side of the minimum value, the maximum value, subtract can
2. The maximum and minimum values are on the left side, the recursive call itself to seek the largest difference can be
3. The maximum and minimum values are on the right side and the treatment method is the same

Walking from right to left is similar to this query.


#include <cstdio> #include <iostream> #include <cmath> #include <cstring> #include <
Algorithm> using namespace std;
int n,m;
int x,y;
	#define MAXN 200001 struct tree{int _max,_min;
int L2r,r2l;//lefttoright,righttoleft}e[maxn<<4];
#define MAX3 (A,B,C) max (A,max (b,c)) int tmp;
	void pushup (int u) {e[u]._max = max (E[u<<1]._max,e[u<<1|1]._max);
	e[u]._min = min (e[u<<1]._min,e[u<<1|1]._min);
	E[U].L2R = Max3 (E[U&LT;&LT;1|1]._MAX-E[U&LT;&LT;1]._MIN,E[U&LT;&LT;1].L2R,E[U&LT;&LT;1|1].L2R);
e[u].r2l = Max3 (e[u<<1]._max-e[u<<1|1]._min,e[u<<1].r2l,e[u<<1|1].r2l);
	} void Build (int u,int l,int r) {e[u].l2r=e[u].r2l=0;
		if (l==r) {scanf ("%d", &tmp);
		E[u]._max = E[u]._min = tmp;	
	Return
	int mid = (l+r) >>1;
	Build (U<<1,l,mid);
	Build (U<<1|1,mid+1,r);
Pushup (U);
	int query (int u,int l,int r,int x,int y)/check max {if (x<=l&&y>=r) {return e[u]._max; int mid = (l+r) >>1;
	if (y<=mid) return query (u<<1,l,mid,x,y);
	else if (x>mid) return query (u<<1|1,mid+1,r,x,y);
	else {return max (query (u<<1,l,mid,x,y), query (U<<1|1,mid+1,r,x,y));
	the int query2 (int u,int l,int r,int x,int y)/query min {if (x<=l&&y>=r) {return e[u]._min;
	int mid = (l+r) >>1;
	if (Y<=mid) return Query2 (u<<1,l,mid,x,y);
	else if (X>mid) return Query2 (u<<1|1,mid+1,r,x,y);
	else {return min (Query2 (u<<1,l,mid,x,y), Query2 (u<<1|1,mid+1,r,x,y));
	the int solve (int u,int l,int r,int x,int y)//left to right {if (x<=l&&y>=r) {return e[u].l2r;
	int mid = (l+r) >>1;
	int ret =-1;
	if (x<=mid) ret = max (Ret,solve (u<<1,l,mid,x,y));
	if (y>mid) ret = max (Ret,solve (u<<1|1,mid+1,r,x,y));
	if (x<=mid&&y>mid) ret = max (Ret,query (1,1,n,mid+1,y)-query2 (1,1,n,x,mid));
return ret; int solve2 (int u,int l,int r,int x,int y) {if (x<=l&&y>=r) {return e[u].r2l;
	int mid = (l+r) >>1;
	int ret =-1;
	if (x<=mid) ret = max (Ret,solve2 (u<<1,l,mid,x,y));
	if (y>mid) ret = max (Ret,solve2 (u<<1|1,mid+1,r,x,y));
	if (x<=mid&&y>mid) ret = max (Ret,query (1,1,n,x,mid)-query2 (1,1,n,mid+1,y));
return ret;
	int main () {scanf ("%d", &n);
	Build (1,1,n);
	scanf ("%d", &m);
		while (m--) {scanf ("%d%d", &x,&y);
		if (x<y) printf ("%d\n", Solve (1,1,n,x,y));
		else if (x==y) puts ("0");
	else printf ("%d\n", Solve2 (1,1,n,y,x));
return 0; }


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.