LinesTime
limit:5000/2500 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 620 Accepted Submission (s): 288
Problem Descriptionjohn has several lines. The lines is covered on the X axis. Let's a point which are covered by the most lines. John wants to know how many lines cover A.
Inputthe first line contains a single integer t ( 1 ≤ t ≤ 100 ) /span> (The data for N> Less than cases), indicating the number of test cases.
Each test case begins with an integer n(1≤n ≤ Ten 5 ) , indicating the number of lines.
Next N lines contains and integers X i and Y i (1≤ X i ≤ Y i ≤ ten 9 ) , describing a line.
Outputfor each case, output an integer means how many lines cover A.
Sample Input
251 2 2 22 43 45 100051 12 23 34 45 5
Sample Output
31
Test instructions is to seek the maximum of the interval, you can use the line segment tree + discretization processing, so that the coordinate interval becomes smaller.
such as [2,5],[3, 100] After discretization, the first sort {2,3,5,100}, corresponding subscript value can be changed to {1,2,3,4}, corresponding to the can go to weight (see Code).
The interval changes to [1,3],[2,4], and the size of the interval is unchanged.
#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include < algorithm>using namespace std; #define N 200005#define ll __int64struct node{int l,r; int v,f; V record maximum, F records the value accumulated by the current layer}f[n*3];struct ST//record the left and right end of the original interval {int x,id;} A[n];int pos[n/2][2]; Record interval endpoint BOOL CMP (ST A,st b) {return a.x<b.x;} void creat (int t,int l,int r) {f[t].l=l; F[t].r=r; f[t].v=f[t].f=0; if (l==r) return; int tmp=t<<1,mid= (L+R) >>1; Creat (Tmp,l,mid); Creat (tmp|1,mid+1,r);} void update (int t,int l,int r) {int tmp=t<<1,mid= (F[T].L+F[T].R) >>1; if (f[t].l==l&&f[t].r==r) {f[t].v++; f[t].f++; return; } if (F[T].F)//down operation Push_down, each time adding a cumulative value of {F[TMP].V+=F[T].F; F[TMP|1].V+=F[T].F; F[TMP].F+=F[T].F; F[TMP|1].F+=F[T].F; f[t].f=0; Mark value F is set to zero} if (r<=mid) update (TMP,L,R); else if (l>mid) update (tmp|1,L,R); else {update (TMP,L,MID); Update (TMP|1,MID+1,R); } f[t].v=max (F[TMP].V,F[TMP|1].V); Upper pressure operation Push_up}int Main () {int t,i,n; scanf ("%d", &t); while (t--) {scanf ("%d", &n); for (i=0;i<n;i++) {scanf ("%d%d", &pos[i][0],&pos[i][1]); A[I*2].X=POS[I][0]; a[i*2].id=-(i+1); Record subscript, negative value is starting point, positive value is end point a[i*2+1].x=pos[i][1]; a[i*2+1].id=i+1; } sort (a,a+n*2,cmp); int t=1,x=a[0].x; T is the coordinate value for the re-assigned interval for (i=0;i<n*2;i++) {if (x!=a[i].x)//can achieve the effect of removing duplicate values { t++; x=a[i].x; } if (a[i].id<0) pos[-1*a[i].id-1][0]=t; else pos[a[i].id-1][1]=t; } creat (1,1,n*2); for (i=0;i<n;i++) {update (1,pos[i][0],pos[i][1]); } printf ("%d\n", F[1].V); } return 0;}
HDU 5124 Lines (segment tree + discretization)