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

划分树水之!!!

Posted by wocha at 2012-08-15 16:31:31 on Problem 2104
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#define stop system("pause")
#define M 1000004+84
using namespace std;
struct no{
	int  val[M],sum[M],deep;
}tree[23];
struct se{
	int x, y;
}a[M];
bool cmp(se s,se d){
	return s.x<d.x;
}
void build(int h,int l,int r){
	if(l==r) return;
	int mid=(l+r)>>1,p=0,t=0;
	for(int i=l;i<=r;i++){
		if(tree[h].val[i]<=mid){
		  tree[h+1].val[l+p]=tree[h].val[i];
	   	  p++;
	      tree[h].sum[i]=p;	
		}else {
		  tree[h+1].val[mid+1+t]=tree[h].val[i];
		  t++;
		  tree[h].sum[i]=p;	
		}
	}
	build(h+1,l,mid);
	build(h+1,mid+1,r);
}
int find(int h,int l,int r,int ll,int rr,int k)
{
    
if (ll==rr) return (tree[h].val[ll]);
    int ls=0,rs=0;
    if (ll>l) 
	ls=tree[h].sum[ll-1];
	else ls=0;
    int mid=(l+r)>>1;
	rs=tree[h].sum[rr];
    if (rs-ls>=k) return find(h+1,l,mid,l+ls,l+rs-1,k);
    else return
	 find(h+1,mid+1,r,mid+1+ll-l-ls,mid+rr-l+1-rs,k-(rs-ls));
	 if(ll==rr) return  tree[h].val[ll];
	 
	  
}

int main(){
	int n,m;
	while(~scanf("%d%d",&n,&m)){
		int t=0;
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i].x);
			a[i].y=i;
		}
		sort(a+1,a+n+1,cmp);
		for(int i=1;i<=n;i++){
			tree[0].val[a[i].y]=i;
		}
		build(0,1,n);
		while(m--){
			int q,w,k;
			scanf("%d%d%d",&q,&w,&k);
			printf("%d\n",a[find(0,1,n,q,w,k)].x);
		}
	}
}

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