LA3938: "Ray, Pass me the dishes!" (Line segment tree)

Source: Internet
Author: User

Description

After doing Ray a great favor to collect sticks for Ray, Poor Neal becomes very hungry. In return for Neal's help, Ray makes a great dinner for Neal. When it was time for dinner, Ray arranges all the dishes he makes in a single line (actually this line is very long) . . , the dishes is represented by 1, 2, 3 ... ). ' Make me work hard and don ' t pay me! You refuse to teach me Latin dance! Now it's time for your to serve me ", Neal says to himself.

Every dish has it own value represented by a integer whose absolute value is less than 1,000,000,000. Before had dinner, Neal was wondering about the total value of the dishes he would eat. So he raises many questions is about the values of dishes he would has.

For each question Neal asks, he would first write down the interval [a, b] (inclusive) to Represe NT all the dishes a, a + 1,..., b , where a and b is positive integ ERS, and then asks Ray which sequence of consecutive dishes in the interval have the most total value. Now Ray needs your help.

Input

The input file contains multiple test cases. For each test case, there is integers n and m in the first line (n, m < 50 0000) . n is the number of dishes and m are the number of questions Neal asks.

Then n numbers come on the second line, which is the values of the dishes from left to right. Next m Lines is the questions and each line contains the numbers a , b as described above. Proceed to the end of the input file.

Output

For each test case, output m lines. Each line contains the numbers, indicating the beginning position and end position of the sequence. If There is multiple solutions, output the one with the smallest beginning position. If There is still multiple solutions then, just output of the one with the smallest end position. Please output the result as in the Sample output.

Sample Input

3 1 1 2 3 1 1

Sample Output

Case 1:1 1
Test instructions
Given the length of the integer sequence of n, and then M inquiry, for each query, asked to find the interval of two subscript x, y to make the interval and as large as possible, if there are many solutions, x as small as possible, and many solutions, then y also as small as possible
Ideas:
Line tree, to be marked in the process of achievement
Max_all: Maximum continuous and
Max_prefix: Maximum prefix and
Max_suffix: Maximum suffix and
Pre_r: The end position of the maximum prefix
suf_l: The starting position of the maximum suffix
Sum: Sum of intervals
For the update, because X as small as possible and y as small as possible, so in the update we have to determine the order of the update, that is, l try to be small, then y as small as possible
There are 5 conditions for updates, so in order
1. The max_prefixon the left sum+ right interval;
2. Left interval Max_all
3. Max_prefix on the right side of the max_suffix+ on the left
4. The sum to the right of the max_suffix+ on the left
5. Max_all on the right
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue > #include <map> #include <set> #include <vector> #include <math.h> #include <bitset># Include <algorithm> #include <climits>using namespace std, #define Lson 2*i#define Rson 2*i+1#define LS L,mid, Lson#define RS mid+1,r,rson#define Up (i,x,y) for (i=x;i<=y;i++) #define OFF (i,x,y) for (i=x;i>=y;i--) #define MEM (  a,x) memset (A,x,sizeof (a)) #define W (a) while (a) #define GCD (A, B) __gcd (A, b) #define LL long long#define N 500005#define MOD 1000000007#define INF 0x3f3f3f3f#define EXP 1e-8#define lowbit (x) (x&-x) LL s[n];int n,m;struct node{int L,r,pre_r    , suf_l; LL Max_all,max_prefix,max_suffix,sum;}    a[n<<2];void pushup (int l,int r,int i) {int mid = (l+r)/2;    A[i].sum = A[lson].sum+a[rson].sum;    A[i].max_prefix = A[lson].max_prefix;    A[i].pre_r = A[lson].pre_r;    A[i].max_suffix = A[rson].max_suffix; a[i].suf_l = A[rson].suf_l;//1. Max_prefix on the left sum+ right interval;    A[i].max_all = A[lson].sum+a[rson].max_prefix;    A[I].L = l; A[I].R = A[rson].pre_r;//2. Left interval max_all if (A[i].max_all<a[lson].max_all | |    (A[I].MAX_ALL==A[LSON].MAX_ALL&AMP;&AMP;A[I].L==A[LSON].L))        {a[i].max_all = A[lson].max_all;        A[I].L = A[LSON].L;    A[I].R = A[LSON].R; }//3. max_suffix+ to the right of the left Max_prefix if (A[i].max_all<a[lson].max_suffix+a[rson].max_prefix | |    (A[i].max_all==a[lson].max_suffix+a[rson].max_prefix &&a[i].l>a[lson].suf_l))        {a[i].max_all=a[lson].max_suffix+a[rson].max_prefix;        A[I].L = a[lson].suf_l;    A[I].R = A[rson].pre_r; }//4. max_suffix+ to the right of the left side of the sum if (A[i].max_all<a[lson].max_suffix+a[rson].sum | |    (A[i].max_all==a[lson].max_suffix+a[rson].sum &&a[i].l>a[lson].suf_l))        {a[i].max_all=a[lson].max_suffix+a[rson].sum;        A[I].L = a[lson].suf_l;    A[I].R = R; }//5. Max_all If on the right (A[i].max_all<a[rson].max_all | | (A[i].max_all==a[rson].max_all&&a[i].l>a[rson].suf_l)) {a[i].max_all = A[rson].max_all;        A[I].L = A[RSON].L;    A[I].R = A[RSON].R; }//update prefix and suffix position if (a[lson].sum+a[rson].max_prefix>a[i].max_prefix) {A[i].max_prefix=a[lson].sum+a[rson].max        _prefix;    A[i].pre_r = A[rson].pre_r;        } if (A[rson].sum+a[lson].max_suffix>=a[i].max_suffix) {a[i].max_suffix=a[rson].sum+a[lson].max_suffix;    a[i].suf_l = a[lson].suf_l; }}void init (int l,int r,int i) {if (L = = r) {A[i].max_all = A[i].max_prefix = A[i].max_suffix = A[i].sum = s[        L];        A[I].L = A[I].R = A[i].pre_r = a[i].suf_l = l;    return;    } int mid = (l+r)/2;    Init (LS);    Init (RS); Pushup (l,r,i);}    node query (int i,int l,int r,int l,int R) {if (l==l&&r==r) {return a[i];    } int mid= (L+R) >>1;    if (r<=mid) return query (LSON,L,R,L,MID);    else if (l>mid) return query (RSON,L,R,MID+1,R);       else { Node T1=query (lson,l,mid,l,mid);        Node T2=query (rson,mid+1,r,mid+1,r);        Node T;        T.sum = T1.sum+t2.sum;        T.max_prefix = T1.max_prefix;        T.pre_r = T1.pre_r;        T.max_suffix = T2.max_suffix;        t.suf_l = t2.suf_l;        T.max_all = T1.sum+t2.max_prefix;        T.L = l;        T.R = T2.pre_r; if (T.max_all<t1.max_all | |        (T.MAX_ALL==T1.MAX_ALL&AMP;&AMP;T.L==T1.L))            {t.max_all = T1.max_all;            T.L = T1.L;        T.R = T1.R; } if (T.max_all<t1.max_suffix+t2.max_prefix | |        (T.max_all==t1.max_suffix+t2.max_prefix &&t.l>t1.suf_l))            {t.max_all=t1.max_suffix+t2.max_prefix;            T.L = t1.suf_l;        T.R = T2.pre_r; } if (T.max_all<t1.max_suffix+t2.sum | |        (T.max_all==t1.max_suffix+t2.sum &&t.l>t1.suf_l))            {t.max_all=t1.max_suffix+t2.sum;            T.L = t1.suf_l;        T.R = T2.R; } if (t.max_all<t2.Max_all | |        (T.MAX_ALL==T2.MAX_ALL&AMP;&AMP;T.L&GT;T2.L))            {t.max_all = T2.max_all;            T.L = T2.L;        T.R = T2.R;            } if (T1.sum+t2.max_prefix>t.max_prefix) {t.max_prefix=t1.sum+t2.max_prefix;        T.pre_r = T2.pre_r;            } if (T2.sum+t1.max_suffix>=t.max_suffix) {t.max_suffix=t2.sum+t1.max_suffix;        t.suf_l = t1.suf_l;    } return t;    }}int Main () {int i,j,l,r,cas = 1;        while (~SCANF ("%d%d", &n,&m)) {for (i = 1; i<=n; i++) scanf ("%lld", &s[i]);        Init (1,n,1);        printf ("Case%d:\n", cas++);            while (m--) {scanf ("%d%d", &l,&r);            Node T=query (1,l,r,1,n);        printf ("%d%d\n", T.L,T.R); }} return 0;}


LA3938: "Ray, Pass me the dishes!" (Line segment tree)

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.