poj 3277 java

本文介绍了一个基于区间树的数据结构实现,用于处理一系列线段上的高度更新问题。通过构造区间树并实现插入、更新及查询操作,该算法可以高效地解决涉及大量线段范围更新的问题。代码中使用了自定义的节点类来存储区间信息,并实现了主要的功能方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.*;

public class POJ_3277 {

	static int A[],B[],H[];
	static int S[];
	static A8 ST[];
	static int x;
	
	public static void main(String[] args) throws Exception{
		
		BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
		
		int n = Integer.parseInt(buf.readLine());
		
		A = new int[n];
		B = new int[n];
		H = new int[n];
		ST = new A8[8*n+1];
		
		int j = 0;
		int t[] = new int[2*n];
		S = new int[2*n];
		
		for(int i=0;i<n;i++){
			String str[] = buf.readLine().split(" ");
			A[i] = Integer.parseInt(str[0]);
			B[i] = Integer.parseInt(str[1]);
			H[i] = Integer.parseInt(str[2]);
			t[j++] = A[i];
			t[j++] = B[i];
		}
		
		java.util.Arrays.sort(t);
		S[0] = t[0];
		int num = 1;
		for(int i=1;i<j;i++){
			if(t[i-1]!=t[i])
				S[num++] = t[i];
		}
		t = null;
		build(1,0,num-1);
		
		for(x=0;x<n;x++)
			insert(1,A[x],B[x]);
		
		System.out.println(Count(1,0));

	}

	public static long Count(int p,int h){  
	     if(h>ST[p].h)
	    	 ST[p].h=h;  
	     if(ST[p].r-ST[p].l==1)
	    	 return (long)(S[ST[p].r]-S[ST[p].l])*ST[p].h;  
	     return Count(2*p,ST[p].h)+Count(2*p+1,ST[p].h);  
	}

	public static void insert(int k, int l, int r) {
		
		if(l<=S[ST[k].l]&&r>=S[ST[k].r]){
			if(ST[k].h<H[x])
				ST[k].h = H[x];
			return ;
		}
		int mid = S[(ST[k].l+ST[k].r)>>1];
		if(l<mid)
			insert(2*k,l,r);
		if(r>mid)
			insert(2*k+1,l,r);
		
	}

	public static void build(int k, int l, int r) {
		
		ST[k] = new A8(l,r,0);
		
		if(r-l==1)
			return ;
		int mid = (l+r)>>1;
		build(2*k,l,mid);
		build(2*k+1,mid,r);
		
	}

}
class A8{
	int l;
	int r;
	int h;
	public A8(int l, int r, int h) {
		super();
		this.l = l;
		this.r = r;
		this.h = h;
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值