Time Limit: 10 sec memory limit: 64 MB
Submit: 1394 solved: 625
[Submit] [Status] [discuss]
Description
There are n black vertices in an infinitely large square mesh, and all other vertices are white (the vertices of the mesh are the points whose coordinates are integers, also known as the whole point ). Each second, all internal White points are black at the same time until there is no internal white point. Your task is to count the number of black spots in the last grid. Definition of internal White points: a white whole point P (x, y) it is an internal white point. if and only when P has at least one Black Point on the left and right sides of the horizontal line (that is, there is X1 <x <X2 to make (x1, Y) and (X2, Y) are black spots), and there is at least one black spot on the top and bottom of the vertical line (that is, there is Y1 <Y <Y2 to make (x, Y1) and (x, Y2) black spots ).
Input
The first line contains an integer N, that is, the number of initial blackpoints. Each line in the following n rows contains two integers (x, y), that is, the coordinates of a black spot. No two black spots share the same coordinates, and the absolute values of coordinates cannot exceed \ (10 ^ 9 \).
Output
The output contains only one row, including the final number of black spots. If the color change process never ends, output-1.
Sample Input
4
0 2
2 0
-2 0
0-2
Sample output
5
Data range
36% of data meets the requirements: n <= 500
64% of data meets the requirements: n <= 30000
100% of data meets the requirements: n <= 100000
Scan questions
It is easy to find that all white spots that can change color must be able to change color in the first second, there are black spots on the top, bottom, and left. And it is impossible to never stop.
Discretization black spots, sorting by Y, and storing 1 at the top and-1 at the bottom of each row with a vector, by the way, the left and right endpoints of each row are saved as arrays.
Finally, scan by row. Add the endpoint and query the Left and Right segments of the row in the interval, and then subtract the next endpoint.
#include<iostream>#include<cstdio>#include<algorithm>#include<vector>#include<cstring>using namespace std;int i,m,n,j,k,zl[100001],zr[100001],x,y,l,r,ll[100001],rr[100001],c[100001],ans;struct vv{ int x,y; } a[100001],b[100001];vector <pair<int,int> > q[100001], p[100001];bool cmp2(vv a,vv b) {if(a.y==b.y) return a.x<b.x; return a.y<b.y;}void add(int now,int z) { for(int i=now;i<=y;i+=i & -i) c[i]+=z;}int ask(int now){ int ans=0; for(int i=now;i>0;i-=i & -i) ans+=c[i]; return ans; }int main(){ scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d%d",&a[i].x,&a[i].y); zl[i]=a[i].x; zr[i]=a[i].y; } sort(zl+1,zl+1+n); sort(zr+1,zr+1+n); x=unique(zl+1,zl+1+n)-zl-1; y=unique(zr+1,zr+1+n)-zr-1; for(i=1;i<=n;i++) { a[i].x=lower_bound(zl+1,zl+1+x,a[i].x)-zl, a[i].y=lower_bound(zr+1,zr+1+y,a[i].y)-zr; b[i].x=a[i].x; b[i].y=a[i].y; } sort(a+1,a+1+n,cmp2); l=0x3f3f3f3f; r=0; memset(ll,0x3f,sizeof(ll)); for(i=1;i<=n;i++) { if(a[i].y!=a[i-1].y) { if(r) p[r].push_back(make_pair(a[i-1].y,-1)), q[l].push_back(make_pair(a[i-1].y,1)); l=0x3f3f3f3f; r=0; } l=min(a[i].x,l); r=max(a[i].x,r); ll[a[i].x]=min(ll[a[i].x],a[i].y); rr[a[i].x]=max(rr[a[i].x],a[i].y); } if(r) p[r].push_back(make_pair(a[n].y,-1)), q[l].push_back(make_pair(a[n].y,1)); for(i=1;i<=x;i++) { for(j=0;j<q[i].size();j++) add(q[i][j].first,q[i][j].second); if(rr[i]) ans+=ask(rr[i])-ask(ll[i]-1); for(j=0;j<p[i].size();j++) add(p[i][j].first,p[i][j].second); } printf("%d",ans);}
1818: [cqoi2010] internal White points