Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

线段树11s,之前用queue存储数据不能过。不知道怎么优化了

Posted by zw1000 at 2016-10-29 14:20:30 on Problem 2823
#include<iostream>
#include<queue>
#define N 1000010
using namespace std;
struct node{
	int l;
	int r;
	int max;
	int min;
}line[3*N+1000];
int a[N+2];
int k=1;
int h=1;
int mmax;
int mmin;
int qmax[N+2];
int qmin[N+2];
int q1=0;
int q2=0;

int themax(int a,int b){
	return a>b?a:b;
}

int themin(int a,int b){
	return a<b?a:b;
}

void buildTree(int index,int l,int r){
	line[index].l=l;
	line[index].r=r;
	if(l>=r){
		line[index].max=a[h];line[index].min=a[h];
		h++;
		return;
	}
	buildTree(index<<1,l,(l+r)>>1);
	buildTree((index<<1)+1,((l+r)>>1)+1,r);
	line[index].max=themax(line[(index<<1)].max,line[(index<<1)+1].max);
	line[index].min=themin(line[(index<<1)].min,line[(index<<1)+1].min);
}

void search(int index,int l,int r){
	if(l<=line[index].l&&r>=line[index].r){
		mmax=themax(mmax,line[index].max);
		mmin=themin(mmin,line[index].min);
		return;
	}
	if(r<=(line[index].l+line[index].r)>>1){
		search(index<<1,l,r);
	}else if(l>=((line[index].l+line[index].r)>>1)+1){
		search((index<<1)+1,l,r);
	}else{
		search(index<<1,l,(line[index].l+line[index].r)>>1);
		search((index<<1)+1,((line[index].l+line[index].r)>>1)+1,r);
	}
	
}
int main(){
	int n,m,i;
	while(~scanf("%d%d",&n,&m)){
		q1=q2=0;
		h=1;
		for(i=1;i<=n;i++){
			cin>>a[i];
		}	
		buildTree(1,1,n);
		for(i=1;i<=n-m+1;i++){
			mmax=-1000000000;mmin=1000000000;
			search(1,i,i+m-1);
			qmax[q1++]=mmax;
			qmin[q2++]=mmin;
		}
		for(i=0;i<q2;i++){
			if(i==q2-1){
				printf("%d\n",qmin[i]);
			}else{
				printf("%d ",qmin[i]);
			}
		}
		for(i=0;i<q1;i++){
			if(i==q1-1){
				printf("%d\n",qmax[i]);
			}else{
				printf("%d ",qmax[i]);
			}
		}
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator