[+ Discrete Fenwick tree] CodeForces - 652D Nested Segments

Nested Segments
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given n segments on a line. There are no ends of some segments that coincide. For each segment find the number of segments it contains.

Input

The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of segments on a line.

Each of the next n lines contains two integers li and ri ( - 109 ≤ li < ri ≤ 109) — the coordinates of the left and the right ends of the i-th segment. It is guaranteed that there are no ends of some segments that coincide.

Output

Print n lines. The j-th of them should contain the only integer aj — the number of segments contained in the j-th segment.

Examples
input
Copy
4
1 8
2 3
4 7
5 6
output
Copy
3
0
1
0
input
Copy
3
3 4
1 5
2 6
output
Copy
0
1
1

 The meaning of problems: there are n a straight line, no two segments have the same endpoints, each segment requires that each segment contains a number

Here are two kinds of practice

1 idea:

The right to record the coordinates of points, recording the coordinates of the point at the right end of the right end point discrete, maintaining the number of prefixes used Fenwick tree and the right point,
to the left point in ascending order, from the left end of the leftmost point to start looking,
because the left point ascending , so the current left point than all the right points are small (the start is satisfied, then we must continue to delete the right point to maintain this state), the current is smaller than the left point behind it, if it's left behind corresponding to the point right point less than the current right end point, the line piece is included in the current line), the right to see how many points you can know that the current segment contains a number line right before the current this point
because we want to ensure that current left All right end point than a little to the right so that only count how many points will be able to know that the current segment contains a number line right before the current point, if left to the next point on a line segment is not included in the next line in the Therefore, if left to the next point on a right end point will be deleted, so you want to delete this current has looked right Point

2 ideas:

The right to record the coordinates of points, recording the coordinates of the point at the right end of the right end point discrete, maintaining the number of prefixes used Fenwick tree and the right point,
to the left point in descending order from the left end of the rightmost point to start looking
because the left point in descending order so after the current left end point are not bigger than it is not calculated in the left point, if larger than its left end points are calculated over and the left point corresponding to the right end point corresponding to the ratio now left point right a little, then the current line segment piece must be contained, so that the left end in descending order of addition of the corresponding point of the right end, the right end can not guarantee the current smaller than the current left end

to sum up:

Two ideas in common are to ensure that no smaller than, the right point, so you only need left before the current statistics of how many points the right point right before the current point in the left corresponding to the point you can know this segment currently contains a number segments, their difference is that ideas 1 in advance all the right are added to the list, and then delete the query every time, but the idea is to start with a maximum of 2 points to start adding the right point left

 

Ideas 1 Code:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int amn=2e5+5;
 5 int bit[amn],n,tp,b[amn];
 6 struct node{
 7     int l,r,id;
 8 } a[amn];
 9 int ans[amn],jg[amn],pos;
10 map<ll,int> mp;
11 bool cmp(node a,node b){
12     if(a.r==b.r)return a.r<b.r;
13     return a.l<b.l;
14 }
15 int sum(int i){
16     int s=0;
17     while(i){
18         s+=bit[i];
19         i-=i&-i;
20     }
21     return s;
22 }
23 void add(int i,int x){
24     while(i<=tp){
25         bit[i]+=x;
26         i+=i&-i;
27      }
 28  }
 29  int main () {
 30      iOS :: sync_with_stdio ( 0 );
 31 is      CIN >> n-;
 32      TP = 0 ;
 33 is      for ( int I = . 1 ; I <= n-; I ++ ) {
 34 is          A [I ] .id = I;           /// recording original order 
35          CIN >> A [I] .L >> A [I] .r;
 36          B [TP ++] = A [I] .r;      /// recorded the coordinates of the lower right end point of 
37 [          ANS [I] = 0 ;            /// initialization answer array 
38      }
39      Sort (B + . 1 , B + . 1 + TP);        /// coordinates sorted to a right end point 
40      int MTP = . 1 ;
 41 is      for ( int I = . 1 ; I <= TP; I ++ ) {
 42 is          MP [B [I] ] = MTP;        /// the right discrete point 
43 is          MTP ++ ;
 44 is      }
 45      for ( int I = . 1 ; I <= n-; I ++ ) {
 46 is          the Add (MP [A [I] .r], . 1 );   / // use the tree array and the number of prefixes to maintain the right end 
47      }
 48     Sort (A + . 1 , A + . 1 + n-, CMP);     /// give the leftmost point in ascending order, from the left end point of the leftmost start looking 
49      for ( int I = . 1 ; n-I <=; I ++ ) {
 50          ANS [A [I] .id] = SUM (MP [a [I] .r] - . 1 ); /// as ascending left point, the left end point of the current calculated than all but the right point is small (start satisfying , and then we have to continue to maintain the right end deleting this state), the left end of the current point is smaller than behind it, if it is left behind a right end point corresponding to the right point is less than the current point, the line piece is included in the current), there is a prior segment in the current look right point right number of points can know that the current number of segments comprising a segment 
51 is          the Add (MP [a [I] .r], - . 1 );              /// because we want ensure that all current left than the right point a little in order to make the right point just count how many will be able to know that the current segment contains a number line right before the current point, if left to the next point on a line segment is not included in the next in the segment, so if left to the next point on a right end point will be deleted, so you want to delete this current has been looking for Through the right end point 
52      }
 53     for ( int I = . 1 ; I <= n; I ++ ) {
 54 is          the printf ( " % D \ n " , ANS [I]);
 55      }
 56 is  }
 57 is  / * **
 58  a line with a straight line n, no two segments have the same end point, each segment requires that each segment contains a number
 59  recording a right end point coordinates, a right end point coordinates of the recording, the right end point discretization, the number of prefixes to maintain the right end of the array and a tree,
 60  give the leftmost point in ascending order, beginning from the left to find the left-most point,
 61  since ascending the left point, the left point than the current had not calculated all the right points are small (the start is satisfied, then we have to constantly be deleted right this point to maintain the state), the current is also smaller than its left endpoint to the latter, if it is left behind a right end point corresponding to the right point is less than the current point, the line piece is included in the current line segment), to see the current how many points can the right to know the current number of this segment contains the right end of the line before
 62  because we want to ensure that all current than the left point to the right so that only a small number of statistics point right before the current right end point will be able to know that the current the line includes more than Line, if left to the next point on a line segment is not included in the next line segment, so if left to the next point on a right end point will be deleted, so you want to delete this current has been approached in the right spot
63 ***/

 

Ideas 2 code:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int amn=2e5+5;
 5 int bit[amn],n,tp,b[amn];
 6 struct node{
 7     int l,r,id;
 8 } a[amn];
 9 int ans[amn],jg[amn],pos;
10 map<ll,int> mp;
11 bool cmp1(node a,node b){
12     if(a.r==b.r)return a.r>b.r;
13     return a.l>b.l;
14 }
15 int sum(int i){
16     int s=0;
17     while(i){
18         s+=bit[i];
19         i-=i&-i;
20     }
21     return s;
22 }
23 void add(int i,int x){
24     while(i<=tp){
25         bit[i]+=x;
26         i+=i&-i;
27      }
 28  }
 29  int main () {
 30      iOS :: sync_with_stdio ( 0 );
 31 is      CIN >> n-;
 32      TP = 0 ;
 33 is      for ( int I = . 1 ; I <= n-; I ++ ) {
 34 is          A [I ] .id = I;           /// recording original order 
35          CIN >> A [I] .L >> A [I] .r;
 36          B [TP ++] = A [I] .r;      /// recorded the coordinates of the lower right end point of 
37 [          ANS [I] = 0 ;            /// initialization answer array 
38      }
39      Sort (B + . 1 , B + . 1 + TP);        /// coordinates sorted to a right end point 
40      int MTP = . 1 ;
 41 is      for ( int I = . 1 ; I <= TP; I ++ ) {
 42 is          MP [B [I] ] = MTP;        /// the right discrete point 
43 is          MTP ++ ;
 44 is      }
 45      Sort (a + . 1 , a + . 1 + n-, CMP1);    /// to sort in descending order the left, from the left to start looking for the rightmost point 
46 is      for ( int I = . 1 ; I <= n-; I ++ ) {
 47         the Add (MP [A [I] .r], . 1 );   /// as sort in descending order the left, so left after the current point is not greater than its not calculated through the left point, if it is larger than the left end point is calculated that the left and right end point corresponding to the point smaller than the right point now corresponds to the left point, the piece must be current line segment included so the left end in descending order of adding the corresponding right end point, we can not currently guarantee the right point smaller than the current left end 
48          ANS [a [I] .id] = SUM (MP [a [I] .r] - . 1 );
 49      }
 50      for ( int I = . 1 ; I <= n-; I ++) {   /// two ideas in common are to ensure that no smaller than, the right point, so you only need left before the current statistics of how many points the right point right before the current point in the left corresponding to the point you can know the current this segment contains a number of segments, their difference is that ideas 1 in advance all the right are added to the list, and then delete the query every time, but the idea is to start with a maximum of 2 points left to start adding the right point 
51          printf ( " D% \ n- " , ANS [I]);
 52 is      }
 53 is  }
 54 is  / * **
55  a line with a straight line n, no two segments have the same endpoints, each segment requires that each segment contains a number
 56  coordinate points in a right end recording, a recording at a right end point coordinate, the right end point discretization, a tree like array maintenance of the number of prefixes right point and,
 57  to the left end point in descending order from the left end of the rightmost point to start looking
 58  because the left point in descending order, so after the current left point are no major uncalculated had left point than it is, if it is larger than the left end points are calculated after the little left and right end point corresponding to the point now corresponds to the left than the right point point, the piece must be current line segment included so the left end in descending order of adding the corresponding right point, can not currently guarantee the right point less than the current left end of the
 59  two ideas in common are to ensure that no less than it was before the current left point right point, so you only need to have a statistical point right before the current point corresponding to the left how many points can know the right end of this segment currently contains a number of segments, their difference is that ideas 1 in advance all the right end are added Go, and then delete the query every time, but the idea is to start with a maximum of 2 points to start adding the right point left
 60  ** * /

 

Guess you like

Origin www.cnblogs.com/brainm/p/11310745.html