Bzoj 1699: [Usaco2007 jan]balanced Lineup Queued Tiles

Source: Internet
Author: User

1699: [Usaco2007 jan]balanced lineup queue time limit:5 Sec Memory limit:64 MB
Description

Every day, farmer John's N (1 <= n <= 50,000) Cows are always queued in the same sequence. One day, John decided to let some cows play a Frisbee game. He was going to find a group of cows that were set up in the column to play. But in order to avoid the level of disparity, the height of the cow should not be too large. John prepares q (1 <= q <= 180,000) for the choice of possible cattle and the height of all cows (1 <= height <= 1,000,000). He wanted to know the height difference between the highest and lowest cows in each group. Note: On the largest data, the input and output will occupy most of the elapsed time.

Input

* First line: N and Q. * 2nd. N+1: Line i+1 is the height of the first cow.

* Line n+2..n+q+1: Two integers, A and B (1 <= a <= b <= N), representing all cows from A to B.

Output

* 1th. Q Line: All inquiries answered (the highest and lowest of the cow's height), one per line.

Sample Input6 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output6
3
0
HINT Source

Gold

Test instructions: Interval maximum-interval minimum;

Ideas: Block writing, the first question;

#include <bits/stdc++.h>using namespaceSTD; #define LL Long Long#define Pi (4*atan (1.0)) const int N=2E5+10,M=4E6+10,INF=1E9+10; intMaxx[n],a[n],k,si,minn[n];void Init (intx) {for (int i=1;i<=si;i++) {minn[i]=Inf for (int t= (i-1) *k+1;t<=i*k;t++) {maxx[i]=Max (A[t],maxx[i]); minn[i]=Min (A[t],minn[i]); }}}int workmax (int l,int r,intx) {int lk= (l/k) + (l%k!=0?1:0); int rk= (r/k) + (r%k!=0?1:0); int ans=0; if (lk==RK) {for (int i=l;i<=r;i++) ans=Max (Ans,a[i]); ReturnAns } for (int i=l;i<=lk*k;i++) ans=Max (Ans,a[i]); for (int i= (rk-1) *k+1;i<=r;i++) ans=Max (Ans,a[i]); for (int i=lk+1;i<=rk-1;i++) ans= Max (Ans,maxx[i]); return  ans;} int workmin (int l,int r,int  x) {int lk= (l/k) + (l%k!=0?1:0 ); int rk= (r/k) + (r%k!=0?1:0 ); int ans=  Inf if (lk==  rk) {for (int i=l;i<=r;i++ ) ans=  min (ans,a[i]), return  ans;} for (int i=l;i<=lk*k;i + + ) ans=  min (ans,a[i]), for (int i= (rk-1) *k+1;i<=r;i++ ) ans=  min (ans,a[i]), for (int i=lk+1;i <=rk-1;i++ ) ans=  min (ans,minn[i]); return  ans; int  Main () {int  x,y,z,i,t,q; while (~scanf ("%d%d", &x,&  Q)} {for (i=1;i<=x;i++ ) scanf ("%d", &  a[i]), k= (int ) sqrt (x); si=x/ K; init (x); while (q--) {int  l,r; scanf ("%d%d", &l,&  R); printf ("%d\n", Workmax (l,r,x)- workmin (l,r,x));}} return 0 ;}                

Bzoj 1699: [Usaco2007 jan]balanced Lineup Queued Tiles

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.