Problem 2136 Fetch Candy---fuoj (line tree + maintenance)

Source: Internet
Author: User

http://acm.fzu.edu.cn/problem.php?pid=2136

The main idea: to give you n a bag with candy in each bag, and then you can take a continuous bag of one interval at a time, then carry the bag with the maximum number of candies in it.

Find the interval length from 1 to n the number of candies you can take away the worst case scenario is the minimum value of the maximum value for all intervals.

Analysis: If you look up from a small start, he will be the maximum of the interval whenever he finds the interval he can cover, so we can query this point every time.

#include <stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include<ctype.h>#include<iostream>#include<map>using namespacestd;#defineINF 1e9+7#defineLson r<<1|1#defineRson r<<1#defineN 101000intAns[n];structnode{intL,r,la,lb,max;///LA is the number of consecutive left of this interval, LB is to the right and Max is the largest continuous sequence of intervals.    intmid () {return(L+R)/2; }    intLen () {return(r-l+1); }}a[n*Ten];structnode{intx, y;} P[n];intCMP (Node c,node d) {returnc.x<d.x;}voidBuildtree (intRintLintR) {A[R].L=L; A[R].R=R; A[r].la= a[r].lb = A[r]. Max =0; if(L = =R)return;    Buildtree (Lson, L, A[r].mid ()); Buildtree (Rson, A[r].mid ()+1, R);}voidQurry (intRintXinty) {    if(A[R].L = =A[R].R) {a[r].la= a[r].lb = A[r]. Max =1; return; }    if(Y >A[r].mid ())    Qurry (Rson, x, y); ElseQurry (Lson, x, y); A[R]. Max= Max (A[lson]. Max, Max (A[rson). Max, a[lson].lb+a[rson].la)); A[r].la=a[lson].la; a[r].lb=a[rson].lb; if(a[lson].la = =A[lson].len ()) a[r].la+=a[rson].la; if(a[rson].lb = =A[rson].len ()) a[r].lb+=a[lson].lb;}intMain () {intT; scanf ("%d", &T);  while(T--)    {        intN; scanf ("%d", &N); memset (P,0,sizeof(P));  for(intI=0; i<n; i++) {scanf ("%d", &p[i].x); P[i].y=i; } buildtree (1,0, N-1); Sort (p, p+N, CMP); intm=1; memset (ans,0,sizeof(ans));  for(intI=0; i<n; i++) {Qurry (1, p[i].x, P[I].Y);///Query the maximum sequence of y points            inttmp=a[1].            Max;  while(M <=tmp) Ans[m++]=p[i].x; }         for(intI=1; i<=n; i++) printf ("%d\n", Ans[i]); }    return 0;}

Problem 2136 Fetch Candy---fuoj (line tree + maintenance)

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.