题目传送门:https://agc015.contest.atcoder.jp/tasks/agc015_c

题目大意:

现有一个\(N×M\)的矩阵\(S\),若\(S_{i,j}=1\),则该处为蓝色,否则为白色。保证蓝色格子构成的联通块为树,即联通块内蓝格子相邻的边为\(cnt-1\),多次询问子矩阵内蓝色联通块个数


题目都保证是棵树了就是个SB题,二维前缀和,记录蓝块数目,相邻蓝块个数(行列分开记好写些),子矩阵内用蓝块个数减去相邻蓝块个数即为联通块个数

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
int x=0,f=1; char ch=gc();
for (;ch<'0'||ch>'9';ch=gc()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline int read(){
int x=0,f=1; char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline void print(int x){
if (x<0) putchar('-'),x=-x;
if (x>9) print(x/10);
putchar(x%10+'0');
}
const int N=2e3;
char map[N+10][N+10];
int sum[N+10][N+10],lne[N+10][N+10],row[N+10][N+10];
int main(){
int n=read(),m=read(),q=read();
for (int i=1;i<=n;i++){
scanf("%s",map[i]+1);
for (int j=1;j<=m;j++){
if (map[i][j]=='1'){
sum[i][j]=1;
if (map[i][j-1]=='1') lne[i][j]=1;
if (map[i-1][j]=='1') row[i][j]=1;
}
}
}
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
sum[i][j]+=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
lne[i][j]+=lne[i-1][j]+lne[i][j-1]-lne[i-1][j-1];
row[i][j]+=row[i-1][j]+row[i][j-1]-row[i-1][j-1];
}
}
for (int i=1;i<=q;i++){
int x1=read(),y1=read(),x2=read(),y2=read(),Ans=0;
Ans+=sum[x2][y2]-sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1];
Ans-=lne[x2][y2]-lne[x1-1][y2]-lne[x2][y1]+lne[x1-1][y1];
Ans-=row[x2][y2]-row[x1][y2]-row[x2][y1-1]+row[x1][y1-1];
printf("%d\n",Ans);
}
return 0;
}

AtCoder Grand Contest 015 C - Nuske vs Phantom Thnook的更多相关文章

  1. AtCoder Grand Contest 015

    传送门 A - A+...+B Problem 题意:n个数最大值a,最小值b,求和的可能数量. #include<cstdio> #include<algorithm> us ...

  2. AtCoder Grand Contest 015 题解

    A - A+...+B Problem 常识 Problem Statement Snuke has N integers. Among them, the smallest is A, and th ...

  3. AtCoder Grand Contest 015 E - Mr.Aoki Incubator

    题目传送门:https://agc015.contest.atcoder.jp/tasks/agc015_e 题目大意: 数轴上有\(N\)个点,每个点初始时在位置\(X_i\),以\(V_i\)的速 ...

  4. Atcoder Grand Contest 015 F - Kenus the Ancient Greek(找性质+乱搞)

    洛谷题面传送门 & Atcoder 题面传送门 一道难度 Au 的 AGC F,虽然看过题解之后感觉并不复杂,但放在现场确实挺有挑战性的. 首先第一问很简单,只要每次尽量让"辗转相除 ...

  5. AtCoder Grand Contest 015题解

    传送门 \(A\) 找到能达到的最大的和最小的,那么中间任意一个都可以被表示出来 typedef long long ll; int n,a,b;ll res; int main(){ scanf(& ...

  6. AtCoder Grand Contest 031 简要题解

    AtCoder Grand Contest 031 Atcoder A - Colorful Subsequence description 求\(s\)中本质不同子序列的个数模\(10^9+7\). ...

  7. AtCoder:C - Nuske vs Phantom Thnook

    C - Nuske vs Phantom Thnook https://agc015.contest.atcoder.jp/tasks/agc015_c 题意: n*m的网格,每个格子可能是蓝色, 可 ...

  8. AtCoder Grand Contest 012

    AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...

  9. AtCoder Grand Contest 011

    AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...

随机推荐

  1. React Native安装

    1.安装 1.1 安装Node.js 下载安装即可 1.2 安装Homebrew 终端中执行: $ /usr/bin/ruby -e "$(curl -fsSL https://raw.gi ...

  2. 第一讲:使用html5——canvas绘制奥运五环

    <html> <head> <title>初识canvas</title> </head> <body> <canvas ...

  3. bash shell和进程

    1 exec builtin 不创建子shell,在原进程的上启动新的脚本,但是它会把老shell的环境清理掉,所以,它从原shell中什么也不继承,在一个干净的环境中执行新的脚本.执行完之后退出当前 ...

  4. quilt

    1 什么是quilt quilt是一个patch管理工具,特别适合于对多个patch进行管理. quilt是基于gnu patch和diff的. 2 使用quilt创建一个patch 第一步,quil ...

  5. Ehcache CacheManager

    CacheManager是Ehcache框架的核心类和入口,它负责管理一个或多个Cache对象.要使用Ehcache框架,必须要先创建 CacheManager 对象.现在我们学习下,如何创建 Cac ...

  6. BZOJ2327: [HNOI2011]勾股定理

    BZOJ2327: [HNOI2011]勾股定理 Description 题解Here! 这是一道神题... 我一开始把题目看错了,我以为是在$n$根木棒中选两个$i,j$满足$gcd(i,j)==1 ...

  7. (转)React Native 使用react-native-image-picker库实现图片上传功能

    react-native-image-picker作为一个集成相机和相册的功能的第三方库,因为其使用相对简单受到前端开发人员的喜爱. react-native-image-picker使用 首先,安装 ...

  8. ios 短音效的使用

    1.通用短音效ID的获取 #import <Foundation/Foundation.h> @interface MJAudioTool : NSObject /** * 播放音效 * ...

  9. JavaScript SHA-1

    1. [文件] webtoolkit.sha1.js ~ 4KB     /****  Secure Hash Algorithm (SHA1)*  http://www.huiyi8.com/css ...

  10. Python小练习_将数据库中表数据存到redis里

    # ##练习:将xxx数据库中my_user表中数据存到redis里面# 分析: pymysql.json.redis# 1.连接数据库,查到数据库里面所有的数据,游标类型要用pymysql.curs ...