线段树区间更新(set暴力)

题目链接:https://cn.vjudge.net/contest/66989#problem/I

具体思路:使用栈存储村庄被损坏的顺序,然后set存的是被损坏的村庄,然后每一次查询,直接找到要查询的数的左边的数和右边的数,如果这个是在set中出现了,那么他相连的村庄为0.否则就是前后两个村庄的编号相差-1.

AC代码:

#include<iostream>
#include<string>
#include<cstring>
#include<iomanip>
#include<stack>
#include<queue>
#include<stdio.h>
#include<algorithm>
#include<set>
using namespace std;
# define inf 0x3f3f3f3f
# define maxn 100+10
# define ll long long
int main(){
int n,m;
while(~scanf("%d%d",&n,&m))
{
set<int>s;
stack<int>q;
s.insert(0);
s.insert(n+1);
while(m--){
int temp;
char str[10];
scanf("%s",str);
if(str[0]=='D'){
scanf("%d",&temp);
q.push(temp);
s.insert(temp);
}
else if(str[0]=='R'){
s.erase(q.top());
q.pop();
}
else if(str[0]=='Q'){
scanf("%d",&temp);
if(s.find(temp)!=s.end())printf("0\n");
else {
set<int>::iterator t=s.lower_bound(temp);
int t1=*t;
int t2=*(--t);
printf("%d\n",t1-t2-1);
}
}
}
}
return 0;
}

 

转载于:https://www.cnblogs.com/letlifestop/p/10262845.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值