The city Gircle has only one street, and that street is cyclic. This was very convenient in times when people didn't carry a device with compass, GPS and detailed maps around in their pockets, because you only have to walk in one direction and will certainly arrive at your destination. Since Gircle's founding a lot of time has passed. Civil engineers now know a lot more about road network design and most people have immediate access to reliable and accurate navigation systems. However, the passage of time also affected the old street surface and more and more cracks and potholes appeared.
The local government has finally decided to improve the situation, but preserving the city's historic appeal and building new streets are unfortunately mutually exclusive. Because tourism is vital for Gircle's economy, the government's only viable option for improving the situation is to renovate segments of the street when necessary. Gircle's street is very narrow, so a construction site at a street segment makes it impossible for citizens to pass that segment or even leave or enter it.
As a member of the Gircle Construction and Planning Commission (GCPC), you always know when one of the nn street segments is closed or reopened. Naturally, the citizens expect you to tell them whether the trips they want to do are currently possible.
Input
The input consists of:
- One line with two integers n (2≤n≤105) and q (1≤q≤105), the number of street segments and the number of events. No street segment is initially closed.
- q lines, each describing an event. Each event is described in one of the following ways:
- "- a": Segment aa (1≤a≤n) is closed. It is guaranteed that segment a was open before.
- "+ a": Segment aa (1≤a≤n) is reopened. It is guaranteed that segment a was closed before.
- "? a b": A person asks you if it is possible to go from segment a to segment bb (1≤a,b≤n and a≠b).
Output
For each event of the form "? a b", print one line containing the word "possible", if it is possible to move from segment aa to segment bb, or "impossible" otherwise. If aa or bb are currently closed, the answer is "impossible".
Input
10 12
? 1 5
- 2
- 8
? 9 2
? 9 8
? 9 7
? 6 7
? 3 7
? 1 9
? 9 1
+ 8
? 10 3
Output
possible
impossible
impossible
impossible
possible
possible
possible
possible
possible
题意:长度为n的环形路,给定q个操作,加减号表示某点是否能走,?是询问某两点是否能到达。
解析:因为环形路,所以相当于在最后延迟一段1~n,然后暴力查询肯定就超时了,我们可以利用set来增删,然后询问的时候利用二分判断即可。
#include <stdio.h>
#include <algorithm>
#include <set>
#include <unordered_map>
using namespace std;
set<int> st;//存储障碍物下标
unordered_map<int,int> mp;//记录某个点是否为障碍物
int main()
{
int n,q,x,y;
scanf("%d%d",&n,&q);
while(q--)
{
char s[3];
scanf("%s",s);
if(s[0]=='-')
{
scanf("%d",&x);
st.insert(x);
st.insert(x+n);//因为环形,所以相当于在后面延长1~n
mp[x]=1;
}else if(s[0]=='+')
{
scanf("%d",&x);
st.erase(x);
st.erase(x+n);
mp[x]=0;
}else{
scanf("%d%d",&x,&y);
if(x>y) swap(x,y);
if(mp[x]||mp[y])//如果两点是障碍物,肯定到不了
{
printf("impossible\n");
continue;
}
auto t=st.lower_bound(x);//找到第一个大于等于x的位置
int f=0;//记录两个方向是否堵塞
if(t!=st.end()&&*t<y) f++;//此方向堵塞
t=st.lower_bound(y);
if(t!=st.end()&&*t<x+n) f++;//此方向堵塞
if(f<2) printf("possible\n");//表示至少有一条可以走通
else printf("impossible\n");
}
}
return 0;
}