Sicily 1800 sequence (rmq)

Source: Internet
Author: User

// Rmq (solving a line segment tree) <br/> // after a, I understand the conversion of the series. If continuous intervals and, it must be converted to sum [I], indicating the sum of the intervals from 1 to I <br/> // This requires the sum of any intervals. Suppose [I, j] of the sum, only need sum [J]-sum [I-1] can be obtained immediately <br/> // note sum [0] = 0; <br/> // train of thought: Enumerate sum [I] (I> = L) from L) <br/> // find the maximum sum [J] (max (0, I-u) <= j <= I-l) in the range that meets the length condition) <br/> // sum [I]-sum [J] is the smallest <br/> // This converts the problem to the maximum value of the given range. <br/>/ /this question cannot be done using the st algorithm, if St is used, it will time out. It may take too much time in the log operation. <br/> // After Reading others' question solutions, we can see that the queue is optimized with priority, this speed should be faster <br/> # include <iostream> <br/> # define Max 32780 <br/> # define INF 2147483647 <br/> using namespace STD; <br/> int n, l, U; <br/> int sum [Max]; <br/> int _ max, ans; <br/> struct seg <br/> {<br/> int L, R, _ max; <br/>} segtree [Max * 4]; <br/> void buildtree (int fa, int L, int R) <br/>{< br/> segtree [fa]. L = L; <br/> segtree [fa]. R = r; <br/> If (L = r) <br/> {<br/> segtree [fa]. _ max = sum [l]; <br/> return; <br/>}< br/> int mid = (L + r)> 1; <br/> buildtree (2 * Fa, L, mid); <br/> buildtree (2 * Fa + 1, Mid + 1, R ); <br/> segtree [fa]. _ max = max (segtree [2 * Fa]. _ max, segtree [2 * Fa + 1]. _ max); <br/>}< br/> void search (int fa, int L, int R) <br/> {<br/> If (segtree [fa]. L = L & segtree [fa]. R = r) <br/>{< br/> _ max = max (_ max, segtree [fa]. _ max); <br/> return; <br/>}< br/> int mid = (segtree [fa]. L + segtree [fa]. r)> 1; <br/> If (r <= mid) Search (2 * Fa, L, R); <br/> else if (L> mid) search (2 * Fa + 1, L, R); <br/> else <br/>{< br/> Search (2 * Fa, L, mid ); <br/> Search (2 * Fa + 1, Mid + 1, R); <br/>}< br/> int main () <br/>{< br/> // freopen ("in.txt", "r", stdin); <br/> while (scanf ("% d", & N) & N) <br/>{< br/> ans = inf; <br/> scanf ("% d", & L, & U ); <br/> sum [0] = 0; <br/> for (INT I = 1; I <= N; ++ I) <br/>{< br/> scanf ("% d", Σ [I]); <br/> sum [I] + = sum [I-1]; <br/>}< br/> buildtree (1,0, n); <br/> for (INT I = L; I <= N; ++ I) <br/>{< br/> _ max = (-1) * INF; <br/> Search (1, max (0, I-U ), i-l); <br/> If (sum [I]-_ max <ans) <br/> ans = sum [I]-_ max; <br/>}< br/> printf ("% d/N", ANS); <br/>}< br/> return 0; <br/>} 

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.