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\) 构成的路径.每次询问给出一个矩形范围,查询在这个范围内的联通快个数. 分析 ...
随机推荐
- windows内核编程--头文件包括的奇葩的问题
先给解决的方法:假设您觉得您的头文件包括木有问题,请用WDK的build命令编译试试,非常有可能是由于您用了VS 2010 + EASY SYS 环境的问题 使用VS 2010 + EASY SYS ...
- 网络协议系列之三:IP
前言 这篇博客主要对IP协议中一些基础知识点加以总结,并将书中一些晦涩难懂的部分去除了.IP地址协议是网络层中最重要的协议,IP地址协议能够对因特网上的全部设备进行唯一标识.也正由于有了IP协议,我们 ...
- URL中的#号
一.#的涵义 #代表网页中的一个位置.其右面的字符,就是该位置的标识符.比如, http://www.example.com/index.html#print 就代表网页index.html的prin ...
- C语言学习笔记(四) 流程控制
流程控制 流程控制,说通俗一点就是程序代码执行的顺序.不管对于哪门语言来说,流程控制都是很重要的一部分内容: 流程控制的分类,可以分为三大类: 1.顺序 这个很好理解,顺序执行就是代码从上往下一行行的 ...
- 深入分析JavaWeb Item22 -- 国际化(i18n)
一.国际化开发概述 软件的国际化:软件开发时,要使它能同一时候应对世界不同地区和国家的訪问,并针对不同地区和国家的訪问.提供对应的.符合来訪者阅读习惯的页面或数据. 国际化(international ...
- Rabbitmq消息队列(二) Hello World! 模拟简单发送接收
1.简介 RabbitMQ是消息代理:它接受和转发消息.你可以把它当作一个邮局:当你把你要邮寄的邮件放在信箱里时,你可以肯定Postman先生最终会把邮件送到你的收件人那里.在这个比喻中,Rabbit ...
- php对象序列化总出错false
php unserialize 返回false的解决方法 php 提供serialize(序列化) 与unserialize(反序列化)方法. 使用serialize序列化后,再使用unseriali ...
- unity, 集成iOS广告sdk注意事项
----更新:2015-10-22 今天在unity里集成讯飞语音听写的iOS sdk,结果发现前面集成domob广告的方法搞复杂了. 其实,直接把UnityViewControllerBase当做s ...
- Atitit. 数据约束 校验 原理理论与 架构设计 理念模式java php c#.net js javascript mysql oracle
Atitit. 数据约束 校验 原理理论与 架构设计 理念模式java php c#.net js javascript mysql oracle 1. 主键1 2. uniq index2 3. ...
- fpga技能树