一:
A - Balanced Lineup POJ - 3264
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.
Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.
Input
Line 1: Two space-separated integers, N and Q.
Lines 2.. N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2.. N+ Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.
Output
Lines 1.. Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.
Sample Input
6 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
//早上专门看视频学了线段树,这道题就是线段树的模板题,就是线段数节点数少乘了2,wa了一次,代码不多说了,如下:
#include<cstdio>
#include<iostream>
#include<algorithm>
#define N 50005
using namespace std;
int n,m;
int a[50005];
int ans_x,ans_n;
struct node
{
int l,r;
int maxx,minn;
}tree[N*4];
void build(int l,int r,int p)
{
tree[p].l=l;
tree[p].r=r;
tree[p].maxx=0;
tree[p].minn=1000005;
if(l==r){tree[p].maxx=tree[p].minn=a[l];return;}
int mid=(l+r)/2;
build(l,mid,p*2);
build(mid+1,r,p*2+1);
tree[p].maxx=max(tree[p*2].maxx,tree[p*2+1].maxx);
tree[p].minn=min(tree[p*2].minn,tree[p*2+1].minn);
}
void query(int l,int r,int p)
{
if(tree[p].l==l&&tree[p].r==r)
{
ans_x=max(ans_x,tree[p].maxx);
ans_n=min(ans_n,tree[p].minn);
return;
}
int mid=(tree[p].l+tree[p].r)/2;
if(r<=mid)
query(l,r,p*2);
else if(l>mid)
query(l,r,p*2+1);
else
{
query(l,mid,p*2);
query(mid+1,r,p*2+1);
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
build(1,n,1);
while(m--)
{
int l,r;
scanf("%d%d",&l,&r);
ans_x=0;
ans_n=1000005;
query(l,r,1);
printf("%d\n",ans_x-ans_n);
}
}
return 0;
}
二:
B - Maximum Absurdity
Reforms continue entering Berland. For example, during yesterday sitting the Berland Parliament approved as much as n laws (each law has been assigned a unique number from 1 to n). Today all these laws were put on the table of the President of Berland, G.W. Boosch, to be signed.
This time mr. Boosch plans to sign 2k laws. He decided to choose exactly two non-intersecting segments of integers from 1 to n of length k and sign all laws, whose numbers fall into these segments. More formally, mr. Boosch is going to choose two integers a, b (1 ≤ a ≤ b ≤ n - k + 1, b - a ≥ k) and sign all laws with numbers lying in the segments [a; a + k - 1] and [b; b + k - 1] (borders are included).
As mr. Boosch chooses the laws to sign, he of course considers the public opinion. Allberland Public Opinion Study Centre (APOSC) conducted opinion polls among the citizens, processed the results into a report and gave it to the president. The report contains the absurdity value for each law, in the public opinion. As mr. Boosch is a real patriot, he is keen on signing the laws with the maximum total absurdity. Help him.
Input
The first line contains two integers n and k (2 ≤ n ≤ 2·105, 0 < 2k ≤ n) — the number of laws accepted by the parliament and the length of one segment in the law list, correspondingly. The next line contains n integers x1, x2, ..., xn — the absurdity of each law (1 ≤ xi ≤ 109).
Output
Print two integers a, b — the beginning of segments that mr. Boosch should choose. That means that the president signs laws with numbers from segments [a; a + k - 1]and [b; b + k - 1]. If there are multiple solutions, print the one with the minimum number a. If there still are multiple solutions, print the one with the minimum b.
Examples
Input
5 2 3 6 1 1 6
Output
1 4
Input
6 2 1 1 1 1 1 1
Output
1 3
Note
In the first sample mr. Boosch signs laws with numbers from segments [1;2] and [4;5]. The total absurdity of the signed laws equals 3 + 6 + 1 + 6 = 16.
In the second sample mr. Boosch signs laws with numbers from segments [1;2] and [3;4]. The total absurdity of the signed laws equals 1 + 1 + 1 + 1 = 4.
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll,int> pii;
int n,k;
ll sum[200005],b[200005];
int a[200005];
pii maxx(pii a,pii b)
{
if(a.first!=b.first)return max(a,b);
return pii(a.first,min(a.second,b.second));
}
struct node
{
int l,r;
pii mx;
}tree[200005*4];
void build(int l,int r,int p)
{
tree[p].l=l,tree[p].r=r;
if(l==r)
{
tree[p].mx.first=b[l];
tree[p].mx.second=l;
return ;
}
int mid=(l+r)/2;
build(l,mid,p*2);
build(mid+1,r,p*2+1);
tree[p].mx=maxx(tree[p*2].mx,tree[p*2+1].mx);
}
pii query(int l,int r,int p)
{
if(tree[p].l>=l&&tree[p].r<=r)return tree[p].mx;
if(tree[p].l>r||tree[p].r<l)return pii(-1,-1);
return maxx(query(l,r,p*2),query(l,r,p*2+1));
}
int main()
{
cin>>n>>k;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
}
for(int i=1;i<=n-k+1;i++)
b[i]=sum[i+k-1]-sum[i-1];
build(1,n+1-k,1);
int x,y;
ll mx=0;
for(int i=k+1;i<=n-k+1;i++)
{
pii now=query(1,i-k,1);
if(b[i]+now.first>mx)
{
x=now.second;
y=i;
mx=b[i]+now.first;
}
}
printf("%d %d\n",x,y);
return 0;
}