1018: [SHOI2008]堵塞的交通traffic

Time Limit: 3 Sec  Memory Limit: 162 MB
Submit: 3064  Solved: 1027
[Submit][Status][Discuss]

Description

  有一天,由于某种穿越现象作用,你来到了传说中的小人国。小人国的布局非常奇特,整个国家的交通系统可
以被看成是一个2行C列的矩形网格,网格上的每个点代表一个城市,相邻的城市之间有一条道路,所以总共有2C个
城市和3C-2条道路。 小人国的交通状况非常槽糕。有的时候由于交通堵塞,两座城市之间的道路会变得不连通,
直到拥堵解决,道路才会恢复畅通。初来咋到的你决心毛遂自荐到交通部某份差事,部长听说你来自一个科技高度
发达的世界,喜出望外地要求你编写一个查询应答系统,以挽救已经病入膏肓的小人国交通系统。 小人国的交通
部将提供一些交通信息给你,你的任务是根据当前的交通情况回答查询的问题。交通信息可以分为以下几种格式:
Close r1 c1 r2 c2:相邻的两座城市(r1,c1)和(r2,c2)之间的道路被堵塞了;Open r1 c1 r2 c2:相邻的两座城
市(r1,c1)和(r2,c2)之间的道路被疏通了;Ask r1 c1 r2 c2:询问城市(r1,c1)和(r2,c2)是否连通。如果存在一
条路径使得这两条城市连通,则返回Y,否则返回N;

Input

  第一行只有一个整数C,表示网格的列数。接下来若干行,每行为一条交通信息,以单独的一行“Exit”作为
结束。我们假设在一开始所有的道路都是堵塞的。我们保证 C小于等于100000,信息条数小于等于100000。

Output

  对于每个查询,输出一个“Y”或“N”。

Sample Input

2
Open 1 1 1 2
Open 1 2 2 2
Ask 1 1 2 2
Ask 2 1 2 2
Exit

Sample Output

Y
N

太Hentai了这道题
跪Vampire http://blog.csdn.net/fzhvampire/article/details/47184397
 
一开始用LCT做,然后不明白为什么一直RE,然后COGS上下载数据不停调试突然发现貌似会有多条边,人家维护的是动态树啊,是树啊是树啊是树啊是树啊是树啊是树啊是树啊是树啊是树啊是树啊是树啊是树啊是树啊是树啊
COGS上拿了20分
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. using namespace std;
  6. #define pa t[x].fa
  7. #define lc t[x].ch[0]
  8. #define rc t[x].ch[1]
  9. const int N=2e5+;
  10. inline int read(){
  11. char c=getchar();int x=,f=;
  12. while(c<''||c>''){if(c=='-')f=-;c=getchar();}
  13. while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
  14. return x*f;
  15. }
  16. int c;
  17. struct LCTnode{
  18. int ch[],fa,rev;
  19. }t[N];
  20. inline int wh(int x){return t[pa].ch[]==x;}
  21. inline int isRoot(int x){return t[pa].ch[]!=x&&t[pa].ch[]!=x;}
  22. inline void update(int x){
  23. }
  24. inline void rever(int x){
  25. t[x].rev^=;
  26. swap(lc,rc);
  27. }
  28. inline void pushDown(int x){
  29. if(t[x].rev){
  30. rever(lc);
  31. rever(rc);
  32. t[x].rev=;
  33. }
  34. }
  35. inline void rotate(int x){
  36. int f=t[x].fa,g=t[f].fa,c=wh(x);
  37. if(!isRoot(f)) t[g].ch[wh(f)]=x;t[x].fa=g;
  38. t[f].ch[c]=t[x].ch[c^];t[t[f].ch[c]].fa=f;
  39. t[x].ch[c^]=f;t[f].fa=x;
  40. update(f);update(x);
  41. }
  42. int st[N],top;
  43. inline void splay(int x){
  44. top=;st[++top]=x;
  45. for(int i=x;!isRoot(i);i=t[i].fa) st[++top]=t[i].fa;
  46. for(int i=top;i>=;i--) pushDown(st[i]);
  47.  
  48. for(;!isRoot(x);rotate(x))
  49. if(!isRoot(pa)) rotate(wh(x)==wh(pa)?pa:x);
  50. }
  51. inline void Access(int x){
  52. for(int y=;x;y=x,x=pa){
  53. splay(x);
  54. rc=y;
  55. update(x);
  56. }
  57. }
  58. inline void MakeR(int x){
  59. Access(x);splay(x);
  60. rever(x);
  61. }
  62. inline int FindR(int x){
  63. Access(x);splay(x);
  64. while(lc) x=lc;
  65. return x;
  66. }
  67. inline void Link(int x,int y){
  68. MakeR(x);
  69. t[x].fa=y;
  70. }
  71. inline void Cut(int x,int y){
  72. MakeR(x);Access(y);splay(y);
  73. t[y].ch[]=t[x].fa=;
  74. update(y);
  75. }
  76.  
  77. int n;
  78. inline int id(int x,int y){
  79. if(x==) return y;
  80. else return n+y;
  81. }
  82. char s[];
  83. int x1,y1,x2,y2,x,y;
  84. int main(){
  85. freopen("bzoj_1018.in","r",stdin);
  86. //freopen("bzoj_1018.out","w",stdout);
  87. n=read();
  88. while(true){
  89. scanf("%s",s);
  90. if(s[]=='E') break;
  91. x1=read();y1=read();x2=read();y2=read();
  92. x=id(x1,y1);y=id(x2,y2); printf("begin %d %d %d %d %d\n",++c,x1,y1,x,y);
  93. if(s[]=='O') Link(x,y);
  94. if(s[]=='C') if(FindR(x)==FindR(y)) Cut(x,y);
  95. if(s[]=='A'){
  96. if(FindR(x)==FindR(y)) puts("Y");
  97. else puts("N");
  98. }
  99. }
  100.  
  101. }

LCT

正解是线段树,只有两行所以上下一行信息一起维护啊

保存luld,rurd,luru,ldrd,lurd,ldru,up,down up和down是说这段区间和左面的区间上或下是不是连着的

合并和查询参考了Vampire的代码

核心思想是:每个区间最后的信息都是通过这个区间连通之后的

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. using namespace std;
  6. #define mid ((l+r)>>1)
  7. #define lc x<<1
  8. #define rc x<<1|1
  9. #define lson x<<1,l,mid
  10. #define rson x<<1|1,mid+1,r
  11. const int N=2e5+;
  12. inline int read(){
  13. char c=getchar();int x=,f=;
  14. while(c<''||c>''){if(c=='-')f=-;c=getchar();}
  15. while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
  16. return x*f;
  17. }
  18. int n,r1,c1,r2,c2;
  19. char op[N];
  20. struct Node{
  21. int luld,rurd,luru,ldrd,lurd,ldru,up,down;
  22. Node():luld(),rurd(),luru(),ldrd(),lurd(),ldru(),up(),down(){}
  23. }t[N<<];
  24. inline Node merge(Node x,Node y){
  25. Node ret;
  26. ret.up=y.up;ret.down=y.down;
  27. ret.luld=x.luld|(x.luru&x.up&y.luld&x.down&x.ldrd);
  28. ret.rurd=y.rurd|(y.luru&x.up&x.rurd&x.down&y.ldrd);
  29. ret.luru=(x.luru&x.up&y.luru) |(x.lurd&x.down&y.ldru);
  30. ret.ldrd=(x.ldrd&x.down&y.ldrd)|(x.ldru&x.up&y.lurd);
  31. ret.lurd=(x.luru&x.up&y.lurd) |(x.lurd&x.down&y.ldrd);
  32. ret.ldru=(x.ldrd&x.down&y.ldru)|(x.ldru&x.up&y.luru);
  33. return ret;
  34. }
  35. void build(int x,int l,int r){
  36. if(l==r) t[x].luru=t[x].ldrd=true;
  37. else{
  38. build(lson);
  39. build(rson);
  40. t[x]=merge(t[lc],t[rc]);//not need
  41. }
  42. }
  43. void segIns1(int x,int l,int r,int c,bool d){
  44. if(l==r) t[x].luld=t[x].rurd=t[x].lurd=t[x].ldru=d;
  45. else{
  46. if(c<=mid) segIns1(lson,c,d);
  47. else segIns1(rson,c,d);
  48. t[x]=merge(t[lc],t[rc]);
  49. }
  50. }
  51. void segIns2(int x,int l,int r,int col,int row,bool d){
  52. if(l==r){
  53. if(row==) t[x].up=d;else t[x].down=d;
  54. }else{
  55. if(col<=mid) segIns2(lson,col,row,d);
  56. else segIns2(rson,col,row,d);
  57. t[x]=merge(t[lc],t[rc]);
  58. }
  59. }
  60.  
  61. Node segQue(int x,int l,int r,int ql,int qr){
  62. if(ql<=l&&r<=qr) return t[x];
  63. else{
  64. Node a,b;int f1=,f2=;
  65. if(ql<=mid) a=segQue(lson,ql,qr),f1=;
  66. if(mid<qr) b=segQue(rson,ql,qr),f2=;
  67. if(f1&&f2) return merge(a,b);
  68. else return f1?a:b;
  69. }
  70. }
  71.  
  72. bool query(){
  73. if(c1>c2) swap(c1,c2),swap(r1,r2);
  74. Node a=segQue(,,n,,c1),b=segQue(,,n,c1,c2),c=segQue(,,n,c2,n);
  75. if(r1==r2){
  76. if(r1==&&( (b.luru)|(c.luld&b.lurd)|(a.rurd&b.ldru)|(a.rurd&b.ldrd&c.luld) )) return true;
  77. if(r1==&&( (b.ldrd)|(c.luld&b.ldru)|(a.rurd&b.lurd)|(a.rurd&b.luru&c.luld) )) return true;
  78. }else{
  79. if(r1==&&( (b.lurd)|(c.luld&b.luru)|(a.rurd&b.ldrd)|(a.rurd&b.ldru&c.luld) )) return true;
  80. if(r2==&&( (b.ldru)|(c.luld&b.ldrd)|(a.rurd&b.luru)|(a.rurd&b.lurd&c.luld) )) return true;
  81. }
  82. return false;
  83. }
  84. int main(){
  85. //freopen("in.txt","r",stdin);
  86. n=read();
  87. build(,,n);
  88. while(true){
  89. scanf("%s",op);
  90. if(op[]=='E') break;
  91. r1=read();c1=read();r2=read();c2=read();
  92. if(op[]=='O'){
  93. if(c1==c2) segIns1(,,n,c1,);
  94. else segIns2(,,n,min(c1,c2),r1,);
  95. }
  96. if(op[]=='C'){
  97. if(c1==c2) segIns1(,,n,c1,);
  98. else segIns2(,,n,min(c1,c2),r1,);
  99. }
  100. if(op[]=='A') printf("%s\n",query()?"Y":"N");
  101. }
  102. }
 
 
 

BZOJ 1018: [SHOI2008]堵塞的交通traffic [线段树 区间信息]的更多相关文章

  1. BZOJ 1018: [SHOI2008]堵塞的交通traffic(线段树分治+并查集)

    传送门 解题思路 可以离线,然后确定每个边的出现时间,算这个排序即可.然后就可以线段树分治了,连通性用并查集维护,因为要撤销,所以要按秩合并,时间复杂度\(O(nlog^2 n)\) 代码 #incl ...

  2. 【bzoj1018】[SHOI2008]堵塞的交通traffic 线段树区间合并+STL-set

    题目描述 给出一张2*n的网格图,初始每条边都是不连通的.多次改变一条边的连通性或询问两个点是否连通. 输入 第一行只有一个整数C,表示网格的列数.接下来若干行,每行为一条交通信息,以单独的一行“Ex ...

  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. [BZOJ1018][SHOI2008]堵塞的交通traffic 线段树维护连通性

    1018: [SHOI2008]堵塞的交通traffic Time Limit: 3 Sec  Memory Limit: 162 MB Submit: 3795  Solved: 1253 [Sub ...

  6. 【BZOJ1018】[SHOI2008]堵塞的交通traffic 线段树

    [BZOJ1018][SHOI2008]堵塞的交通traffic Description 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常奇特,整个国家的交通系统可以被看成是一个 ...

  7. [BZOJ 1018] [SHOI2008] 堵塞的交通traffic 【线段树维护联通性】

    题目链接:BZOJ - 1018 题目分析 这道题就说明了刷题少,比赛就容易跪..SDOI Round1 Day2 T3 就是与这道题类似的..然而我并没有做过这道题.. 这道题是线段树维护联通性的经 ...

  8. BZOJ 1018: [SHOI2008]堵塞的交通traffic(线段树)

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1018 用线段树维护区间连通性,对于每一个区间记录6个域表示(左上,左下)(左上,右上)(右上, ...

  9. bzoj1018[SHOI2008]堵塞的交通traffic——线段树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1018 巧妙的线段树.维护矩阵四个角的连通性. 考虑两个点连通的可能路径分成3部分:两点左边. ...

随机推荐

  1. linux下如何删除文件夹?

    直接rm就可以了,不过要加两个参数-rf 即:rm -rf 目录名字-r 就是向下递归,不管有多少级目录,一并删除:-f 就是直接强行删除,不作任何提示的意思. 例如:删除文件夹实例: rm -rf ...

  2. 使用Git将本地项目或代码上传到GitHub上

    1.要托管到github,那你就应该要有一个属于你自己的github帐号,所以你应该先到github.com注册.打开浏览器在地址栏输入地址:github.com 填写用户名.邮箱.密码,点击Sign ...

  3. console.log()的作用是什么

    主要是方便你调式javascript用的.你可以看到你在页面中输出的内容. 相比alert他的优点是: 他能看到结构话的东西,如果是alert,淡出一个对象就是[object object],但是co ...

  4. geotools实现多边形的合并&缓冲区

    这算是第一次接触开源工具包,说实话刚开始有点不知所措,中途遇到很多问题的时候也感觉头皮发麻,不过很高兴自己还是坚持下来了. geotools就不做过多的介绍了,想总结一下如何根据开源内容做自己的项目. ...

  5. 如何跳出页面的Frame框架

    摘录自:http://blog.csdn.net/clare504/article/details/9347363 很多网页都是框架结构的,在很多的情况下会通过按钮点击事件或链接,跳出框架转到其它界面 ...

  6. Maven1-HelloWorld简单入门

    编写POM(Project Object Model) Maven项目的核心是pom.xml,它定义了项目的基本信息,用于描述项目如何构建,声明项目依赖 创建文件夹,名称为hello-world 创建 ...

  7. python3 第二十一章 - 函数式编程之return函数和闭包

    我们来实现一个可变参数的求和.通常情况下,求和的函数是这样定义的: def calc_sum(*args): ax = 0 for n in args: ax = ax + n return ax 但 ...

  8. Linux网络设备驱动架构

    Linux网络设备驱动程序体系结构分为四层:网络协议接口层.网络设备接口层.提供实际功能的设备驱动层以及网络设备与媒介层. (1)网络协议接口层向网络层协议提供统一的数据包收发接口,不论上层协议是AR ...

  9. js 抛物线 笔记备份

    var funParabola = function(element, target, options) { /* * 网页模拟现实需要一个比例尺 * 如果按照1像素就是1米来算,显然不合适,因为页面 ...

  10. Oracle通过JOB定时执行存储过程实现两表数据比对

    需求: 第三方云平台管理的虚拟机会进行关机.资源扩展等操作,因此开关机状态.CPU.内存.磁盘大小等数据需要进行同步.这里第三方云平台是BMC CLM云平台,底层虚拟化平台是Vcenter.进行同步的 ...