Nuske vs Phantom Thnook
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,1, yi,1, xi,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≤i≤Q)
- 1≤yi,1≤yi,2≤M(1≤i≤Q)
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
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
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
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
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的更多相关文章
- AtCoder:C - Nuske vs Phantom Thnook
C - Nuske vs Phantom Thnook https://agc015.contest.atcoder.jp/tasks/agc015_c 题意: n*m的网格,每个格子可能是蓝色, 可 ...
- AGC 015C.Nuske vs Phantom Thnook(思路 前缀和)
题目链接 闻本题有格子,且何谓格子也 \(Description\) 给定\(n*m\)的蓝白矩阵,保证蓝格子形成的的同一连通块内,某蓝格子到达另一个蓝格子的路径唯一. \(Q\)次询问.每次询问一个 ...
- AGC015 C Nuske vs Phantom Thnook(前缀和)
题意 题目链接 给出一张$n \times m$的网格,其中$1$为蓝点,$2$为白点. $Q$次询问,每次询问一个子矩阵内蓝点形成的联通块的数量 保证任意联通块内的任意蓝点之间均只有一条路径可达 S ...
- AtCoder Grand Contest 015 C - Nuske vs Phantom Thnook
题目传送门:https://agc015.contest.atcoder.jp/tasks/agc015_c 题目大意: 现有一个\(N×M\)的矩阵\(S\),若\(S_{i,j}=1\),则该处为 ...
- [agc015c]nuske vs phantom thnook
题意: 有一个n*m的网格图,每个格子是蓝色或白色.四相邻的两个格子连一条边,保证蓝格子构成一个森林. 有q组询问,每次询问给出一个矩形,问矩形内蓝格子组成的联通块个数. $1\leq n,m\leq ...
- Atcoder C - Nuske vs Phantom Thnook(递推+思维)
题目链接:http://agc015.contest.atcoder.jp/tasks/agc015_c 题意:给一个n*m的格,蓝色的组成路径保证不成环,q个询问,计算指定矩形区域内蓝色连通块的个数 ...
- C - Nuske vs Phantom Thnook
题意:n*m矩阵,n,m<=2e3,矩阵中的1能走到相邻4个1上,0代表障碍,若两个1联通 则只有一条路径 q个询问,q<=2e5,每次询问一个子矩阵中有多少个连通分量? 同一个连通分量中 ...
- [NOIP2019模拟赛][AT2381] Nuske vs Phantom Thnook
题目链接 评测姬好快啊(港记号?)暴力40pts变成60pts 因为题目说了保证蓝色点两两之间只有一条路径,所以肯定组成了一棵树,而对于每次询问的x1,y1,x2,y2的子矩阵中就存在着一个森林 不难 ...
- 「AT2381 [AGC015C] Nuske vs Phantom Thnook」
题目大意 给出一个01矩阵,这个矩阵有一个特殊的性质: 对于任意两个 \(1\) 之间最多只有 \(1\) 条由 \(1\) 构成的路径.每次询问给出一个矩形范围,查询在这个范围内的联通快个数. 分析 ...
随机推荐
- MySQL 创建数据库及数据表
1.创建数据库 (1) 命令行创建 [root@host]# mysqladmin -u root -p create RUNOOB Enter password:****** (2) php创建 语 ...
- lodash map
_.map(collection, [iteratee=_.identity]) 创建一个经过 iteratee 处理的集合中每一个元素的结果数组. iteratee 会传入3个参数:(value, ...
- GBDT--简单理解
梳理 1.Model Ensemble 能够分为三大类:Bagging,Boosting. Stacking. 2.Boosting能够说是一个思想(框架),而Adaboost等算法仅仅是其一个子类, ...
- ajax+webapi上传图片问题
自己想写一个原生的JS的图片上传,不想一直只是使用上传文件的框架 网上有很多jquery上传图片上传文件的插件,但是要不是用特定的后台框架接收,要不就是只能上传图片,不是文件,还有一些其他的问题,所以 ...
- C# socket编程 使用fleck轻松实现对话 https://github.com/statianzo/Fleck
class Program { static void Main(string[] args) { FleckLog.Level = LogLevel.Debug; var allSockets = ...
- Nginx开发从入门到精通 nginx平台初探
初探nginx架构(100%) 众所周知,nginx性能高,而nginx的高性能与其架构是分不开的.那么nginx究竟是怎么样的呢?这一节我们先来初识一下nginx框架吧. nginx在启动后,在un ...
- mysql调用存储过程出现Illegal mix of collations错误
执行sql语句正常 执行存储过程 异常 提示 Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMP ...
- ubuntu 新建一个root用户
1. 新建一个终端(Applications menu -> Accessories -> Terminal), 输入: sudo –s sudo passwd 输入要设置的密码,这样以后 ...
- js基础系列框架图 (转载)
- makefile使用注意点
1. 小心空格 变量赋值a:= b, 不会将b前面的空格赋值给a 大部分函数调用,特别是$(call func, param) 如果参数前面有空格,则会将空格连同参数一起传入.因此要特别小心. 使 ...