Spoj 2916 can you answer these queries v line segment tree to find the maximum child segment and

Source: Internet
Author: User

Question link: Click the open link

Question:

T Test Data

N numbers

Q inquiries

For each query: [X1, Y1] [X2, y2]

Q:

int ans = -inf;for(int i = x1; i <= y1; i++)for(int j = max(x2, i); j <= y2; j++)ans = max(ans, query(i, j));

Ideas:

Query_l (int l, int R) calculates the maximum child segment and the left endpoint of L within the range [L, R.

Similar to gss3.

Classification to discuss the given interval [X1, Y1] [X2, y2]

1. If two intervals are separated (Y1 <X2), the (Y1, X2) part is required, and the other part is expanded as much as possible.

2. If Y1 = x2, this point in the middle is required. The other two sides are either deselected or expanded as much as possible.

3. When the interval is retained, we can discuss the interval and divide it into [X1, Y1), [Y1, x2], (X2, y2], then [I, j: There are four situations that fall into these three intervals.

The problem with gss3.

The other two cases are similar to 2.

# Include <cstdio> # include <iostream> # include <algorithm> # include <string. h> # include <math. h >#include <vector >#include <map> using namespace STD; # define n 10005 # define lson (x) tree [X]. L # define rson (x) tree [X]. R # define L (x) (x <1) # define R (x) (x <1 | 1) # define sum (x) tree [X]. sum # define max (x) tree [X]. max # define Lmax (x) tree [X]. lmax # define rmax (x) tree [X]. rmaxstruct node {int L, R; int mid () {return (L + r)> 1;} int Lmax, rmax, Max, sum ;} tree [n <2]; int n, a [n], Q; int query_sum (int l, int R, int ID) {If (L = lson (ID) & rson (ID) = r) return sum (ID); int mid = tree [ID]. mid (); If (mid <L) return query_sum (L, R, R (ID); else if (r <= mid) return query_sum (L, R, L (ID); else return query_sum (L, mid, L (ID) + query_sum (Mid + 1, R, R (ID ));} void push_up (int id) {Lmax (ID) = max (Lmax (L (ID), sum (L (ID) + Lmax (R (ID ))); rmax (ID) = max (rmax (R (ID), sum (R (ID) + rmax (L (ID); sum (ID) = sum (L (ID) + sum (R (ID); max (ID) = max (L (ID )), max (R (ID), rmax (L (ID) + Lmax (R (ID);} void updata_point (INT Val, int ID) {Lmax (ID) = rmax (ID) = max (ID) = sum (ID) = val;} void build (int l, int R, int ID) {lson (ID) = L; rson (ID) = r; If (L = r) {updata_point (A [L], ID); return ;} int mid = tree [ID]. mid (); Build (L, mid, L (ID); Build (Mid + 1, R, R (ID); push_up (ID );} int query_l (int l, int R, int ID) {If (L = lson (ID) & rson (ID) = r) return Lmax (ID ); int mid = tree [ID]. mid (); If (mid <L) return query_l (L, R, R (ID); else if (r <= mid) return query_l (L, R, L (ID); int LANs = query_l (L, mid, L (ID), Rans = query_l (Mid + 1, R, R (ID )); return max (LANs, sum (L (ID) + RANS);} int query_r (int l, int R, int ID) {If (L = lson (ID) & rson (ID) = r) return rmax (ID); int mid = tree [ID]. mid (); If (mid <L) return query_r (L, R, R (ID); else if (r <= mid) return query_r (L, R, L (ID); int LANs = query_r (L, mid, L (ID), Rans = query_r (Mid + 1, R, R (ID )); return max (RANS, sum (R (ID) + LANs);} int query_l (int l, int R, int ID) {If (L = lson (ID) & rson (ID) = r) return Lmax (ID); int mid = tree [ID]. mid (); If (mid <L) return query_l (L, R, R (ID); else if (r <= mid) return query_l (L, R, L (ID); int LANs = query_l (L, mid, L (ID), Rans = query_l (Mid + 1, R, R (ID )); return max (LANs, query_sum (L, mid, ID) + RANS);} int query_r (int l, int R, int ID) {If (L = lson (ID) & rson (ID) = r) return rmax (ID); int mid = tree [ID]. mid (); If (mid <L) return query_r (L, R, R (ID); else if (r <= mid) return query_r (L, R, L (ID); int LANs = query_r (L, mid, L (ID), Rans = query_r (Mid + 1, R, R (ID )); return max (RANS, query_sum (Mid + 1, R, ID) + LANs);} int query (int l, int R, int ID) {If (L = lson (ID) & rson (ID) = r) return max (ID); int mid = tree [ID]. mid (); If (mid <L) return query (L, R, R (ID); else if (r <= mid) return query (L, R, L (ID); int LANs = query (L, mid, L (ID), Rans = query (Mid + 1, R, R (ID )); int ans = max (LANs, RANS); Return max (ANS, query_r (L, mid, L (ID) + query_l (Mid + 1, R, R (ID);} void solve () {scanf ("% d", & N); For (INT I = 1; I <= N; I ++) scanf ("% d", & A [I]); Build (1, n, 1); scanf ("% d", & Q); While (Q --) {int x1, x2, Y1, Y2, ans; scanf ("% d", & X1, & Y1, & X2, & Y2 ); if (Y1 <X2) {ans = query_r (x1, Y1, 1) + query_l (X2, Y2, 1); If (Y1 + 1 <= x2-1) ans + = query_sum (Y1 + 1, x2-1, 1);} else if (Y1 = x2) {ans = query_sum (Y1, X2, 1 ); if (X1 <= y1-1) ans + = max (0, query_r (x1, y1-1, 1); If (X2 + 1 <= Y2) ans + = max (0, query_l (X2 + 1, Y2, 1);} else {ans = query (X2, Y1, 1); // I, J is in the middle of int TMP = query_r (x1, x2, 1) + query_l (Y1, Y2, 1); // I, j All in 2 side if (X2 + 1 <= y1-1) TMP + = query_sum (X2 + 1, y1-1, 1); ans = max (ANS, TMP ); TMP = query_l (X2, Y2, 1); If (X1 <= x2-1) TMP + = query_r (x1, x2-1, 1); ans = max (ANS, TMP ); TMP = query_r (x1, Y1, 1); If (Y1 + 1 <= Y2) TMP + = query_l (Y1 + 1, Y2, 1); ans = max (ANS, TMP);} printf ("% d \ n", ANS) ;}int main () {int t; scanf ("% d", & T ); while (t --) solve (); Return 0 ;}


Spoj 2916 can you answer these queries v line segment tree to find the maximum child segment and

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.