思路

同LOJ121

动态图连通性的板子

好像有很神的线段树做法,不会,先码住

代码

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <stack>
  5. #include <map>
  6. #include <set>
  7. #include <vector>
  8. using namespace std;
  9. const int MAXN = 100100;
  10. int c,n,time,ans[MAXN],anscnt;
  11. char s[MAXN];
  12. inline int id(int rr,int cc){
  13. return (rr-1)*c+cc;
  14. }
  15. struct uDSU{
  16. int h[MAXN],fa[MAXN];
  17. stack<int> height,mer;
  18. int find(int x){
  19. if(x==fa[x])
  20. return x;
  21. else
  22. return find(fa[x]);
  23. }
  24. void merge(int x,int y){
  25. x=find(x);
  26. y=find(y);
  27. if(h[x]<=h[y]){
  28. fa[x]=y;
  29. mer.push(x);
  30. if(h[x]==h[y]){
  31. h[y]++;
  32. height.push(1);
  33. }
  34. else{
  35. height.push(0);
  36. }
  37. }
  38. else{
  39. fa[y]=x;
  40. mer.push(y);
  41. height.push(0);
  42. }
  43. }
  44. bool query(int x,int y){
  45. return find(x)==find(y);
  46. }
  47. void undo(void){
  48. int val=height.top();
  49. height.pop();
  50. int x=mer.top();
  51. mer.pop();
  52. h[fa[x]]-=val;
  53. fa[x]=x;
  54. }
  55. void init(void){
  56. for(int i=1;i<=n;i++)
  57. h[i]=1,fa[i]=i;
  58. }
  59. }DSU;
  60. map<pair<int,int>,int > M;
  61. struct Node{
  62. int le,u,v;
  63. bool operator < (const Node &b) const{
  64. return le<b.le;
  65. }
  66. };
  67. vector<Node> seg[MAXN<<2];
  68. void add(int L,int R,int l,int r,int o,int le,int u,int v){
  69. if(L<=l&&r<=R){
  70. seg[o].push_back((Node){le,u,v});
  71. return;
  72. }
  73. int mid=(l+r)>>1;
  74. if(L<=mid)
  75. add(L,R,l,mid,o<<1,le,u,v);
  76. if(R>mid)
  77. add(L,R,mid+1,r,o<<1|1,le,u,v);
  78. }
  79. void dfs(int o,int l,int r){
  80. sort(seg[o].begin(),seg[o].end());
  81. for(int i=0;i<seg[o].size();i++)
  82. if(seg[o][i].le==-1)
  83. DSU.merge(seg[o][i].u,seg[o][i].v);
  84. else
  85. ans[seg[o][i].le]=DSU.query(seg[o][i].u,seg[o][i].v);
  86. if(l!=r){
  87. int mid=(l+r)>>1;
  88. dfs(o<<1,l,mid);
  89. dfs(o<<1|1,mid+1,r);
  90. }
  91. for(int i=0;i<seg[o].size();i++)
  92. if(seg[o][i].le==-1)
  93. DSU.undo();
  94. }
  95. int main(){
  96. scanf("%d",&c);
  97. n=c*2;
  98. DSU.init();
  99. scanf("%s",s);
  100. while(s[0]!='E'){
  101. ++time;
  102. int r1,c1,r2,c2;
  103. scanf("%d %d %d %d",&r1,&c1,&r2,&c2);
  104. int x=id(r1,c1),y=id(r2,c2);
  105. if(x>y)
  106. swap(x,y);
  107. if(s[0]=='C'){
  108. add(M[make_pair(x,y)],time,1,100010,1,-1,x,y);
  109. M.erase(make_pair(x,y));
  110. }
  111. if(s[0]=='O'){
  112. M[make_pair(x,y)]=time;
  113. }
  114. if(s[0]=='A'){
  115. add(time,time,1,100010,1,++anscnt,x,y);
  116. }
  117. scanf("%s",s);
  118. }
  119. ++time;
  120. for(map<pair<int,int>,int >::iterator it=M.begin();it!=M.end();it++)
  121. add((*it).second,time,1,100010,1,-1,(*it).first.first,(*it).first.second);
  122. dfs(1,1,100010);
  123. for(int i=1;i<=anscnt;i++)
  124. printf("%s\n",(ans[i])?"Y":"N");
  125. return 0;
  126. }

P4246 [SHOI2008]堵塞的交通的更多相关文章

  1. Luogu P4246 [SHOI2008]堵塞的交通(线段树+模拟)

    P4246 [SHOI2008]堵塞的交通 题意 题目描述 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常奇特,整个国家的交通系统可以被看成是一个\(2\)行\(C\)列的矩形 ...

  2. Bzoj1018/洛谷P4246 [SHOI2008]堵塞的交通(线段树分治+并查集)

    题面 Bzoj 洛谷 题解 考虑用并查集维护图的连通性,接着用线段树分治对每个修改进行分治. 具体来说,就是用一个时间轴表示图的状态,用线段树维护,对于一条边,我们判断如果他的存在时间正好在这个区间内 ...

  3. 数据结构(线段树):BZOJ 1018: [SHOI2008]堵塞的交通traffic

    1018: [SHOI2008]堵塞的交通traffic Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 2638  Solved: 864 Descri ...

  4. BZOJ 1018 [SHOI2008]堵塞的交通traffic

    1018: [SHOI2008]堵塞的交通traffic Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 2247  Solved: 706[Submit ...

  5. BZOJ 1018: [SHOI2008]堵塞的交通traffic [线段树 区间信息]

    1018: [SHOI2008]堵塞的交通traffic Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 3064  Solved: 1027[Submi ...

  6. bzoj千题计划108:bzoj1018: [SHOI2008]堵塞的交通traffic

    http://www.lydsy.com/JudgeOnline/problem.php?id=1018 关键点在于只有两行 所以一个2*m矩形连通情况只有6种 编号即对应代码中的a数组 线段树维护 ...

  7. 【BZOJ1018】[SHOI2008]堵塞的交通

    [BZOJ1018][SHOI2008]堵塞的交通 题面 bzoj 洛谷 洛谷 题解 菊队讲要用线段树维护连通性,但是好像没人写 解法一 将所有的加边删边离线,然后以最近删除时间为边权,$LCT$维护 ...

  8. 1018: [SHOI2008]堵塞的交通traffic

    1018: [SHOI2008]堵塞的交通traffic 链接 分析: 用线段树维护区间的四个端点的联通情况,然后查询的时候,把所有覆盖到的区间合并起来即可. 六种情况左上到右上(左边到右边的情况)… ...

  9. 【bzoj1018】[SHOI2008]堵塞的交通traffic

    1018: [SHOI2008]堵塞的交通traffic Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 2887  Solved: 954[Submit ...

随机推荐

  1. 10.for

    要遍历一个范围(如数组或表),使用 for 循环.在数组上迭代时,当前数组元素存储在循环变量中.在遍历字典时, index 存储在循环变量中. (in 内容测试) for x in [5, 7, 11 ...

  2. Shader2.0常用语义

    POSITION: 获取模型顶点的信息.NORMAL: 获取法线信息TEXCOORD(n):        高精度的从顶点传递信息到片段着色器COLOR:     表示低精度从顶点传递信息到片段着色器 ...

  3. 改装原生的dialog

    改装 dialog 定制 View rootView = LayoutInflater.from(mContext). inflate(R.layout.nfc_tag_name_dialog, nu ...

  4. Yii ActiveRecord生命周期

  5. BestCoder Round #55 ($)

    C 构造一个矩阵,然后采用矩阵快速幂 #include <iostream> #include <algorithm> #include <string.h> #i ...

  6. jQuery事件--mouseover()、mouseout()、mouseenter()和mouseleave()

       mouseover([[data],fn]) 概述 当鼠标指针位于元素上方时,会发生 mouseover 事件.该事件大多数时候会与 mouseout 事件一起使用 注释:与 mouseente ...

  7. python 可视化

    一.环境安装 windows:pip install numpy scipy matplotlib #pip install http://effbot.org/downloads/Imaging-1 ...

  8. spring4.0.0 源码导入eclipse(sts)

    其余步骤请见:http://www.cnblogs.com/xiluhua/p/7450972.html 执行 gradle eclipse -x :eclipse 报错: 解决办法: 找到 行,注释 ...

  9. linux常用命令:touch 命令

    linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. 1.命令格式: touch [选项]... 文件... 2.命令参数: -a    ...

  10. svnrdump:E175000:SSL is not supported错误的解决

    参考博客:https://www.cnblogs.com/jkko123/p/6358461.html 参考博客:https://blog.csdn.net/w171066/article/detai ...