CODEVS3304 fruit sister visits Fruit Street

Source: Internet
Author: User
Tags ranges
(I wanted to finish writing about the fruit sister series, but I suddenly found that I will leave noip tomorrow, so ...) Description Description

The fruit sister was in a good mood today and came to the fruit Street.

There are n fruit shops in Fruit Street, in a straight line structure, numbered 1 ~ N. Each store can buy or sell fruit, and the same store can sell at the same price.

The fruit sister who has learned oi quickly found a way to make money: buy a fruit in a fruit shop and then sell it to another store to make a difference.

Just when the fruit sister had a crush, cgh suddenly appeared. In order to embarrass the fruit sister, he gave m problems. Each problem required the fruit sister to depart from the x store to the y store, on the way, you can only choose one store to buy one fruit, and then choose one store (which can be the same store, but cannot go back) to sell it. Find the maximum amount of money you can earn for each problem.

 

Idea: The linear problem of the fruit sister series is maintained using the line segment tree, saving time. Use the four items in the Struct (maximum range, small value, maximum range trade value (positive and inverse )). Here is a clever thing, that is, for the range [a, B], its maximum and small values are relatively simple, however, the maximum trade value is updated based on the difference between the maximum trade value of the left and right child ranges and the maximum and minimum values of the left and right ranges. I started to use four arrays, and later found tle, so I learned from the dada program and changed it to a struct. As a result, I made another magic mistake (directly changed the value of the tree ), you only need to return the value...

 

# Include <iostream> # include <cstdio> using namespace std; struct use {int maxi, mini, zan, fan;} tr [800000]; int a [200001] = {0}, n; void build (int I, int l, int r) {int mid; if (l = r) {tr [I]. maxi = tr [I]. mini = a [l]; return;} mid = (l + r)/2; build (I * 2, l, mid); build (I * 2 + 1, mid + 1, r); tr [I]. maxi = max (tr [I * 2]. maxi, tr [I * 2 + 1]. maxi); tr [I]. mini = min (tr [I * 2]. mini, tr [I * 2 + 1]. mini); tr [I]. zan = max (tr [I * 2]. zan, max (tr [I * 2 + 1]. zan, tr [I * 2 + 1]. maxi-tr [I * 2]. mini); tr [I]. fan = max (tr [I * 2]. fan, max (tr [I * 2 + 1]. fan, tr [I * 2]. maxi-tr [I * 2 + 1]. mini);} struct use work (int I, int l, int r, int x, int y) {int mid, maxn = 0; struct use tem, temm, temi; if (x> r | y <l) return (use) {-1, 2100000000,-1,-1}; if (x <= l & r <= y) return tr [I]; mid = (l + r)/2; tem = work (I * 2, l, mid, x, y ); temm = work (I * 2 + 1, mid + 1, r, x, y); temi. maxi = max (tem. maxi, temm. maxi); temi. mini = min (tem. mini, temm. mini); temi. zan = max (tem. zan, max (temm. zan, temm. maxi-tem.mini); temi. fan = max (tem. fan, max (temm. fan, tem. maxi-temm.mini); return temi;} int main () {int m, I, j, x, y; struct use ans; scanf ("% d", & n ); for (I = 1; I <= n; ++ I) scanf ("% d", & a [I]); build (1, 1, n ); scanf ("% d", & m); for (I = 1; I <= m; ++ I) {scanf ("% d", & x, & y); if (x = y) printf ("0 \ n"); else {if (x <y) {ans = work (1, 1, n, x, y); printf ("% d \ n", ans. zan);} else {ans = work (1, 1, n, y, x); printf ("% d \ n", ans. fan );}}}}
RZUC Code

 

CODEVS3304 fruit sister visits Fruit Street

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.