线段树区间更新(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;
}

 

posted @ 2018-10-19 21:05  Let_Life_Stop  阅读(244)  评论(0编辑  收藏  举报