1. 程式人生 > >POJ 2823 線段樹區間查詢

POJ 2823 線段樹區間查詢

Sliding Window
Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 49775 Accepted: 14360
Case Time Limit: 5000MS

Description

An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example: 
The array is [1 3 -1 -3 5 3 6 7]
, and k is 3.
Window position Minimum value Maximum value
[1  3  -1] -3  5  3  6  7  -1 3
 1 [3  -1  -3] 5  3  6  7  -3 3
 1  3 [-1  -3  5] 3  6  7  -3 5
 1  3  -1 [-3  5  3] 6  7  -3 5
 1  3  -1  -3 [5  3  6] 7  3 6
 1  3  -1  -3  5 [3  6  7] 3 7

Your task is to determine the maximum and minimum values in the sliding window at each position. 

Input

The input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line. 

Output

There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values. 

Sample Input

8 3
1 3 -1 -3 5 3 6 7

Sample Output

-1 -3 -3 -3 3 3
3 3 5 5 6 7

Source

題意:求長度為k連續區間的最值,詳情見題目

題解:構造線段樹,然後向上更新,定義2個全域性變數去儲存區間的最值,最後壓入陣列輸出就可以啦。現在做這一題還是比較輕鬆的,一次AC。搜了一下還可以用單調佇列做解決。還是看不太懂

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define N 1000005
#define inf 0x3f3f3f3f
int Ma,Mi;
vector<int>ax;
vector<int>in;
struct point
{
	int lc,rc,max,min,va;
}tree[N<<2];
void Pushup(int rt)
{
	tree[rt].max=max(tree[rt<<1].max,tree[rt<<1|1].max);
	tree[rt].min=min(tree[rt<<1].min,tree[rt<<1|1].min);
}
void build(int L,int R,int rt)
{
	tree[rt].lc=L;
	tree[rt].rc=R;
	tree[rt].max=-inf;
	tree[rt].min=inf;
	if(L==R)
	{
		scanf("%d",&tree[rt].va);
		tree[rt].max=tree[rt].min=tree[rt].va;
		return ;
	}
	int mid=(L+R)>>1;
	build(L,mid,rt<<1);
	build(mid+1,R,rt<<1|1);
	Pushup(rt);
}
void query(int L,int R,int rt)
{
	if(L==tree[rt].lc&&R==tree[rt].rc)
	{
		Ma=max(tree[rt].max,Ma);
		Mi=min(tree[rt].min,Mi);
		return ;
	}
	int mid=(tree[rt].lc+tree[rt].rc)>>1;
	if(R<=mid)query(L,R,rt<<1);
	else if(L>mid)query(L,R,rt<<1|1);
	else
	{
		query(L,mid,rt<<1);
		query(mid+1,R,rt<<1|1);
	}
}
int main()
{
#ifdef CDZSC
	freopen("i.txt","r",stdin);
#endif
	int n,k;
	while(~scanf("%d%d",&n,&k))
	{
		ax.clear();
		in.clear();
		build(1,n,1);
		for(int i=1;i<=n-k+1;i++)
		{
			Ma=-inf;
			Mi=inf;
			query(i,k+i-1,1);
			ax.push_back(Ma);
			in.push_back(Mi);
		}
#ifdef CDZSC
		printf("%d %d\n",in.size(),ax.size());
#endif
		int fi=1;
		for(int i=0;i<in.size();i++)
		{
			if(fi)
			{
				printf("%d",in[i]);
				fi=0;
			}
			else
				printf(" %d",in[i]);
		}
		printf("\n");
		fi=1;
		for(int i=0;i<ax.size();i++)
		{
			if(fi)
			{
				printf("%d",ax[i]);
				fi=0;
			}
			else
				printf(" %d",ax[i]);
		}
		printf("\n");
	}
	return 0;
}