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\) 构成的路径.每次询问给出一个矩形范围,查询在这个范围内的联通快个数. 分析 ...
随机推荐
- IT痴汉的工作现状21-Android开发前景论
饭间闲谈 齐天.周权和我是饭搭子.总是边吃边聊一些与技术.汽车和女人相关的话题. "前阵子Nokia裁员之事不知道完没完?这艾洛普挺能作啊."我吃着香喷喷的过桥米线说." ...
- eclipse工程重命名后,无法生产class问题
在很多时候我们对project重新命名后,class文件始终没有生产,尤其是在web项目的时候,如果不注意class文件生成问题,会浪费大量的时间找错误.这里分享下如何解决eclipse重命名后cla ...
- C语言 | 计算器实现 version 2.
在之前版本中使用栈结构来实现,但由于51单片机不支持malloc函数,所以使用C语言又写了一个计算器版本. 通过数组存放值和操作符模拟栈操作. 实现代码: #include <stdio.h&g ...
- JsonConvert.SerializeObject() 输出josn格式 也就是序列化。
JsonConvert.SerializeObject() 输出josn格式 也就是序列化. JSON.parse 反序列化 http://www.cnblogs.com/ahlx/p/52280 ...
- 5.触摸touch,单点触摸,多点触摸,触摸优先和触摸事件的吞噬
1 触摸 Coco2dx默认仅仅有CCLayer及其派生类才有触摸的功能. 2 单点触摸 打开触摸开关和触摸方式 setTouchEnabled(true); setTouchMode(kCCT ...
- 深入浅出理解之 onInterceptTouchEvent与onTouchEvent
参考:http://blog.csdn.net/android_tutor/article/details/7193090 与 http://www.cnblogs.com/kingcent/ ...
- rational rose 2007安装破解全过程
1:下载安装文件 下载地址: http://pan.baidu.com/s/1c0ldKEs 2:下载虚拟光驱 由于下载的文件须要光驱安装,所以须要下载一个虚拟光驱,虚拟光驱名称:daemon too ...
- Atitit.android jsbridge v1新特性
Atitit.android jsbridge v1新特性 1. Java代码调用js并传参其实是通过WebView的loadUrl方法去调用的.只是参数url的写法不一样而已1 2. 三.JAVA ...
- 如何高效利用github提升自己
作为开源代码库以及版本控制系统,Github拥有超过900万开发者用户,是开发者打开程序开源大门的一扇窗口,也是开发者快速提升自己的一个重要途径.本文将从两个方面介绍github的使用方式. 和逛微博 ...
- spark-streaming的checkpoint机制源码分析
转发请注明原创地址 http://www.cnblogs.com/dongxiao-yang/p/7994357.html spark-streaming定时对 DStreamGraph 和 JobS ...