POJ2182 segment tree maintenance interval values are available

The meaning of problems

Below you n n -. 1 represents a number from 2 to n-th - how many number of cows has less than its i-th front cow

N ask you this arrangement only the cows.

Thinking

No segment tree maintenance intervals can be used there are several.

From the previously enumerated ans [i] is every time a [i] in front of available answers

After finding the answer to this one segment tree has been updated to delete numbers used

Accode

#include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;
const int MaxN = 8e4 + 5;
const int inf = 0x3f3f3f3f;

int n;
int a[MaxN],ans[MaxN];

struct NODE{
	int l,r,w;
}tree[MaxN];

void build(int k,int ll,int rr){
	tree[k].l = ll;
	tree[k].r = rr;
	tree[k].w = rr - ll + 1;
	if(ll == rr){
		return ;
	}
	int m = (ll + rr) / 2;
	build(2 * k,ll,m);
	build(2 * k + 1,m + 1,rr);
}

int ask_p(int k,int x){
	if(tree[k].l == tree[k].r){
		return tree[k].l;
	}
	if(tree[k * 2].w >= x) return ask_p(k * 2,x);
	else return ask_p(k * 2 + 1,x - tree[k * 2].w);//已经继承前半部分
}

void chan_p(int k,int x){
	if(tree[k].l == tree[k].r){
		tree[k].w = 0;
		return ;
	}
	int m = (tree[k].l + tree[k].r) / 2;
	if(x <= m) chan_p(k * 2,x);
	else chan_p(k * 2 + 1,x);
	tree[k].w = tree[k * 2].w + tree[k * 2 + 1].w;
}

int main()
{
	scanf("%d",&n);
	for(int i = 2;i <= n; i++) scanf("%d",&a[i]);
	a[1] = 0;
	build(1,1,n);//区间内有 how many 可用
	for(int i = n;i >= 1; i--){
		ans[i] = ask_p(1,a[i] + 1);//统计到前面需要 x 个 这意味到哪
		chan_p(1,ans[i]);
//		cout << tree[1].w << "#"<<ans[i] <<endl;
	}
	for(int i = 1;i <= n; i++) printf("%d\n",ans[i]);
}

 

Published 31 original articles · won praise 5 · Views 1361

Guess you like

Origin blog.csdn.net/qq_43685900/article/details/103473799