1818: [Cqoi2010]内部白点
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 719 Solved: 351
[ Submit][ Status][ Discuss]
Description
无限大正方形网格里有n个黑色的顶点,所有其他顶点都是白色的(网格的顶点即坐标为整数的点,又称整点)。每秒钟,所有内部白点同时变黑,直到不存在内部白点为止。你的任务是统计最后网格中的黑点个数。 内部白点的定义:一个白色的整点P(x,y)是内部白点当且仅当P在水平线的左边和右边各至少有一个黑点(即存在x1 < x < x2使得(x1,y)和(x2,y)都是黑点),且在竖直线的上边和下边各至少有一个黑点(即存在y1 < y < y2使得(x,y1)和(x,y2)都是黑点)。
Input
输入第一行包含一个整数n,即初始黑点个数。以下n行每行包含两个整数(x,y),即一个黑点的坐标。没有两个黑点的坐标相同,坐标的绝对值均不超过109。
Output
输出仅一行,包含黑点的最终数目。如果变色过程永不终止,输出-1。
Sample Input
4
0 2
2 0
-2 0
0 -2
0 2
2 0
-2 0
0 -2
Sample Output
5
数据范围
36%的数据满足:n < = 500
64%的数据满足:n < = 30000
100%的数据满足:n < = 100000
数据范围
36%的数据满足:n < = 500
64%的数据满足:n < = 30000
100%的数据满足:n < = 100000
解题思路:和那个什么墓地是一样的,还要更简单,先hash,然后再树状数组。(注意细节)
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
long long ans;
int n,len,lgg;
struct ss
{
int zhi,dui;
}x[100001],y[100001];
struct st
{
int x,y;
}q[100001];
int h[100001],heng[100001],s[100001],shu[100001];
int f[100001],sh[100001];
inline int read()
{
char y; int x=0,f=1; y=getchar();
while (y<'0'||y>'9') {if (y=='-') f=-1; y=getchar();}
while (y>='0'&&y<='9') {x=x*10+int(y)-48; y=getchar();}
return x*f;
}
int query(int now)
{
int sum=0;
while (now>0)
{
sum+=f[now];
now-=now&-now;
}
return sum;
}
void change(int now,int zhi)
{
while (now<=lgg)
{
f[now]+=zhi;
now+=now&-now;
}
}
bool cmp(ss xg,ss yg)
{
return xg.zhi<yg.zhi;
}
bool cop(st xg,st yg)
{
if (xg.x!=yg.x)return xg.x<yg.x;else return xg.y<yg.y;
}
int main()
{
n=read();
for (int i=1;i<=n;++i)
{
x[i].zhi=read(); y[i].zhi=read();
x[i].dui=i; y[i].dui=i;
}
sort(x+1,x+n+1,cmp);
sort(y+1,y+n+1,cmp);
int tcc1=0,tcc2=0;
for (int i=1;i<=n;++i)
{
if (x[i].zhi!=x[i-1].zhi)
{
++tcc1; q[x[i].dui].x=tcc1;
}else q[x[i].dui].x=tcc1;
if (y[i].zhi!=y[i-1].zhi)
{
++tcc2; q[y[i].dui].y=tcc2;
}else q[y[i].dui].y=tcc2;
}
sort(q+1,q+n+1,cop); lgg=-1;
for (int i=1;i<=n;++i)
{
h[i]=heng[q[i].x]; ++heng[q[i].x];
s[i]=shu[q[i].y]; ++shu[q[i].y];
lgg=max(lgg,q[i].y);
}
ans=n;
len=q[n].x;
for (int i=1;i<=n;++i)
{
if (q[i-1].x==q[i].x && q[i].x==q[i+1].x && s[i]+1!=shu[q[i].y] && s[i]!=0) --ans;
}
int now=1; memset(f,0,sizeof(f));
for (int i=1;i<=q[n].x;++i)
{
int u=now;
for (;q[now].x==i;++now)
{
if (sh[q[now].y]+1==shu[q[now].y] && sh[q[now].y]!=0) change(q[now].y,-1);
}
int u1=query(q[now-1].y-1); int u2=query(q[u].y);
if (now-1!=u) ans+=u1-u2;
for (int j=u;j<=now-1;++j)
{
if (sh[q[j].y]==0 && sh[q[j].y]+1!=shu[q[j].y]) change(q[j].y,1);
++sh[q[j].y];
}
}
printf("%lld",ans);
}
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
long long ans;
int n,len,lgg;
struct ss
{
int zhi,dui;
}x[100001],y[100001];
struct st
{
int x,y;
}q[100001];
int h[100001],heng[100001],s[100001],shu[100001];
int f[100001],sh[100001];
inline int read()
{
char y; int x=0,f=1; y=getchar();
while (y<'0'||y>'9') {if (y=='-') f=-1; y=getchar();}
while (y>='0'&&y<='9') {x=x*10+int(y)-48; y=getchar();}
return x*f;
}
int query(int now)
{
int sum=0;
while (now>0)
{
sum+=f[now];
now-=now&-now;
}
return sum;
}
void change(int now,int zhi)
{
while (now<=lgg)
{
f[now]+=zhi;
now+=now&-now;
}
}
bool cmp(ss xg,ss yg)
{
return xg.zhi<yg.zhi;
}
bool cop(st xg,st yg)
{
if (xg.x!=yg.x)return xg.x<yg.x;else return xg.y<yg.y;
}
int main()
{
n=read();
for (int i=1;i<=n;++i)
{
x[i].zhi=read(); y[i].zhi=read();
x[i].dui=i; y[i].dui=i;
}
sort(x+1,x+n+1,cmp);
sort(y+1,y+n+1,cmp);
int tcc1=0,tcc2=0;
for (int i=1;i<=n;++i)
{
if (x[i].zhi!=x[i-1].zhi)
{
++tcc1; q[x[i].dui].x=tcc1;
}else q[x[i].dui].x=tcc1;
if (y[i].zhi!=y[i-1].zhi)
{
++tcc2; q[y[i].dui].y=tcc2;
}else q[y[i].dui].y=tcc2;
}
sort(q+1,q+n+1,cop); lgg=-1;
for (int i=1;i<=n;++i)
{
h[i]=heng[q[i].x]; ++heng[q[i].x];
s[i]=shu[q[i].y]; ++shu[q[i].y];
lgg=max(lgg,q[i].y);
}
ans=n;
len=q[n].x;
for (int i=1;i<=n;++i)
{
if (q[i-1].x==q[i].x && q[i].x==q[i+1].x && s[i]+1!=shu[q[i].y] && s[i]!=0) --ans;
}
int now=1; memset(f,0,sizeof(f));
for (int i=1;i<=q[n].x;++i)
{
int u=now;
for (;q[now].x==i;++now)
{
if (sh[q[now].y]+1==shu[q[now].y] && sh[q[now].y]!=0) change(q[now].y,-1);
}
int u1=query(q[now-1].y-1); int u2=query(q[u].y);
if (now-1!=u) ans+=u1-u2;
for (int j=u;j<=now-1;++j)
{
if (sh[q[j].y]==0 && sh[q[j].y]+1!=shu[q[j].y]) change(q[j].y,1);
++sh[q[j].y];
}
}
printf("%lld",ans);
}