HDU-1540 Tunnel warfare

维护每个区间左端点 能向右走到的最大距离,右端点能向左走到的最大距离

查询的时候,如果当前查询的节点在左子树的rmax里面话,就返回相应的反正继续递归

若位于右子树的话同理可以得到结果

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
struct Segment{
	int l,r;
	int lmax,rmax;
}tr[N<<2];
int n,m;


void pushup(int u){
	Segment &root = tr[u],&left = tr[u<<1],&right = tr[u<<1|1];
	root.lmax = left.lmax + (left.lmax == left.r-left.l+1?right.lmax:0);
	root.rmax = right.rmax + (right.rmax == right.r-right.l+1?left.rmax:0);
}


void build(int u,int l,int r){
	tr[u] = {l,r,1,1};
	if(l==r)return;
	int mid = l+r>>1;
	build(u<<1,l,mid),build(u<<1|1,mid+1,r);
	pushup(u);
}

void modify(int u,int x,int c){
	if(tr[u].l==x && tr[u].r==x){
		tr[u].lmax = tr[u].rmax = c;
		return;
	}
	
	int mid = tr[u].l+tr[u].r>>1;
	if(x<=mid)modify(u<<1,x,c);
	if(x>mid)modify(u<<1|1,x,c);
	pushup(u);
}

int query(int u,int x){
	
	if(tr[u].l==tr[u].r)return tr[u].lmax;
	int mid = tr[u].l+tr[u].r>>1;
	
	if(x<=mid){
		if(x>=tr[u<<1].r-tr[u<<1].rmax+1)
		 return tr[u<<1].rmax + tr[u<<1|1].lmax;
		
		return query(u<<1,x);
	}
	
	else{
		if(x<=tr[u<<1|1].l+tr[u<<1|1].lmax-1)
		 return tr[u<<1|1].lmax + tr[u<<1].rmax;
		
		return query(u<<1|1,x);
	}
	
	
	
}



int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	while(cin>>n>>m){
		build(1,1,n);
		//cout<<query(1,n)<<"\n";
		char c;int x;
		stack<int>st;
		while(m--){
			cin>>c;
			if(c=='D'){cin>>x;modify(1,x,0);st.push(x);}
			else if(c=='R'){
				if(st.size()){
					int t = st.top();
					st.pop();
					modify(1,t,1);
				}
				
			}
			else {cin>>x;cout<<query(1,x)<<"\n";}
		}
		
	}
}

相关推荐

  1. hdu 2079 选课时间

    2023-12-12 05:50:01       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-12 05:50:01       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-12 05:50:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-12 05:50:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-12 05:50:01       20 阅读

热门阅读

  1. C++编程:使用boost::gil::channel_type的示例程序

    2023-12-12 05:50:01       41 阅读
  2. MATLAB中的Table数据使用

    2023-12-12 05:50:01       38 阅读
  3. 测试:接口参数测试

    2023-12-12 05:50:01       40 阅读
  4. OpenSSL 编程示例

    2023-12-12 05:50:01       33 阅读
  5. 第20关 快速掌握K8S下的有状态服务StatefulSet

    2023-12-12 05:50:01       45 阅读
  6. 产品经理进阶:以客户为中心的8个维度

    2023-12-12 05:50:01       45 阅读
  7. 猜数字游戏的Python实现

    2023-12-12 05:50:01       38 阅读
  8. Vue3源码梳理:设计一个微型Vue的源码框架环境

    2023-12-12 05:50:01       43 阅读
  9. [go 面试] 缓存策略与应对数据库压力的良方

    2023-12-12 05:50:01       46 阅读
  10. 宏定义控制printf

    2023-12-12 05:50:01       42 阅读
  11. Matlab窄带信号的测向算法

    2023-12-12 05:50:01       37 阅读
  12. 12.11

    12.11

    2023-12-12 05:50:01      39 阅读
  13. 【力扣100】238.除自身以外数组的乘积

    2023-12-12 05:50:01       40 阅读