Half (silos)

Macondo is a strange town, town houses along the south bank of a river was built, and the town's only residents of a lifetime in the range of a fixed radius near his home event, some residents will never be in contact with each other, even if they live a lifetime never in contact with. 

Macondo town a total of n a house to the west end of town from the operator, residents of the home's location is p, the scope of their activities is r, I ask Macondo never in between the town will have a total number of households in contact with.

Entry

Line 1: a number N, the number of the house (1 <= N <= 50000 )
of 2 - N + 1 line: the number of each line 2 P, R separated by a space, P represents the location of the house, R represents this range of household activities half
radius (1 <= P, R < = 10 ^ 9)

Export

How much for never in contact with a total output of households.

Sample input Copy

4
1 1
2 1
3 2
4 1

Sample output Copy

1

prompt

1. Sample explanation
4 houses are located 1, 2, 3, 4, the movable range of radii 1, 1, 2, 1, then {1, 2}, {1, 3} {2, 3} {2, 4} {3,4} five activities are resident on the scope of the intersection, only {1, 4} is never in contact with the.
2. Data range
for 10% of the data, 1≤N≤10;
for 40% of the data, 1≤N≤2000;
to 100% of the data, 1≤N≤50000.
Topic Analysis:

Our scope of activities can be seen every round residents, for a round, we can determine it on a diameter line

[L, R & lt] , if the diameter of the intersection of the two circles do not, it may be considered from the phases of these two circles.


We L [] and R & lt [] are recorded about the endpoints of each diameter were sorted. From left to right through each R & lt [I] , exists j such that

l [j]> r [i ] is described j and l values are further to the right circle and i are away from.

Scheduling complexity of O (nlogn) ; each i Comparative endpoint using the binary complexity of O (logN) , the overall O (nlogn) ; Therefore, the total complexity

O (nlogn) .

The latter half portion can also be optimized to O (n-) , all segments of the upcoming start and end sorting mixed, and then individually for each access point. For each starting point and end point as long as the number of records in front of him. Sorting O (nlogn) , traversing O (n-) , the overall complexity is O (nlogn) .

AC Code:

 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e7+10;
struct node{
    int l,r;
};
node a[maxn];
bool cmp(node x,node y){
    return x.l < y.l ;
}
int ef(int l,int r,int x){
    int ans;
    while(l<=r){
        int mid=(l+r)/2;
        if(a[mid].l<x){
            l=mid+1;
        }
        else if(a[mid].l>=x){
            r=mid-1;        
        }
    }
    return l;
}
int main()
{
    int n,x,y;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        a[i].l=x-y;
        a[i].r=x+y;
    }
    sort(a,a+n-, CMP);
     int ANS = 0 ;
     for ( int I = 0 ; I <N- . 1 ; I ++ ) { 
        ANS + = n--EF (I + . 1 , N- . 1 , A [I] .r + . 1 ); / / bipartite find the i-th minimum phase from the residents of that 
    }                   
    the printf ( " % D \ n- " , ANS);
     return  0 ; 
}

 

 

 

 

Guess you like

Origin www.cnblogs.com/lipu123/p/12170160.html