题目传送门: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;
}

最新文章

  1. Solr_全文检索引擎系统
  2. 【积累】validate验证框架的使用
  3. JS传参中文乱码
  4. 百度Site App的uaredirect.js实现手机访问,自动跳转网站手机版
  5. 随机数生成器console
  6. 安装Python图型处理库Python Imaging Library(PIL)
  7. HDU 4118 Holiday&#39;s Accommodation
  8. linux入门教程(八) Linux磁盘管理
  9. oracle rac logminer有限制用法及session_info为unknown情况
  10. HDU 3008 Warcraft(DP)
  11. Factorials 阶乘
  12. 推断js中的类型:typeof / instanceof / constructor / prototype
  13. npx
  14. 网络通信协议tcp,udp区别
  15. ArcGIS Portal与Adapter安装问题
  16. 搭建Java后台
  17. git 撤销本地修改
  18. chrome误删了bookmarks且已经同步清空了google云端的挽救方式
  19. js中this揭秘
  20. Android之getSystemService

热门文章

  1. Animated progress view with CAGradientLayer(带翻译)
  2. Pycharm下运行程序查看每个变量的值的方法(类似于Spyder和MATLAB)
  3. linux 输入子系统(4) intput_dev 接口描述
  4. (转)使用MAT比较多个heap dump文件
  5. Vue 之 npm 及 安装的包
  6. C# 给窗体添加事件
  7. Hadoop MapReduce输入输出类型
  8. 一步一步学Silverlight 2系列(21):如何在Silverlight中调用JavaScript
  9. UVA-11078(水题)
  10. [存档]获取通讯录信息并写到SD卡上