1453: [Wc]Dface双面棋盘

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 617  Solved: 317
[Submit][Status][Discuss]

Description

Input

Output

Sample Input

Sample Output

HINT

 
 
 
用线段树+数组模拟并查集,维护每一列的连通性,然后暴力合并就行了,常数巨大
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. #define N 210
  6. int father[N<<],tmp[N<<],n,m,a[N][N];
  7. struct Node{
  8. int l,r;
  9. int le[N],ri[N];
  10. int lb[N],rb[N];
  11. int s0,s1;
  12. }tree[N<<];
  13.  
  14. inline int find(int x){
  15. if(father[x]!=x)father[x]=find(father[x]);
  16. return father[x];
  17. }
  18. void update(int rt){
  19. tree[rt].s0=tree[rt<<].s0+tree[rt<<|].s0;
  20. tree[rt].s1=tree[rt<<].s1+tree[rt<<|].s1;
  21. memcpy(tree[rt].lb,tree[rt<<].lb,sizeof tree[rt].lb);
  22. memcpy(tree[rt].rb,tree[rt<<|].rb,sizeof tree[rt].rb);
  23. for(int i=;i<=n<<;i++) father[i]=i;
  24. for(int i=;i<=n;i++) tree[rt<<|].le[i]+=*n,tree[rt<<|].ri[i]+=*n;
  25. for(int i=;i<=n;i++){
  26. int x=tree[rt<<].ri[i],y=tree[rt<<|].le[i];
  27. if(find(x)!=find(y)&&tree[rt<<].rb[i]==tree[rt<<|].lb[i]){
  28. father[find(x)]=find(y);
  29. if(tree[rt<<].rb[i]){
  30. tree[rt].s1--;
  31. }else{
  32. tree[rt].s0--;
  33. }
  34. }
  35. }
  36. for(int i=;i<=n;i++) tree[rt].le[i]=find(tree[rt<<].le[i]),
  37. tree[rt].ri[i]=find(tree[rt<<|].ri[i]);
  38. for(int i=;i<=n;i++) tmp[i<<]=tree[rt].le[i],tmp[(i<<)-]=tree[rt].ri[i];
  39. sort(tmp+,tmp++*n);
  40. int mxdata=unique(tmp+,tmp++*n)-tmp-;
  41. for(int i=;i<=n;i++) tree[rt].le[i]=lower_bound(tmp+,tmp++mxdata,
  42. tree[rt].le[i])-tmp,tree[rt].ri[i]=lower_bound(tmp+,tmp++mxdata,tree[rt].ri[i])-tmp;
  43. for(int i=;i<=n;i++) tree[rt<<|].le[i]-=*n,tree[rt<<|].ri[i]-=*n;
  44. }
  45.  
  46. void build(int l,int r,int rt){
  47. tree[rt].l=l;tree[rt].r=r;
  48. if(l==r){
  49. int tot=;
  50. for(int i=;i<=n;i++)
  51. {
  52. if(a[i][l]!=a[i-][l])
  53. {
  54. tot++;
  55. if(a[i][l]) tree[rt].s1++;
  56. else tree[rt].s0++;
  57. }
  58. tree[rt].le[i]=tree[rt].ri[i]=tot;
  59. tree[rt].lb[i]=tree[rt].rb[i]=a[i][l];
  60. }
  61. return;
  62. }
  63. int mid=(l+r)>>;
  64. build(l,mid,rt<<);
  65. build(mid+,r,rt<<|);
  66. update(rt);
  67. }
  68.  
  69. void modify(int rt,int pos)
  70. {
  71. if(tree[rt].l==tree[rt].r)
  72. {
  73. int tot=;tree[rt].s1=;tree[rt].s0=;
  74. for(int i=;i<=n;i++)
  75. {
  76. if(a[i][pos]!=a[i-][pos])
  77. {
  78. tot++;
  79. if(a[i][pos]) tree[rt].s1++;
  80. else tree[rt].s0++;
  81. }
  82. tree[rt].le[i]=tree[rt].ri[i]=tot;
  83. tree[rt].lb[i]=tree[rt].rb[i]=a[i][pos];
  84. }
  85. return;
  86. }
  87. int mid=(tree[rt].l+tree[rt].r)>>;
  88. if(pos<=mid) modify(rt<<,pos);
  89. else modify(rt<<|,pos);
  90. update(rt);
  91. }
  92.  
  93. int main(){
  94. scanf("%d",&n);
  95. for(int i=;i<=n;i++){
  96. a[][i]=-;
  97. for(int j=;j<=n;j++){
  98. scanf("%d",&a[i][j]);
  99. }
  100. }
  101. build(,n,);
  102. scanf("%d",&m);
  103. while(m--){
  104. int x,y;
  105. scanf("%d%d",&x,&y);
  106. a[x][y]^=;
  107. modify(,y);
  108. printf("%d %d\n",tree[].s1,tree[].s0);
  109. }
  110. return ;
  111. }

bzoj 1453: [Wc]Dface双面棋盘的更多相关文章

  1. 【刷题】BZOJ 1453 [Wc]Dface双面棋盘

    Description Input Output Sample Input Sample Output HINT Solution 不强制在线的动态图问题,那就LCT了 类似二分图那道题目 对于四个方 ...

  2. 【BZOJ1453】[Wc]Dface双面棋盘 线段树+并查集

    [BZOJ1453][Wc]Dface双面棋盘 Description Input Output Sample Input Sample Output HINT 题解:话说看到题的第一反应其实是LCT ...

  3. 【BZOJ1453】[WC] Dface双面棋盘(LCT维护联通块个数)

    点此看题面 大致题意: 给你一个\(n*n\)的黑白棋盘,每次将一个格子翻转,分别求黑色连通块和白色连通块的个数. \(LCT\)动态维护图连通性 关于这一部分内容,可以参考这道例题:[BZOJ402 ...

  4. BZOJ1453:[WC]Dface双面棋盘

    浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://lydsy.com/JudgeOnline/problem. ...

  5. BZOJ1453: [Wc]Dface双面棋盘

    Description Input Output Sample Input Sample Output HINT 线段树套并查集应该是比较好写的做法,时间复杂度为O(N^3+M*NlogN). #in ...

  6. [Wc]Dface双面棋盘()

    题解: 一道维护奇怪信息的线段树... 我刚开始看了标签想的是删去图上一个点后求连通性 发现不会 于是退化成一般图支持删除 插入 维护连通性 发现有2两种做法 1.lct维护 按照结束顺序先后排序,给 ...

  7. BZOJ1453: [WC2005]Dface双面棋盘

    离线LCT维护MST,和3082的方法一样.然而比较码农,适合颓废的时候写. PS:线段树分治要好写得多,LCT比较自娱自乐. #include<bits/stdc++.h> using ...

  8. [BZOJ1453]Dface双面棋盘

    Description Input Output Sample Input Sample Output HINT 线段树+并查集,暴力记录和更新一些信息,详情见代码注解. #include<cm ...

  9. P4121 [WC2005]双面棋盘

    题目 P4121 [WC2005]双面棋盘 貌似是刘汝佳出的题目?? 做法 线段树维护并查集 线段树分治\(1\)~\(n\)行,我们要考虑维护的肯定是黑.白各自的联通块数量 考虑区间合并,其实就与中 ...

随机推荐

  1. leetcode-13-basic-binaryTree

    101. Symmetric Tree 解题思路: 递归的方法如下.分几种情况考虑,如果左子树和右子树都是空,那么返回true:如果不同时为空,返回false:如果都不为空,则 判断其值是否相等,不相 ...

  2. 稀疏表(ST / Sparse Table)

    RMQ问题: 给定一个序列,每次询问一个区间最小值 / 最大值. 没有修改. //拿区间最大值来举例. memset(ans, -INF, sizeof(ans)); for (int i = 1; ...

  3. zoj 4057

    #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> ...

  4. SPOJ COT2 Count on a tree II 树上莫队算法

    题意: 给出一棵\(n(n \leq 4 \times 10^4)\)个节点的树,每个节点上有个权值,和\(m(m \leq 10^5)\)个询问. 每次询问路径\(u \to v\)上有多少个权值不 ...

  5. 如何将Python对象保存在本地文件中?

    Python对象的永久存储 1.使用Python的pickle模块 import pickle class A: def __init__(self,name,a): self.name=name s ...

  6. iOS ifdef ifndef endif

    #ifdef DEBUG //标识符//定义过执行#else//未定义过执行#endif #ifndef DEBUG//标识符//未定义过执行#else//定义过执行#endif #if (5> ...

  7. Jmeter(四)_逻辑控制器详解

    循环控制器: 指定其子节点运行的次数,可以使用具体的数值,也可以设置为变量 1:勾选永远:表示一直循环下去 2:如果同时设置了线程组的循环次数和循环控制器的循环次数,那循环控制器的子节点运行的次数为两 ...

  8. python-高级编程-06-长连接&连接池

    我们都知道tcp是基于连接的协议,其实这个连接只是一个逻辑上面的概念,在ip层来看,tcp和udp仅仅是内容上稍有差别而已. tcp 的连接仅仅是连接两端对于四元组和sequence号的一种约定而已 ...

  9. 添加rpmforge源 centos 7

    (原创)RHEL/CentOS 7.x使用EPEL第三方yum源 时间 2014-07-22 19:45:50 服务器运维与网站架构 原文  http://www.ha97.com/5649.html ...

  10. 【CCF】送货 欧拉路径

    80分,暂时没找出20分的Bug #include<iostream> #include<cstdio> #include<cstring> #include< ...