题目传送门: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. OpenStack源码系列---nova-api

    OpenStack源码实际上是比较规范的,但是对刚刚接触到源代码的人来说,却感觉有点混乱.我刚开始的时候也常常搞乱,比如service.Service类继承自openstack.common.serv ...

  2. 使用Axis2开发WebService

    一.准备 1.下载Axis2.eclipse插件 axis2-1.6.2-war.zip: http://mirror.bjtu.edu.cn/apache//axis/axis2/java/core ...

  3. HDU 6044 Limited Permutation 读入挂+组合数学

    Limited Permutation Problem Description As to a permutation p1,p2,⋯,pn from 1 to n, it is uncomplica ...

  4. SHOW PROCESSLIST Syntax

    https://dev.mysql.com/doc/refman/5.7/en/show-processlist.html SHOW PROCESSLIST shows you which threa ...

  5. 计算(!(~+[])+{})[--[~+""][+[]]*[~+[]] + ~~!+[]]+({}+[])[[~!+[]]*~+[]]

     (!(~+[])+{})[--[~+""][+[]]*[~+[]] + ~~!+[]]+({}+[])[[~!+[]]*~+[]]  一.JS运算符的优先级 首先要运用到的第一个 ...

  6. android user用户版本提高adb权限【转】

    本文转载自:http://blog.csdn.net/liyongming1982/article/details/14108111 有的user用户版本的log 不全,且push/pull某些文件或 ...

  7. POJ3579 Median —— 二分

    题目链接:http://poj.org/problem?id=3579 Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissio ...

  8. 构造方法,重载,static,math类(java基础知识七)

    1.构造方法概述和格式 * A:构造方法概述和作用     * 给对象的数据(属性)进行初始化 * B:构造方法格式特点     * a:方法名与类名相同(大小也要与类名一致)     * b:没有返 ...

  9. [Selenium] 操作新弹出窗口之验证标题和内容

    1)验证标题 package com.learningselenium.normalwebdriver; import static org.junit.Assert.*; import java.u ...

  10. 【NOIP2017Day1T1】 小凯的疑惑

    [题目链接] 点击打开链接 [算法] px + qy不能表示的最大整数为 pq - p - q 证明见这篇博客,过程很详细,推荐阅读 : https://blog.csdn.net/qwerty112 ...