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

Re:贴个傻逼线段树!!!

Posted by SONGS at 2013-01-05 07:24:29 on Problem 2823
In Reply To:贴个傻逼线段树!!! Posted by:wocha at 2012-08-15 10:24:19
/*
ID:songskg
PROB:zkw-supertree
LANG:C++
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int M=1048576;
const int max_=1<<29;
typedef struct
	{
		int min,max;
	}node;
node tree[M*2];
int n,k;

int query_max(int s,int t)
	{
		int ans=-max_;
		for (s+=M-1,t+=M+1; s^t^1; s>>=1,t>>=1)
			{
				if (~s&1) ans=max(ans,tree[s^1].max);
				if (t&1) ans=max(ans,tree[t^1].max);
			}
		return ans;
	}
	
int query_min(int s,int t)
	{
		int ans=max_;
		for (s+=M-1,t+=M+1; s^t^1; s>>=1,t>>=1)	
			{
				if (~s&1) ans=min(ans,tree[s^1].min);
				if (t&1) ans=min(ans,tree[t^1].min);
			}
		return ans;
	}

void init()
	{
		scanf("%d %d",&n,&k);
		for (int i=1; i<=n; ++i)
			{
				scanf("%d",&tree[M+i].max);
				tree[M+i].min=tree[M+i].max;
			}
		for (int i=M-1; i>=1; --i)
			{
				tree[i].min=min(tree[i*2].min,tree[i*2+1].min);
				tree[i].max=max(tree[i*2].max,tree[i*2+1].max);
			}
		for (int i=1; i<=n-k+1; ++i)
			printf("%d ",query_min(i,i+k-1));
		printf("\n");
		for (int i=1; i<=n-k+1; ++i)
			printf("%d ",query_max(i,i+k-1));
		printf("\n");
	}
	
int main()
	{
		init();
		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