• 2022 GCPC--C. Chaotic Construction


    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

    1. 10 12
    2. ? 1 5
    3. - 2
    4. - 8
    5. ? 9 2
    6. ? 9 8
    7. ? 9 7
    8. ? 6 7
    9. ? 3 7
    10. ? 1 9
    11. ? 9 1
    12. + 8
    13. ? 10 3

    Output 

    1. possible
    2. impossible
    3. impossible
    4. impossible
    5. possible
    6. possible
    7. possible
    8. possible
    9. possible

     题意:长度为n的环形路,给定q个操作,加减号表示某点是否能走,?是询问某两点是否能到达。

    解析:因为环形路,所以相当于在最后延迟一段1~n,然后暴力查询肯定就超时了,我们可以利用set来增删,然后询问的时候利用二分判断即可。

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. set<int> st;//存储障碍物下标
    7. unordered_map<int,int> mp;//记录某个点是否为障碍物
    8. int main()
    9. {
    10. int n,q,x,y;
    11. scanf("%d%d",&n,&q);
    12. while(q--)
    13. {
    14. char s[3];
    15. scanf("%s",s);
    16. if(s[0]=='-')
    17. {
    18. scanf("%d",&x);
    19. st.insert(x);
    20. st.insert(x+n);//因为环形,所以相当于在后面延长1~n
    21. mp[x]=1;
    22. }else if(s[0]=='+')
    23. {
    24. scanf("%d",&x);
    25. st.erase(x);
    26. st.erase(x+n);
    27. mp[x]=0;
    28. }else{
    29. scanf("%d%d",&x,&y);
    30. if(x>y) swap(x,y);
    31. if(mp[x]||mp[y])//如果两点是障碍物,肯定到不了
    32. {
    33. printf("impossible\n");
    34. continue;
    35. }
    36. auto t=st.lower_bound(x);//找到第一个大于等于x的位置
    37. int f=0;//记录两个方向是否堵塞
    38. if(t!=st.end()&&*t//此方向堵塞
    39. t=st.lower_bound(y);
    40. if(t!=st.end()&&*t//此方向堵塞
    41. if(f<2) printf("possible\n");//表示至少有一条可以走通
    42. else printf("impossible\n");
    43. }
    44. }
    45. return 0;
    46. }

  • 相关阅读:
    快速修改ppt | 显得不单调
    面试干货丨Redis缓存数据库,持久化机制有哪几种你知道吗?!
    Java集合常见面试题
    shell脚本入门-编写格式以及执行方式
    MYSQL-存储引擎
    MySql死锁
    Atlas (三) --------- Atlas 使用
    Mac系统国内通过nvm快速安装node
    php遵义旅游管理系统的设计与实现毕业设计源码091801
    gflags学习笔记(一)gflags的介绍和安装(ubuntu环境)
  • 原文地址:https://blog.csdn.net/qq_63739337/article/details/128023795