题解请看 Felix-Lee的CSDN博客

写的很好,不过最后不用判断最小值是不是1,因为[i,i]只有一个点,一定满足条件,最小值一定是1。

CODE

写完就A,刺激。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. #define X first
  5. #define Y second
  6. inline void read(int &x) {
  7. int flag = 1; char ch;
  8. while(!isdigit(ch=getchar()))if(ch=='-')flag=-flag;
  9. for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
  10. x*=flag;
  11. }
  12. const int MAXN = 1005;
  13. const int MAXP = 1000005;
  14. const int INF = 1e9;
  15. namespace LCT {
  16. #define ls ch[x][0]
  17. #define rs ch[x][1]
  18. int ch[MAXP][2], fa[MAXP];
  19. bool rev[MAXP];
  20. inline bool isr(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; }
  21. inline bool get(int x) { return ch[fa[x]][1] == x; }
  22. inline void mt(int x) { if(rev[x]) rev[x]^=1, rev[ls]^=1, rev[rs]^=1, swap(ls, rs); }
  23. void mtpath(int x) { if(!isr(x)) mtpath(fa[x]); mt(x); }
  24. inline void rot(int x) {
  25. int y = fa[x], z = fa[y]; bool l = get(x), r = l^1;
  26. if(!isr(y)) ch[z][get(y)] = x;
  27. fa[ch[x][r]] = y; fa[y] = x; fa[x] = z;
  28. ch[y][l] = ch[x][r]; ch[x][r] = y;
  29. }
  30. inline void splay(int x) {
  31. mtpath(x);
  32. for(; !isr(x); rot(x))
  33. if(!isr(fa[x])) rot(get(fa[x]) == get(x) ? fa[x] : x);
  34. }
  35. inline void access(int x) { int y = 0;
  36. for(; x; x = fa[y=x]) splay(x), ch[x][1] = y;
  37. }
  38. inline void bert(int x) { access(x), splay(x), rev[x]^=1; }
  39. inline int sert(int x) { access(x), splay(x); for(; ch[x][0]; x=ch[x][0]); return x; }
  40. inline void link(int x, int y) { bert(x); if(sert(y) != x) fa[x] = y; }
  41. inline void cut(int x, int y) { bert(x); sert(y); fa[x] = ch[y][0] = 0; }
  42. inline bool connect(int x, int y) { bert(x); return sert(y) == x; }
  43. }
  44. using namespace LCT;
  45. int n, m, a[MAXN][MAXN], tot;
  46. #define pii pair<int, int>
  47. pii pos[MAXP], val[MAXP<<2];
  48. int lz[MAXP<<2];
  49. inline pii merge(pii A, pii B) {
  50. if(A.X < B.X) return A;
  51. else if(A.X > B.X) return B;
  52. return pii(A.X, A.Y + B.Y);
  53. }
  54. inline void upd(int i) { val[i] = merge(val[i<<1], val[i<<1|1]); }
  55. inline void pd(int i) {
  56. if(lz[i]) {
  57. val[i<<1].X += lz[i], lz[i<<1] += lz[i];
  58. val[i<<1|1].X += lz[i], lz[i<<1|1] += lz[i];
  59. lz[i] = 0;
  60. }
  61. }
  62. void build(int i, int l, int r) {
  63. if(l == r) { val[i] = pii(0, 1); return; }
  64. int mid = (l + r) >> 1;
  65. build(i<<1, l, mid);
  66. build(i<<1|1, mid+1, r);
  67. upd(i);
  68. }
  69. void modify(int i, int l, int r, int x, int y, int v) {
  70. if(x <= l && r <= y) { val[i].X += v, lz[i] += v; return; }
  71. pd(i);
  72. int mid = (l + r) >> 1;
  73. if(x <= mid) modify(i<<1, l, mid, x, y, v);
  74. if(y > mid) modify(i<<1|1, mid+1, r, x, y, v);
  75. upd(i);
  76. }
  77. pii query(int i, int l, int r, int x, int y) {
  78. if(x <= l && r <= y) return val[i];
  79. pd(i);
  80. int mid = (l + r) >> 1;
  81. pii re = pii(INF, 0);
  82. if(x <= mid) re = merge(re, query(i<<1, l, mid, x, y));
  83. if(y > mid) re = merge(re, query(i<<1|1, mid+1, r, x, y));
  84. return re;
  85. }
  86. const int dx[4] = { 0, 0, 1, -1 };
  87. const int dy[4] = { 1, -1, 0, 0 };
  88. inline bool chkout(int x, int y) {
  89. return x < 1 || y < 1 || x > n || y > m;
  90. }
  91. inline bool check(int l, int r) {
  92. for(int i = 0; i < 3; ++i) {
  93. int x = pos[r].X + dx[i];
  94. int y = pos[r].Y + dy[i];
  95. if(chkout(x, y) || a[x][y] < l || a[x][y] > r) continue;
  96. for(int j = i+1; j < 4; ++j) {
  97. int u = pos[r].X + dx[j];
  98. int v = pos[r].Y + dy[j];
  99. if(chkout(u, v) || a[u][v] < l || a[u][v] > r) continue;
  100. if(connect(a[x][y], a[u][v])) return 0;
  101. }
  102. }
  103. return 1;
  104. }
  105. inline void del(int l) {
  106. for(int i = 0; i < 4; ++i) {
  107. int x = pos[l].X + dx[i];
  108. int y = pos[l].Y + dy[i];
  109. if(chkout(x, y) || !connect(l, a[x][y])) continue;
  110. cut(l, a[x][y]);
  111. }
  112. }
  113. inline void solve(int l, int r) {
  114. for(int i = 0; i < 4; ++i) {
  115. int x = pos[r].X + dx[i];
  116. int y = pos[r].Y + dy[i];
  117. if(chkout(x, y) || a[x][y] < l || a[x][y] > r) continue;
  118. link(a[x][y], r);
  119. modify(1, 1, tot, 1, a[x][y], -1);
  120. }
  121. modify(1, 1, tot, l, r, 1);
  122. }
  123. int main () {
  124. scanf("%d%d", &n, &m); tot = n*m;
  125. for(int i = 1; i <= n; ++i)
  126. for(int j = 1; j <= m; ++j)
  127. scanf("%d", &a[i][j]), pos[a[i][j]] = pii(i, j);
  128. build(1, 1, tot);
  129. LL ans = 0;
  130. for(int i = 1, j = 1; i <= tot; ++i) {
  131. while(!check(j, i)) del(j++);
  132. solve(j, i);
  133. ans += query(1, 1, tot, j, i).second;
  134. }
  135. printf("%I64d\n", ans);
  136. }

Codeforces Round #539 (Div. 1) 1109F. Sasha and Algorithm of Silence's Sounds LCT+线段树 (two pointers)的更多相关文章

  1. Codeforces Round #539 (Div. 1) C. Sasha and a Patient Friend 动态开点线段树

    题解看这里 liouzhou_101的博客园 更简洁的代码看这里: #include <bits/stdc++.h> using namespace std; typedef long l ...

  2. Codeforces Round #539 (Div. 2) - D. Sasha and One More Name(思维)

    Problem   Codeforces Round #539 (Div. 2) - D. Sasha and One More Name Time Limit: 1000 mSec Problem ...

  3. Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax(思维题)

    Problem   Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax Time Limit: 2000 mSec Problem ...

  4. Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】

    传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...

  5. Codeforces 1109F - Sasha and Algorithm of Silence's Sounds(LCT)

    Codeforces 题面传送门 & 洛谷题面传送门 讲个笑话,这题是 2020.10.13 dxm 讲题时的一道例题,而我刚好在一年后的今天,也就是 2021.10.13 学 LCT 时做到 ...

  6. Codeforces Round #539 (Div. 2) C. Sasha and a Bit of Relax(前缀异或和)

    转载自:https://blog.csdn.net/Charles_Zaqdt/article/details/87522917 题目链接:https://codeforces.com/contest ...

  7. Codeforces Round #539 (Div. 2) C Sasha and a Bit of Relax

    题中意思显而易见,即求满足al⊕al+1⊕…⊕amid=amid+1⊕amid+2⊕…⊕ar且l到r的区间长为偶数的这样的数对(l,r)的个数. 若al⊕al+1⊕…⊕amid=amid+1⊕amid ...

  8. Codeforces Round #539 (Div. 1) E - Sasha and a Very Easy Test 线段树

    如果mod是质数就好做了,但是做除法的时候对于合数mod可能没有逆元.所以就只有存一下mod的每个质因数(最多9个)的幂,和剩下一坨与mod互质的一部分.然后就能做了.有点恶心. CODE #incl ...

  9. Codeforces Round #169 (Div. 2) E. Little Girl and Problem on Trees dfs序+线段树

    E. Little Girl and Problem on Trees time limit per test 2 seconds memory limit per test 256 megabyte ...

随机推荐

  1. Django中常用的那些模块路径

    Django中常用的那些模块路径 from django.shortcuts import HttpResponse, render, redirect from django import temp ...

  2. ngix介绍

    ngix能做什么?  1 反向代理 2 负载均衡 3 正向代理 4 HTTP服务器(动静分离) 1 反向代理 Reverse Proxy 是指以代理服务器来接受来自internet或者是客户端的连接请 ...

  3. Docker使用整理

    Docker技术的基础: namespace,容器隔离的基础,保证A容器看不到B容器. 6个名空间:User,Mnt,Network,UTS,IPC,Pid cgroups,容器资源统计和隔离.主要用 ...

  4. ActivityMQ消息中间件【待完成】

    1,MQ的引入 使用场景,将耗时的通知业务交给消息中间件[业务逻辑进行解耦] 使用消息中间件的逻辑交互 2,MQ的应用场景 首先消息中间件是一个异步处理 有两个关键点:①耗时:②业务的耦合度 案例1: ...

  5. Java框架 高并发系列 1第1天:必须知道的几个概念

    https://mp.weixin.qq.com/s?__biz=MzA5MTkxMDQ4MQ==&mid=2648933019&idx=1&sn=3455877c451de9 ...

  6. IO流的部分类简述

    InputStream 类: InputStream 位于 java.io 包中,它是一个抽象类,表示字节输入流,需要定义 InputStream 子类的应用程序必须总是提供返回下一个输入字节的方法 ...

  7. docker系列之六容器数据卷

    docker之容器数据卷 一.容器数据卷 docker容器运行的时候,会产生一系列的文件,那么我们希望容器产生的数据能提供给其他的容器使用,也就是说怎么实现容器间的数据的共享呢?这就需要用到我们所提到 ...

  8. 3 webpack 4 加vue 2.0生产环境搭建

    1 在前两篇笔记中已经能把开发环境弄好了,接来下构建他的生产环境 2 使用npm 安装url-loader和file-loader来支持图片和字体 npm install --save-dev url ...

  9. Ubuntu输入密码后重新返回登陆界面

    Xserver启动时,需要读取文件~/.Xauthority.由于权限不够,导致登录失败 解决办法 ctrl+alt+F1组合键进入终端,修改文件权限 # ls -l .Xauthority -rw- ...

  10. Image Processing and Analysis_8_Edge Detection:Design of steerable filters for feature detection using canny-like criteria ——2004

    此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...