uoj#25. 【IOI2014】Wall

本文介绍了一道交互题的实现,重点讲解了如何使用线段树进行区间更新和查询。通过具体的代码示例,展示了线段树的构建、更新和下放操作,以及如何将线段树应用于解决特定的编程问题。

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

题目
写的第一道交互题,编译要加这么一句话(代码在 w a l l . c p p wall.cpp wall.cpp中):

g++ -o wall grader.cpp wall.cpp

其他的,就是裸的线段树了

#include "wall.h"
const int N=2000002;
#define mid ((l+r)>>1)
int mx[N<<2],mn[N<<2];
void build(int t,int l,int r){
	if (l==r) return;
	mn[t]=1e9;
	build(t<<1,l,mid);
	build(t<<1|1,mid+1,r);
}
inline int min(int x,int y){return x<y?x:y;}
inline int max(int x,int y){return x>y?x:y;}
void up(int t,int h,bool f){
	mx[t]=(f?min(mx[t],h):max(mx[t],h));
	mn[t]=(f?min(mn[t],h):max(mn[t],h));
}
void down(int t){
	up(t<<1,mx[t],0),up(t<<1|1,mx[t],0),mx[t]=0;
	up(t<<1,mn[t],1),up(t<<1|1,mn[t],1),mn[t]=1e9;
}
void change(int t,int l,int r,int x,int y,int v,bool f){
	if (x<=l && r<=y){
		up(t,v,f);
		return;
	}
	down(t);
	if (x<=mid) change(t<<1,l,mid,x,y,v,f);
	if (mid<y) change(t<<1|1,mid+1,r,x,y,v,f);
}
void write(int t,int l,int r,int *ans){
	if (l==r){
		ans[l-1]=mx[t];
		return;
	}
	down(t);
	write(t<<1,l,mid,ans);
	write(t<<1|1,mid+1,r,ans);
}
void buildWall(int n,int k,int op[],int l[],int r[],int h[],int ans[]){
	build(1,1,n);
	for (int i=0;i<k;i++) change(1,1,n,l[i]+1,r[i]+1,h[i],op[i]-1);
	write(1,1,n,ans);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值