题意

有一个\(n*m\)的网格,每个格子有一个数,为\(1\)~\(n * m\)的排列

一个区间\((1<=l<=r<=n*m)\)是好的,当且仅当:数值在该区间内的格子,构成一棵树(有公共边的格子)

统计好区间数

\(n,m<=2000,n*m<=2*10^5\)

题解

首先先考虑什么情况形成了一棵树?

显然是 这个区间的点数 - 这个区间内互相连的边数 = 1 且 只有一个联通块

这样统计区间的问题一般都要双指针扫一下

我们可以固定左指针\(l\),然后让右指针\(r\)向右扫

显然如果\([l,r]\)之间的边构成了一个环,那么就不能让\(r\)往右移动,也就是保证所有的区间都不存在环

显然随着\(l\)的增加\(r\)不会减小

那么我们就可以对于每一个\(l\)都统计出ta所对应的最大的不存在环的连续区间能到哪里,也就是\(r\),这一步可以用\(LCT\)完成

现在我们的问题就是怎么快速的统计每一个合法的区间\([l,i](l\le i \le r)\)

我们已经可以确定这段区间是没有环的了

所以只需要统计哪些区间的 点数 - 边数 = 1 就好了(这个边数指的是在区间内的边)

这玩意儿怎么统计?

可以发现线段树上每个合法区间的节点的最小值就是1,所以可以统计线段树上的最小值的数量

代码

/*
用线段树统计最小值的个数
就是在扩展的时候扩展到了一个点u
连边只连[l,u-1]之间的边
删除点l的时候只删除[l + 1 , r]的边 */
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
# define LL long long
const int M = 200005 ;
const int N = 2005 ;
const int INF = 1e9 ;
const int dx[] = {0 , 1 , 0 , -1} ;
const int dy[] = {1 , 0 , -1 , 0} ;
using namespace std ; inline int read() {
char c = getchar() ; int x = 0 , w = 1 ;
while(c>'9'||c<'0') { if(c=='-') w = -1 ; c = getchar() ; }
while(c>='0'&&c<='9') { x = x*10+c-'0' ; c = getchar() ; }
return x*w ;
} LL ans ;
int n , m , e , val[N][N] ;
vector < int > eb[M] , ec[M] , vec ;
struct Node { int tmin , cnt ; } ;
inline Node chkmin(Node A , Node B) {
Node c ; c.cnt = 0 ;
c.tmin = min(A.tmin , B.tmin) ;
if(c.tmin == A.tmin) c.cnt += A.cnt ;
if(c.tmin == B.tmin) c.cnt += B.cnt ;
return c ;
}
struct Link_Cut_Tree {
# define ls (son[now][0])
# define rs (son[now][1])
int root , tot ;
int fa[M] , son[M][2] , rev[M] , st[M] ;
inline bool Nrt(int now) { return (son[fa[now]][0] == now || son[fa[now]][1] == now) ; }
inline void Flip(int now) { swap(ls , rs) ; rev[now] ^= 1 ; }
inline void pushdown(int now) {
if(!rev[now]) return ;
if(ls) Flip(ls) ; if(rs) Flip(rs) ;
rev[now] = 0 ;
}
inline void rotate(int now) {
int father = fa[now] , fafa = fa[father] , k = (son[father][1] == now) , w = son[now][k ^ 1] ;
if(Nrt(father)) son[fafa][son[fafa][1] == father] = now ; son[now][k ^ 1] = father ; son[father][k] = w ;
if(w) fa[w] = father ; fa[father] = now ; fa[now] = fafa ;
}
inline void splay(int now) {
int top = 0 , father = now , fafa ; st[++top] = father ;
while(Nrt(father)) father = fa[father] , st[++top] = father ;
while(top) pushdown(st[top --]) ;
while(Nrt(now)) {
father = fa[now] , fafa = fa[father] ;
if(Nrt(father)) rotate(((son[father][0] == now) ^ (son[fafa][0] == father)) ? now : father) ;
rotate(now) ;
}
}
inline void access(int now) {
for(int ch = 0 ; now ; ch = now , now = fa[now])
splay(now) , rs = ch ;
}
inline void makeroot(int now) {
access(now) ; splay(now) ; Flip(now) ;
}
inline int findroot(int now) {
access(now) ; splay(now) ;
while(ls) pushdown(now) , now = ls ;
return now ;
}
inline void Split(int x , int y) {
makeroot(x) ; access(y) ; splay(y) ;
}
inline void Link(int x , int y) {
makeroot(x) ;
if(findroot(y) != x) fa[x] = y ;
}
inline void Cut(int x , int y) {
makeroot(x) ;
if(findroot(y) == x && !son[x][1] && fa[x] == y) fa[x] = son[y][0] = 0 ;
}
# undef ls
# undef rs
} lct ;
struct Segment_Tree {
# define ls (now << 1)
# define rs (now << 1 | 1)
int tmin[M << 2] , cnt[M << 2] , Tag[M << 2] ;
inline void pushup(int now) {
cnt[now] = 0 ;
tmin[now] = min(tmin[ls] , tmin[rs]) ;
if(tmin[ls] == tmin[now]) cnt[now] += cnt[ls] ;
if(tmin[rs] == tmin[now]) cnt[now] += cnt[rs] ;
}
void build(int l , int r , int now) {
tmin[now] = 0 ; cnt[now] = r - l + 1 ;
if(l == r) return ; int mid = (l + r) >> 1 ;
build(l , mid , ls) ; build(mid + 1 , r , rs) ;
}
inline void pushdown(int now) {
if(!Tag[now]) return ;
Tag[ls] += Tag[now] ; Tag[rs] += Tag[now] ;
tmin[ls] += Tag[now] ; tmin[rs] += Tag[now] ;
Tag[now] = 0 ;
}
void Change(int L , int R , int k , int l , int r , int now) {
if(l >= L && r <= R) { tmin[now] += k ; Tag[now] += k ; return ; }
pushdown(now) ; int mid = (l + r) >> 1 ;
if(mid >= R) Change(L , R , k , l , mid , ls) ;
else if(mid < L) Change(L , R , k , mid + 1 , r , rs) ;
else Change(L , mid , k , l , mid , ls) , Change(mid + 1 , R , k , mid + 1 , r , rs) ;
pushup(now) ;
}
Node query(int L , int R , int l , int r , int now) {
if(l > R || r < L) return ((Node) { INF , 0 }) ;
if(l >= L && r <= R) return ((Node) { tmin[now] , cnt[now] }) ;
pushdown(now) ; int mid = (l + r) >> 1 ;
if(mid >= R) return query(L , R , l , mid , ls) ;
else if(mid < L) return query(L , R , mid + 1 , r , rs) ;
else return chkmin(query(L , mid , l , mid , ls) , query(mid + 1 , R , mid + 1 , r , rs)) ;
}
# undef ls
# undef rs
} seg ; int main() {
n = read() ; m = read() ; e = n * m ;
for(int i = 1 ; i <= n ; i ++)
for(int j = 1 ; j <= m ; j ++)
val[i][j] = read() ;
for(int i = 1 ; i <= n ; i ++)
for(int j = 1 , x , y ; j <= m ; j ++)
for(int k = 0 ; k < 4 ; k ++) {
x = i + dx[k] , y = j + dy[k] ;
if(x < 1 || y < 1 || x > n || y > m) continue ;
if(val[x][y] > val[i][j])
eb[val[i][j]].push_back(val[x][y]) ;
else
ec[val[i][j]].push_back(val[x][y]) ;
}
// eb[i] 连的边都是大于i的边
// ec[i] 连的边都是小于i的边
seg.build(1 , e , 1) ;
int r = 0 ;
for(int l = 1 ; l <= e ; l ++) {
bool iscir = false ;
for(int ri = r + 1 ; ri <= e ; ri ++) {
vec.clear() ;
for(int i = 0 , v ; i < ec[ri].size() ; i ++) {
v = ec[ri][i] ; if(v < l) continue ;
if(lct.findroot(ri) == lct.findroot(v)) {
iscir = true ; break ;
}
lct.Link(ri , v) ;
vec.push_back(v) ;
}
for(int i = 0 ; i < vec.size() ; i ++)
lct.Cut(ri , vec[i]) ;
if(iscir) break ;
++ r ; int tot = 0 ;
for(int i = 0 , v ; i < ec[ri].size() ; i ++) {
v = ec[ri][i] ; if(v < l) continue ;
lct.Link(ri , v) ; ++ tot ;
}
seg.Change( ri , e , - tot , 1 , e , 1 ) ;
seg.Change( ri , ri , r - l + 1 , 1 , e , 1 ) ;
}
Node temp = seg.query(l , r , 1 , e , 1) ;
if(temp.tmin == 1) ans += temp.cnt ;
for(int i = 0 , v ; i < eb[l].size() ; i ++) {
v = eb[l][i] ; if(v > r) continue ;
lct.Cut(l , v) ;
seg.Change( v , e , 1 , 1 , e , 1 ) ;
}
seg.Change(l , r , -1 , 1 , e , 1) ;
}
printf("%lld\n",ans) ;
return 0 ;
}

[CF1109F]Sasha and Algorithm of Silence's Sounds的更多相关文章

  1. CF1109F Sasha and Algorithm of Silence's Sounds LCT、线段树

    传送门 构成一棵树可以分成两个限制:图不成环.图的点数-边数=1. 我们考虑枚举右端点\(r\)计算所有可能的左端点\(l\)的答案.我们先考虑第一个限制:图不成环.注意到当\(r\)确定的时候,满足 ...

  2. CodeForces 1109F. Sasha and Algorithm of Silence's Sounds

    题目简述:给定一个$n \times m$的二维矩阵$a[i][j]$,其中$1 \leq nm \leq 2 \times 10^5$,矩阵元素$1 \leq a[i][j] \leq nm$且互不 ...

  3. Codeforces Round #539 (Div. 1) 1109F. Sasha and Algorithm of Silence's Sounds LCT+线段树 (two pointers)

    题解请看 Felix-Lee的CSDN博客 写的很好,不过最后不用判断最小值是不是1,因为[i,i]只有一个点,一定满足条件,最小值一定是1. CODE 写完就A,刺激. #include <b ...

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

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

  5. Codeforces Round #539 Div1 题解

    Codeforces Round #539 Div1 题解 听说这场很适合上分QwQ 然而太晚了QaQ A. Sasha and a Bit of Relax 翻译 有一个长度为\(n\)的数组,问有 ...

  6. Codeforces Round #539&#542&#543&#545 (Div. 1) 简要题解

    Codeforces Round #539 (Div. 1) A. Sasha and a Bit of Relax description 给一个序列\(a_i\),求有多少长度为偶数的区间\([l ...

  7. 转 释一首美国民谣:沉默之音(The Sound Of Silence)

    Ask not what your country can do for you , ask what you can do for your country.    六十年代对美国而言是个多事之秋的 ...

  8. Silence Removal and End Point Detection MATLAB Code

    转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/08/silence-removal-and-end-point-detection.html ...

  9. 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法

    C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...

随机推荐

  1. Spring基于Java的JSR-250注解

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration/spring-jsr250-annot ...

  2. 基于commons-net实现ftp创建文件夹、上传、下载功能

    原文:http://www.open-open.com/code/view/1420774470187 package com.demo.ftp; import java.io.FileInputSt ...

  3. 【APUE】一个fork的面试题及字符设备、块设备的区别

    具体内容见:http://coolshell.cn/articles/7965.html 字符设备.块设备主要区别是:在对字符设备发出读/写请求时,实际的硬件I/O一般就紧接着发生了,而块设备则不然, ...

  4. 怎样扩展EasyUI在页面中马上显示选中的本地图片

    在编写前台页面的时候,有时须要将选中的图片夹杂着其它信息一起上传到服务端,在选着本地图片的时候,为了获得更好的效果,须要将该图片显示在页面上. 最初思路有两个.详细例如以下: 1.获取选中文件的二进制 ...

  5. python 多线程中同步的小样例

    #!/usr/bin/python # -*- coding: UTF-8 -*- # 在一个资源池中.获取资源 # Author: zhang # Date: 2015-7-27 import ti ...

  6. Web安全漏洞及攻击

    背景介绍 先说一个在互联网上常见,但是普通人又不太理解的东西--“验证码”. 验证码(CAPTCHA)是“Completely Automated Public Turing test to tell ...

  7. performSelector调用和直接调用的区别

    今天在准备出笔试题的过程中随便搜了一下其他的笔试题,看到其中一个就是关于performSelector与直接调用的区别. 个人感觉这其实是一个陷阱题,因为大部分应用场景下,用哪一种都可以,可以说是没有 ...

  8. php事务操作示例

    <?php //数据库连接 $conn = mysql_connect('localhost', 'root', '');mysql_select_db('test', $conn); /* 支 ...

  9. STL源代码剖析——STL算法之set集合算法

    前言 本节介绍set集合的相关算法,各自是并集set_union,差集set_difference,交集set_intersection 和对称差集set_symmetric_difference.这 ...

  10. linux安装anaconda中的问题及解决办法

    安装过程: 0:在ananconda官网网站上下载anaconda的linux版本https://www.anaconda.com/download/: 1:linux上切换到下载目录后(用cd), ...