Codeforces 484E. Sign on Fence to persist segment tree

Source: Internet
Author: User



Probably test instructions:

Given an array of a, ask in an interval l~r, what is the maximum value for the smallest number in a contiguous period of W.



Obviously it can be turned into a two-point height and then judge the feasibility problem.


Considering the height of a certain value in the array, sort the arrays from large to small. To build n tree segment tree, for the first tree segment tree, the value of the leaves greater than or equal to A[i] is set to 1, the other leaves are set to 0, the problem is transformed into a segment tree to find the longest continuous 1 of the number of segments, this is a classical problem of a segment tree, you can maintain the left of each node continuous and , right continuous and , continuous and maximum values are obtained.


Because of the space problem, it is impossible to establish a 10^5 tree segment, considering that there are only one side of the adjacent two segment tree value is not the same, you can share other nodes to save space, so build a durable line tree can be, specific details can see the code.



E. Sign on fencetime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Biz On the Champion have recently finished painting his wood fence. The fence consists of a sequence Of  n  panels of  i -th panel ' s height is  h i  meters. The adjacent planks follow without a gap between them.

Aft Er Bizon painted the fence he decided to put a ' for sale ' sign on it. The sign would be drawn on a rectangular piece of paper and placed on the fence so that the sides of the sign is parallel To the fence panels and is also aligned with the edges of some panels. Bizon the Champion introduced the following constraints for the sign position:

      the width of the sign should be Exactly  the sign must fit into the Segment of the fence from The 

The sign would be really pretty, so bizon the Champion wants the sign's height to be as large as possible.

You is given the description of the fence and several queries for placing sign. For each query, print the maximum possible height of the sign so can be placed on the corresponding segment of the fence With the given fixed width of the sign.

Input

The First line of the input contains Integer 5 ).

The Second line contains  n  space-separated integers  h i ,  -the heights of the panels ( i ? ≤?109 ).

The third line contains a integer m -the number of the queries (1?≤? M.≤?105).

The nextmLines contain the descriptions of the queries, each query was represented by three integersL,RandW(1?≤? l? ≤? r≤? N ,1?≤? w? ≤? R?-? l. +?1)-the segment of the fence and the width of the sign respectively.

Output

For each query, print the answer on a separate line-the maximum height of the "that" can be put in the corresponding s Egment of the fence with all the conditions being satisfied.

Sample Test (s) input
51 2 2 3 332 5 32 5 21 5 5
Output
231
Note

The fence described in the sample looks as follows:

The possible positions for the signs-all queries is given below.

The optimal position for the first query. The optimal position for the second query. The optimal position for the third query.
/* ***********************************************author:ckbosscreated time:2015 March 11 Wednesday 19:18 54 seconds file Name : cf484e_2.cpp************************************************ * * #include <iostream> #include <cstdio># Include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib > #include <vector> #include <queue> #include <set> #include <map>using namespace Std;const int Maxn=100100;const int maxm=maxn*40;int n;int CH[MAXM][2],LEFTSUM[MAXM],RIGHTSUM[MAXM],ALLSUM[MAXM],LENTH[MAXM]; int t[maxn],tot,res,toleft;struct fe{int H,id;} Fe[maxn];bool CMP (Fe A,fe b) {return a.h>b.h;} Push_upvoid push_up (int rt) {Lenth[rt]=lenth[ch[rt][0]]+lenth[ch[rt][1]];leftsum[rt]=leftsum[ch[rt][0]];if ( Leftsum[ch[rt][0]]==lenth[ch[rt][0]]) leftsum[rt]+=leftsum[ch[rt][1]];rightsum[rt]=rightsum[ch[rt][1]];if ( RIGHTSUM[CH[RT][1]]==LENTH[CH[RT][1]]) Rightsum[rt]+=rightsum[ch[rt][0]];allsum[rt]=max (allsum[ch[rt][0]],allSUM[CH[RT][1]]); Allsum[rt]=max (allsum[rt],rightsum[ch[rt][0]]+leftsum[ch[rt][1]);} Buildint Build (int l,int r) {int rt=tot++;if (l==r) {Leftsum[rt]=rightsum[rt]=allsum[rt]=0;lenth[rt]=1;return rt;} int m= (L+R)/2;ch[rt][0]=build (l,m), Ch[rt][1]=build (m+1,r);p ush_up (RT), return RT;} Insertint Insert (int oldrt,int pos,int l,int r) {int rt=tot++;ch[rt][0]=ch[oldrt][0];ch[rt][1]=ch[oldrt][1];if (l== Pos&&r==pos) {Leftsum[rt]=rightsum[rt]=allsum[rt]=1;lenth[rt]=1;return rt;} int m= (L+R)/2;if (pos<=m) Ch[rt][0]=insert (ch[oldrt][0],pos,l,m); else Ch[rt][1]=insert (CH[OLDRT][1],POS,M+1,R); PUSH_UP (RT); return RT;} queryvoid query (int rt,int l,int r,int l,int R) {if (l<=l&&r<=r) {Res=max (res,allsum[rt]); toleft+= Leftsum[rt];res=max (Res,toleft); if (Leftsum[rt]!=lenth[rt]) Toleft=rightsum[rt];res=max (res,toleft); return;} int m= (L+R)/2;if (r<=m) return query (CH[RT][0],L,R,L,M), else if (l>m) return query (CH[RT][1],L,R,M+1,R); else{ Query (ch[rt][0],l,r,l,m); query (ch[rt][1],l,r,m+1,r);}}int Main () {//freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "w", stdout), scanf ("%d", &n), for (int i=1;i<=n;i++) {int x;scanf ("%d", &x); fe[i].h=x; Fe[i].id=i;} Sort (fe+1,fe+1+n,cmp); T[0]=build (1,n), for (int i=1;i<=n;i++) T[i]=insert (t[i-1],fe[i].id,1,n), int t_t;scanf ("%d", &t_t), and while (T_ t--) {int l,r,w;scanf ("%d%d%d", &l,&r,&w); int Low=1,high=n,ans;while (low<=high) {int mid= (Low+high)/2 ; Toleft=res=0;query (t[mid],l,r,1,n); if (res>=w) {ans=mid; high=mid-1;} else low=mid+1;}    printf ("%d\n", fe[ans].h);} return 0;}







Codeforces 484E. Sign on Fence to persist 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.