Nuske vs Phantom Thnook


Time limit : 4sec / Memory limit : 256MB

Score : 700 points

Problem Statement

Nuske has a grid with N rows and M columns of squares. The rows are numbered 1 through N from top to bottom, and the columns are numbered 1 through M from left to right. Each square in the grid is painted in either blue or white. If Si,j is 1, the square at the i-th row and j-th column is blue; if Si,j is 0, the square is white. For every pair of two blue square a and b, there is at most one path that starts from a, repeatedly proceeds to an adjacent (side by side) blue square and finally reachesb, without traversing the same square more than once.

Phantom Thnook, Nuske's eternal rival, gives Q queries to Nuske. The i-th query consists of four integers xi,1yi,1xi,2 and yi,2 and asks him the following: when the rectangular region of the grid bounded by (and including) the xi,1-th row, xi,2-th row, yi,1-th column and yi,2-th column is cut out, how many connected components consisting of blue squares there are in the region?

Process all the queries.

Constraints

  • 1≤N,M≤2000
  • 1≤Q≤200000
  • Si,j is either 0 or 1.
  • Si,j satisfies the condition explained in the statement.
  • 1≤xi,1≤xi,2≤N(1≤iQ)
  • 1≤yi,1≤yi,2≤M(1≤iQ)

Input

The input is given from Standard Input in the following format:

N M Q
S1,1..S1,M
:
SN,1..SN,M
x1,1 yi,1 xi,2 yi,2
:
xQ,1 yQ,1 xQ,2 yQ,2

Output

For each query, print the number of the connected components consisting of blue squares in the region.


Sample Input 1

Copy
3 4 4
1101
0110
1101
1 1 3 4
1 1 3 1
2 2 3 4
1 2 2 4

Sample Output 1

Copy
3
2
2
2

In the first query, the whole grid is specified. There are three components consisting of blue squares, and thus 3 should be printed.

In the second query, the region within the red frame is specified. There are two components consisting of blue squares, and thus 2 should be printed. Note that squares that belong to the same component in the original grid may belong to different components.


Sample Input 2

Copy
5 5 6
11010
01110
10101
11101
01010
1 1 5 5
1 2 4 5
2 3 3 4
3 3 3 3
3 1 3 5
1 1 3 4

Sample Output 2

Copy
3
2
1
1
3
2

题意: n*m (n,m<=2000) 的地图,q (q<=200000)次询问,地图每个格子非蓝即白,上下相连,或者左右相连算作连续,还有,每两个蓝色格子,如果连通,保证只有一条确定路径相连,对于每次询问,求范围矩形蓝色连通块个数。

//题解:这题,题意都读了很久。。。常规求连通的算法会超时,因为Q很大。对于每两个连通蓝色格子,只有一条路径连通很关键,每个连通块就变成了一颗树,,所以,即求森林个数,树的话有个性质,点数减边数等于1,所以,即求范围内点数减边数,查询时间复杂度为 O(1)

思路明白,代码比较好写

 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define MX 2005 int n,m,q;
int ans;
char mp[MX][MX];
int d[MX][MX]; //点
int x[MX][MX]; //线
int up[MX][MX]; //上相连
int lef[MX][MX];//左相连 void slv()
{
memset(x,,sizeof(x));
memset(d,,sizeof(d));
memset(up,,sizeof(up));
memset(lef,,sizeof(lef));
for (int i=;i<=n;i++)
{
for (int j=;j<=m;j++)
{
d[i][j]=d[i-][j]+d[i][j-]-d[i-][j-];
x[i][j]=x[i-][j]+x[i][j-]-x[i-][j-];
up[i][j]=up[i][j-];
lef[i][j]=lef[i-][j];
if (mp[i][j]=='')
{
d[i][j]++;
if (i>&&mp[i-][j]=='') x[i][j]++;
if (j>&&mp[i][j-]=='') x[i][j]++;
if (i>&&mp[i-][j]=='') up[i][j]++;
if (j>&&mp[i][j-]=='') lef[i][j]++;
}
}
}
} int main()
{
scanf("%d%d%d",&n,&m,&q); for (int i=;i<=n;i++)
scanf("%s",mp[i]+); slv(); while (q--)
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if (x1>x2) swap(x1,x2);
if (y1>y2) swap(y1,y2);
int ans = d[x2][y2]-d[x2][y1-]-d[x1-][y2]+d[x1-][y1-]; ans -= x[x2][y2]-x[x2][y1-]-x[x1-][y2]+x[x1-][y1-];
ans += up[x1][y2]-up[x1][y1-];
ans += lef[x2][y1]-lef[x1-][y1]; cout<<ans<<endl;
}
return ;
}

Nuske vs Phantom Thnook的更多相关文章

  1. AtCoder:C - Nuske vs Phantom Thnook

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

  2. AGC 015C.Nuske vs Phantom Thnook(思路 前缀和)

    题目链接 闻本题有格子,且何谓格子也 \(Description\) 给定\(n*m\)的蓝白矩阵,保证蓝格子形成的的同一连通块内,某蓝格子到达另一个蓝格子的路径唯一. \(Q\)次询问.每次询问一个 ...

  3. AGC015 C Nuske vs Phantom Thnook(前缀和)

    题意 题目链接 给出一张$n \times m$的网格,其中$1$为蓝点,$2$为白点. $Q$次询问,每次询问一个子矩阵内蓝点形成的联通块的数量 保证任意联通块内的任意蓝点之间均只有一条路径可达 S ...

  4. AtCoder Grand Contest 015 C - Nuske vs Phantom Thnook

    题目传送门:https://agc015.contest.atcoder.jp/tasks/agc015_c 题目大意: 现有一个\(N×M\)的矩阵\(S\),若\(S_{i,j}=1\),则该处为 ...

  5. [agc015c]nuske vs phantom thnook

    题意: 有一个n*m的网格图,每个格子是蓝色或白色.四相邻的两个格子连一条边,保证蓝格子构成一个森林. 有q组询问,每次询问给出一个矩形,问矩形内蓝格子组成的联通块个数. $1\leq n,m\leq ...

  6. Atcoder C - Nuske vs Phantom Thnook(递推+思维)

    题目链接:http://agc015.contest.atcoder.jp/tasks/agc015_c 题意:给一个n*m的格,蓝色的组成路径保证不成环,q个询问,计算指定矩形区域内蓝色连通块的个数 ...

  7. C - Nuske vs Phantom Thnook

    题意:n*m矩阵,n,m<=2e3,矩阵中的1能走到相邻4个1上,0代表障碍,若两个1联通 则只有一条路径 q个询问,q<=2e5,每次询问一个子矩阵中有多少个连通分量? 同一个连通分量中 ...

  8. [NOIP2019模拟赛][AT2381] Nuske vs Phantom Thnook

    题目链接 评测姬好快啊(港记号?)暴力40pts变成60pts 因为题目说了保证蓝色点两两之间只有一条路径,所以肯定组成了一棵树,而对于每次询问的x1,y1,x2,y2的子矩阵中就存在着一个森林 不难 ...

  9. 「AT2381 [AGC015C] Nuske vs Phantom Thnook」

    题目大意 给出一个01矩阵,这个矩阵有一个特殊的性质: 对于任意两个 \(1\) 之间最多只有 \(1\) 条由 \(1\) 构成的路径.每次询问给出一个矩形范围,查询在这个范围内的联通快个数. 分析 ...

随机推荐

  1. Json杂谈系列------(一)初始json

    1. JSON 是什么 JSON,全称是 JavaScript Object Notation,即 JavaScript 对象标记法.这是一种轻量级(Light-Weight).基于文本的(Text- ...

  2. Django——20141014深入理解Django HttpRequest HttpResponse的类和实例

    深入理解Django HttpRequest HttpResponse的类和实例 了解META选项 了解中间件 理清所有模板传输模板变量的方式,并作出选择 Django模板系统:如何利用Django模 ...

  3. Tp框架—方法中处理数据

    Tp框架-方法中处理数据 可以使用函数过滤处理内容. $data[$key]['content_title'] = mb_substr(strip_tags($val['content']) ,0,4 ...

  4. matlab中syms与sym有什么差别

    syms x y %就是定了符号变量x y以后x y就能够直接使用了,有他们运算出来的结果也是符号变量 当然上面的也能够x=sym('x'),y=sym('y') sys('a+b')%就是将a+b转 ...

  5. Openerp图片路径处理

    Openerp目前存储图片如人力资源头像图片等都是以二进制的方式存储在数据库中,若要修改数据库里只存储路径可以用这种方法 Image 装饰器: Image装饰器包含3中图片显示 Image 大图片 i ...

  6. 【Python3 爬虫】05_安装Scrapy

    Scrapy简介 Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛.框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页内容 ...

  7. web.py学习遇到的问题

    刚配置好了web.py运行所需要的环境,试着运行一个入门小实例,结果遇到了异常提示.不知道是什么原因导致的(是环境没配置好?还是……),暂时做个标记,记录一下. 运行的代码 import web ur ...

  8. 在iis6里使用没有扩展名的mvc

    这2天mvc第二个预览终于出来了.赶紧拿过来测试.在装了3.5的虚拟主机上运行正常.应该已经可以到能正常使用的阶段了.不过我前面一篇翻译的文章中说到,如果使用的是iis6的话就只能忍气吞声的使用带扩展 ...

  9. ubuntu 环境变量PATH的修改

    sudo nano /etc/environment 环 境变量是和Shell紧密相关的,用户登录系统后就启动了一个Shell.对于Linux来说一般是bash,但也可以重新设定或切换到其它的 She ...

  10. mysql group by 与order by的实例分析(mysql分组统计后最大值)

    CREATE TABLE `test` ( `id` ) NOT NULL AUTO_INCREMENT, `name` ) CHARACTER SET latin1 DEFAULT NULL, `c ...