Inner Vertices
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 2447   Accepted: 678
Case Time Limit: 2000MS

Description

There is an infinite square grid. Some vertices of the grid are black and other vertices are white.

A vertex V is called inner if it is both vertical-inner and horizontal-inner. A vertex V is called horizontal-inner if there are two such black vertices in the same row that V is located between them. A vertex V is called vertical-inner if there are two such black vertices in the same column that V is located between them.

On each step all white inner vertices became black while the other vertices preserve their colors. The process stops when all the inner vertices are black.

Write a program that calculates a number of black vertices after the process stops.

Input

The first line of the input file contains one integer number n (0 ≤ n ≤ 100 000) — number of black vertices at the beginning.

The following n lines contain two integer numbers each — the coordinates of different black vertices. The coordinates do not exceed 109 by their absolute values.

Output

Output the number of black vertices when the process stops. If the process does not stop, output -1.

Sample Input

4
0 2
2 0
-2 0
0 -2

Sample Output

5

Hint

题意:无限大的棋盘上有黑白两种棋,前后左右都被黑棋包围的白棋都会最终变为黑棋,求最后有多少黑棋。

思路:因为棋盘是无限大的,所以要进行离散化,将有用的坐标节点取出来重新排列,譬如考虑横坐标的离散化,把所有黑子的横坐标取出,去掉重复,按原来的位置先后排序后再把横坐标赋予新的

值,即该横坐标从左到右排列排在第几位,以1开头;纵坐标也可以同理进行离散化。

离散化后所有被黑子包住的白子都将会变成黑子。此时的任务就是数出被包围的白子的个数。可以对离散化后的图形进行一行一行的扫描,白子的个数可以用树状数组进行存储,对于每一行的取出来的每一个黑子的

横坐标,先判断该横坐标在前面的行中有没有被取出过,如果取出过,那么这次取出意味着有上下两个黑子,之间也许夹着白子,那么可以通过树状数组把存储在该横坐标中的白子个数提取出来。树状数组在关于这个横坐标上

白子个数的记录清零,方便下一次记录新情况。之后从这个黑子的横坐标开始直到下一个黑子的横坐标为止,找出两者的坐标差(不含这两个横坐标本身),这个坐标差就是两个黑子之间白子的个数,之后就可以对中间这个

区间中的每一个横坐标在树状数组中对应位置处加1,表示这些横坐标上出现过一个白子并记录之。遍历所有行的所有黑子即可得到最终答案。

AC代码:

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int N_MAX = +;
int N;
int X[N_MAX], Y[N_MAX];
vector<int>line[N_MAX];//一行一行的记录
bool visited[N_MAX];
ll bit0[N_MAX], bit1[N_MAX]; ll sum(ll *b,int i) {
ll s = ;
while (i>) {
s += b[i];
i -= i&-i;
}
return s;
} void add(ll *b,int i,ll x) {
while (i <= N_MAX) {
b[i] += x;
i += i&-i;
}
} ll sum(int to) {
return sum(bit1, to)*to + sum(bit0, to);
} ll sum(ll*b,int from,int to) {//计算区间(from,to]的和
return sum( to) - sum( from);
} void add(int from,int to,ll x) {//对[from,to]区间同时加上值x
add(bit0,from,-x*(from-));
add(bit1,from,x);
add(bit0,to+,x*to);//!!
add(bit1,to+,-x);
} int compress(int *x) {//离散化,每一个x坐标的含义新定义为:所有的x坐标中排第几,1为下标起点
vector<int>v(N);//千万别忘记初始化,使用distance
for (int i = ; i < N;i++) {
v[i] = x[i];
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int i = ; i < N;i++) {//!!!!!!!1
x[i] = distance(v.begin(), lower_bound(v.begin(), v.end(), x[i])) + ;
}
return v.size();
} int main() {
scanf("%d",&N);
for (int i = ; i < N;i++) {
scanf("%d%d",&X[i],&Y[i]);
}
int h = compress(Y);
int w = compress(X);
for (int i = ; i < N; i++) {//离散化后,把坐标存起来,之后即可一行一行的考虑
line[Y[i]].push_back(X[i]);
}
ll res = N;//res记录最终的黑点数
for (int i = ; i <= h;i++) {//!!!!
vector<int>&xs = line[i];//对于每一行,查找黑点之间的白点个数
sort(xs.begin(),xs.end()); //!!!!!!!!!!!!!!!
for (vector<int>::iterator it = xs.begin(); it != xs.end();it++) {
int x = *it;
ll s = sum(bit0, x - , x);//首先判断有没有以该值作为横坐标的白点,记录个数
if (visited[x]) {//如果这个横坐标以前到达过,再次到达就会封闭包围白点
res += s;
}
else
visited[x] = true;
add(bit0,x,-s);//在该横坐标处减去已经做记录的白点,清零,方便下次继续记录
//接下来计算从该黑点开始一直到下一个黑点为止中间的白点的个数
if ((it + ) != xs.end()) {
if(x+<*(it+)-){//中间多于1个白点
add(x + , *(it + )-, );
}
else if (x + == *(it + ) - ) {
add(bit0, x + , );//中间只有一个黑点
}
}
}
}
printf("%lld\n",res);
return ;
}

poj 3109的更多相关文章

  1. POJ - 3109 Inner Vertices

    不存在-1的情况,而且最多一轮就结束了.如果新增加的黑点v0会产生新的黑点v1,那么v0和v1肯定是在一条轴上的,而原来这条轴上已经有黑点了. 离散以后扫描线统计,往线段上插点,然后查询区间上点数. ...

  2. poj很好很有层次感(转)

    OJ上的一些水题(可用来练手和增加自信) (POJ 3299,POJ 2159,POJ 2739,POJ 1083,POJ 2262,POJ 1503,POJ 3006,POJ 2255,POJ 30 ...

  3. POJ题目分类推荐 (很好很有层次感)

    著名题单,最初来源不详.直接来源:http://blog.csdn.net/a1dark/article/details/11714009 OJ上的一些水题(可用来练手和增加自信) (POJ 3299 ...

  4. POJ 2109 Inner Vertices(扫描线+树状数组)

    [题目链接] http://poj.org/problem?id=3109 [题目大意] 在一个棋盘上放满白子,现在把一些白子变成黑子, 如果一个白子上下左右都有黑子,就会变成黑子,问最终黑子个数 [ ...

  5. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  6. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  7. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  8. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  9. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

随机推荐

  1. 天坑之mysql乱码问题以及mysql重启出现1067的错误解决

    相信很多小伙伴都遇到过数据库中文乱码问题,很头疼,明明Navicat上的编码格式都是utf-8是一样的啊? 为什么还是乱码? 原因是Navicat上的数据库编码格式并不是真正的编码格式 ,所以明白了吗 ...

  2. Java中this关键字的用法

    this关键字作用: 1. 如果存在同名成员变量与局部变量时,在方法内部默认是访问局部变量的数据,可以通过this关键字指定访问成员变量的数据. 2. 在一个构造函数中可以调用另外一个构造函数初始化对 ...

  3. k8s 基于NFS部署storageclass pv自动供给

    在k8s中部署有状态应用时,通常需要做数据持久化存储. 后端存储的方式有以下几种: 1.基于宿主机本地的存储方式: (重启pod时,若pod被调度到其他节点上,尽管原来节点上的数据不会丢失,但是其他节 ...

  4. DeepFaceLab小白入门(6):脸部替换以及合成视频!

    前面的都是准备工作,这个环节才是真的换脸.换脸主要分两部分,1,图片换脸,2,把图片合成视频. 7) convert H64 debug.bat 这个环节是和训练环节相对于的,比如我们之前选的是H64 ...

  5. URLError与HTTPError

    urllib2 的异常错误处理 在我们用urlopen或opener.open方法发出一个请求时,如果urlopen或opener.open不能处理这个response,就产生错误. 这里主要说的是U ...

  6. BZOJ 2721: [Violet 5]樱花

    (X-N)(Y-N)=N^2 #include<cstdio> using namespace std; const int mod=1e9+7; int n,cnt,isprime[10 ...

  7. CodeForces 489F DP Special Matrices

    首先统计一下前m行中,有x列1的个数为0,有y列1的个数为1. 设d(i, j)表示有i列1的个数为0,有j列1的个数为1,能到达这个状态的矩阵的个数. 则d(x, y) = 1 每一行都是两个1一起 ...

  8. edge浏览器识别ip地址为手机号的解决办法

    edge浏览器识别ip地址为手机号的解决办法 今天突然发现类似101.231.70.242的ip地址会在edge浏览器里面识别为可点击的链接,后来看了一下,原因就是被识别为手机号了,因为我发现点击的时 ...

  9. webdriver高级应用- 测试HTML5语言实现的视频播放器

    能够获取HTML5语言实现的视频播放器,视频文件的地址.时长,控制播放器进行播放或暂停播放等操作. #encoding=utf-8 import unittest from selenium impo ...

  10. Selenium WebDriver-操作复选框

    #encoding=utf-8 import unittest import time import chardet from selenium import webdriver class Visi ...