首页 技术 正文
技术 2022年11月14日
0 收藏 498 点赞 4,024 浏览 1206 个字

目录

题目链接

AGC015 C-Nuske vs Phantom Thnook AtCoder

题解

树的性质有:

如果每个蓝色连通块都是树,那么连通块个数=总点数−总边数。

二维前缀和维护点数和边数。

\(O(nm + q)\)

代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#define gc getchar()
#define pc putchar
#define LL long long
inline int read() {
int x = 0,f = 1;
char c = getchar();
while(c < '0' || c > '9') c = gc;
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar();
return x * f;
}
void print(int x) {
if(x < 0) {
pc('-');
x = -x;
}
if(x >= 10) print(x / 10);
pc(x % 10 + '0');
}
const int maxn = 2010; int n,m,q;
int a[maxn][maxn];
int b[maxn][maxn],c[maxn][maxn];
char s[2007];
inline int calc(int a[maxn][maxn],int x1,int y1,int x2,int y2) {
if(x1 > x2 || y1 > y2) return 0;
return a[x2][y2] - a[x1 - 1][y2] - a[x2][y1 - 1] + a[x1 - 1][y1 - 1];
}
int main() {
n = read(),m = read(),q = read();
for(int i = 1;i <= n;++ i) {
scanf("%s",s + 1);
for(int j = 1;j <= m;++ j)
a[i][j] = s[j] - '0';
}
for(int i = 2;i <= n;++ i)
for(int j = 1;j <= m;++ j)
b[i][j] = a[i][j] & a[i - 1][j];
for(int i = 1;i <= n;++ i)
for(int j = 2;j <= m;++ j)
c[i][j] = a[i][j] & a[i][j - 1];
for(int i = 1;i <= n;++ i)
for(int j = 1;j <= m;++ j) {
a[i][j] = a[i][j] + a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
b[i][j] = b[i][j] + b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];
c[i][j] = c[i][j] + c[i - 1][j] + c[i][j - 1] - c[i - 1][j - 1];
}
while(q -- ) {
int x1 = read(),y1 = read(),x2 = read(),y2 = read();
print(calc(a,x1,y1,x2,y2) - calc(b,x1 + 1,y1,x2,y2) - calc(c,x1,y1 + 1,x2,y2));
pc('\n');
}
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295