题解请看 Felix-Lee的CSDN博客

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

CODE

写完就A,刺激。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define X first
#define Y second
inline void read(int &x) {
int flag = 1; char ch;
while(!isdigit(ch=getchar()))if(ch=='-')flag=-flag;
for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
x*=flag;
}
const int MAXN = 1005;
const int MAXP = 1000005;
const int INF = 1e9;
namespace LCT {
#define ls ch[x][0]
#define rs ch[x][1]
int ch[MAXP][2], fa[MAXP];
bool rev[MAXP];
inline bool isr(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; }
inline bool get(int x) { return ch[fa[x]][1] == x; }
inline void mt(int x) { if(rev[x]) rev[x]^=1, rev[ls]^=1, rev[rs]^=1, swap(ls, rs); }
void mtpath(int x) { if(!isr(x)) mtpath(fa[x]); mt(x); }
inline void rot(int x) {
int y = fa[x], z = fa[y]; bool l = get(x), r = l^1;
if(!isr(y)) ch[z][get(y)] = x;
fa[ch[x][r]] = y; fa[y] = x; fa[x] = z;
ch[y][l] = ch[x][r]; ch[x][r] = y;
}
inline void splay(int x) {
mtpath(x);
for(; !isr(x); rot(x))
if(!isr(fa[x])) rot(get(fa[x]) == get(x) ? fa[x] : x);
}
inline void access(int x) { int y = 0;
for(; x; x = fa[y=x]) splay(x), ch[x][1] = y;
}
inline void bert(int x) { access(x), splay(x), rev[x]^=1; }
inline int sert(int x) { access(x), splay(x); for(; ch[x][0]; x=ch[x][0]); return x; }
inline void link(int x, int y) { bert(x); if(sert(y) != x) fa[x] = y; }
inline void cut(int x, int y) { bert(x); sert(y); fa[x] = ch[y][0] = 0; }
inline bool connect(int x, int y) { bert(x); return sert(y) == x; }
}
using namespace LCT;
int n, m, a[MAXN][MAXN], tot;
#define pii pair<int, int>
pii pos[MAXP], val[MAXP<<2]; int lz[MAXP<<2];
inline pii merge(pii A, pii B) {
if(A.X < B.X) return A;
else if(A.X > B.X) return B;
return pii(A.X, A.Y + B.Y);
}
inline void upd(int i) { val[i] = merge(val[i<<1], val[i<<1|1]); }
inline void pd(int i) {
if(lz[i]) {
val[i<<1].X += lz[i], lz[i<<1] += lz[i];
val[i<<1|1].X += lz[i], lz[i<<1|1] += lz[i];
lz[i] = 0;
}
}
void build(int i, int l, int r) {
if(l == r) { val[i] = pii(0, 1); return; }
int mid = (l + r) >> 1;
build(i<<1, l, mid);
build(i<<1|1, mid+1, r);
upd(i);
}
void modify(int i, int l, int r, int x, int y, int v) {
if(x <= l && r <= y) { val[i].X += v, lz[i] += v; return; }
pd(i);
int mid = (l + r) >> 1;
if(x <= mid) modify(i<<1, l, mid, x, y, v);
if(y > mid) modify(i<<1|1, mid+1, r, x, y, v);
upd(i);
}
pii query(int i, int l, int r, int x, int y) {
if(x <= l && r <= y) return val[i];
pd(i);
int mid = (l + r) >> 1;
pii re = pii(INF, 0);
if(x <= mid) re = merge(re, query(i<<1, l, mid, x, y));
if(y > mid) re = merge(re, query(i<<1|1, mid+1, r, x, y));
return re;
}
const int dx[4] = { 0, 0, 1, -1 };
const int dy[4] = { 1, -1, 0, 0 };
inline bool chkout(int x, int y) {
return x < 1 || y < 1 || x > n || y > m;
}
inline bool check(int l, int r) {
for(int i = 0; i < 3; ++i) {
int x = pos[r].X + dx[i];
int y = pos[r].Y + dy[i];
if(chkout(x, y) || a[x][y] < l || a[x][y] > r) continue;
for(int j = i+1; j < 4; ++j) {
int u = pos[r].X + dx[j];
int v = pos[r].Y + dy[j];
if(chkout(u, v) || a[u][v] < l || a[u][v] > r) continue;
if(connect(a[x][y], a[u][v])) return 0;
}
}
return 1;
}
inline void del(int l) {
for(int i = 0; i < 4; ++i) {
int x = pos[l].X + dx[i];
int y = pos[l].Y + dy[i];
if(chkout(x, y) || !connect(l, a[x][y])) continue;
cut(l, a[x][y]);
}
}
inline void solve(int l, int r) {
for(int i = 0; i < 4; ++i) {
int x = pos[r].X + dx[i];
int y = pos[r].Y + dy[i];
if(chkout(x, y) || a[x][y] < l || a[x][y] > r) continue;
link(a[x][y], r);
modify(1, 1, tot, 1, a[x][y], -1);
}
modify(1, 1, tot, l, r, 1);
}
int main () {
scanf("%d%d", &n, &m); tot = n*m;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
scanf("%d", &a[i][j]), pos[a[i][j]] = pii(i, j);
build(1, 1, tot);
LL ans = 0;
for(int i = 1, j = 1; i <= tot; ++i) {
while(!check(j, i)) del(j++);
solve(j, i);
ans += query(1, 1, tot, j, i).second;
}
printf("%I64d\n", ans);
}

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. [转帖]CPU时间片

    CPU时间片 https://www.cnblogs.com/xingzc/p/6077214.html CPU的时间片 CPU的利用率好CPU的 load average 是不一样的 Conntex ...

  2. 【转】spring基础:@ResponseBody,PrintWriter用法

    理解:很多情况我们需要在controller接收请求然后返回一些message. 1.在springmvc中当返回值是String时,如果不加@ResponseBody的话,返回的字符串就会找这个St ...

  3. C++Primer 5th Chap6 Functions

    局部静态变量,关键字static修饰,即使函数结束执行也不受影响,生存期直到程序终止. java中static的单一存储空间的概念与其或有异曲同工之妙. 函数的形参可以无名,但有名可以使其意义更加清晰 ...

  4. stal 安装

    1.1 准备实验环境: 安装系统 1)硬件配置如下 2) 先把光标放到”install CentOS 7”,按 Tab键编辑内核参数,添加 (net.ifnames=0 biosdevname=0) ...

  5. SAS学习笔记23 线性回归、多元回归

    线性回归 由样本资料计算的回归系数b和其他统计量一样,存在抽样误差,因此,需要对线性回归方程进行假设检验 1.方差分析 2.t检验 相关系数的假设检验 相关系数(correlation coeffic ...

  6. javascript移动端 电子书 翻页效果

    1.后端给一长串的纯文本 2.前端根据屏幕的高度,将文本切割为 n 页 3.使用插件 turn.js 将切割好的每页,加上翻书效果 <!DOCTYPE html> <html lan ...

  7. PAT-1012 The Best Rank (25 分) 查询分数对应排名(包括并列)

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...

  8. 雷达无线电系列(二)经典CFAR算法图文解析与实现(matlab)

    一,CFAR基础知识介绍 简介 恒虚警检测技术是指雷达系统在保持虚警概率恒定条件下对接收机输出的信号与噪声作判别以确定目标信号是否存在的技术. 前提 由于接收机输出端中肯定存有噪声(包括大气噪声.人为 ...

  9. 数据仓库之抽取数据:openrowset函数带bulk操作符的用法

    原文:数据仓库之抽取数据:openrowset函数带bulk操作符的用法 在做数据仓库时,最重要的就是ETL的开发,而在ETL开发中的第一步,就是要从原OLTP系统中抽取数据到过渡区中,再对这个过渡区 ...

  10. MySql外网不能访问设置

    mysql的root账户,我在连接时通常用的是localhost或127.0.0.1,公司的测试服务器上的mysql也是localhost所以我想访问无法访问,测试暂停. 解决方法如下: 1,修改表, ...