POJ 3667 线段树 区间合并

本文介绍了一种基于线段树的数据结构实现,用于处理区间更新和查询的问题。具体实现了两种操作:一是将指定区间内的所有元素置为特定值;二是查询是否存在连续的、长度等于给定值的子区间,所有元素均为0,若有则返回该区间的起始位置并将其标记为已占用。

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

2 a b:将[a,a+b-1]的房间清空

1 a:询问是不是有连续长度为a的空房间,有的话住进最左边


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) {
         new POJ3667().solve() ;
	}
}

class POJ3667{
	InputReader in = new InputReader(System.in) ;
	PrintWriter out = new PrintWriter(System.out) ;
	SegTree tree = new SegTree() ;
	
	void solve(){
		int n = in.nextInt() ; 
		int m = in.nextInt() ; 
		tree.build(1, 1, n) ;
		while(m-- > 0){
			if(in.nextInt() == 1){
				int v = in.nextInt() ; 
				if(tree.sum[1] < v) out.println(0) ;
				else{
					int d = tree.query(v, 1, 1, n) ;
					out.println(d) ;
					tree.update(d, d+v-1, 0, 1, 1, n) ;
				}
			}
			else{
				int d = in.nextInt() ;
				int w = in.nextInt() ;
				tree.update(d , d+w-1, 1, 1, 1, n) ;
			}
		}
		out.flush() ; 
	}
 	
}

class SegTree{
	int N = 50008 ;
	int[] sum = new int[N<<2] ;
	int[] lsum = new int[N<<2] ;
	int[] rsum = new int[N<<2] ;
	int[] color = new int[N<<2] ;
	int[] len = new int[N<<2] ;
	
	void down(int t){
		if(color[t] != -1){
			color[t<<1] = color[t<<1|1] = color[t] ;
			sum[t<<1] = lsum[t<<1] = rsum[t<<1] = color[t] * len[t<<1] ;
			sum[t<<1|1] = lsum[t<<1|1] = rsum[t<<1|1] = color[t] * len[t<<1|1] ;
		    color[t] = -1 ;
		}
	}
	
	void up(int t){
		lsum[t] = (lsum[t<<1] == len[t<<1]) ? len[t<<1] + lsum[t<<1|1] : lsum[t<<1]  ;
		rsum[t] = (rsum[t<<1|1] == len[t<<1|1]) ? len[t<<1|1] + rsum[t<<1] : rsum[t<<1|1] ;
		sum[t] = Math.max(sum[t<<1] , sum[t<<1|1]) ;
		sum[t] = Math.max(sum[t], rsum[t<<1] + lsum[t<<1|1]) ;
	}
	
	void build(int t , int l , int r){
		color[t] = -1 ;
		sum[t] = lsum[t] = rsum[t] = len[t] = r - l + 1 ;
		if(len[t] == 1) return ; 
		int m = (l + r) >> 1 ;
		build(t<<1 , l , m) ;
		build(t<<1|1, m+1, r) ;
	}
	
	void update(int L , int R , int V , int t , int l , int r){
		if(L <= l && r <= R){
			lsum[t] = rsum[t] = sum[t] = len[t] * V  ; 
			color[t] = V ;
			return ; 
		}
		down(t) ;
		int m = (l + r) >> 1 ;
		if(L <= m) update(L, R, V, t<<1, l, m) ;
		if(R > m) update(L, R, V, t<<1|1, m+1 , r) ;
		up(t) ;
	}
	
	int query(int V , int t , int l , int r){
		if(l == r) return l ;
		down(t) ;
		int m = (l + r) >> 1 ;
		if(sum[t<<1] >= V) return query(V, t<<1 , l, m) ;
		else if(rsum[t<<1] + lsum[t<<1|1] >= V) return m - rsum[t<<1] + 1 ;
		else return query(V, t<<1|1, m+1 , r) ;
	}
}

class InputReader {
	public BufferedReader reader;
	public StringTokenizer tokenizer;

	public InputReader(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = new StringTokenizer("");
	}

	private void eat(String s) {
		tokenizer = new StringTokenizer(s);
	}

	public String nextLine() {
		try {
			return reader.readLine();
		} catch (Exception e) {
			return null;
		}
	}

	public boolean hasNext() {
		while (!tokenizer.hasMoreTokens()) {
			String s = nextLine();
			if (s == null)
				return false;
			eat(s);
		}
		return true;
	}

	public String next() {
		hasNext();
		return tokenizer.nextToken();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}

	public BigInteger nextBigInteger() {
		return new BigInteger(next());
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值