This kind of problem first sorts the interval and the line segment, the sorting method see code CMP
Then analyze one of the points and evaluate with sum
POJ 3067
Description
Japan plans to welcome the ACM ICPC World Finals and a lot of roads must is built for the venue. Japan is Tall island with N cities on the East coast and M cities on the West coast (M <=, N <= 1000). K superhighways would be build. Cities on each coast is numbered 1, 2, ... Each superhighway are straight line and connects city in the East coast with city of the West coast. The funding for the construction are guaranteed by ACM. A major portion of the sum is determined by the number of crossings between superhighways. At the most of the superhighways cross at the one location. Write A program that calculates the number of the crossings between superhighways.
Input
The input file starts with t-the number of test cases. Each test case is starts with three numbers–n, M, K. Each of the next K lines contains, numbers–the numbers of cities connected by the superhighway. The first one is the number of the city on the East coast and second one are the number of the city of the West coast.
Output
For each test case, write one line in the standard output:
Test Case Number: (Number of crossings)
Sample Input
13 4 41 42 33 23 1
Sample Output
Test Case 1:5
#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <string > #include <algorithm> #define LL long long#define inf 0x3f3f3f3fusing namespace std;struct line{int x, y;} Q[1000100];int C[1010];int cmp (line A,line b) {if (a.x==b.x) return a.y>b.y; return a.x>b.x;} int n,m,k;int lowbit (int x) {return x& (-X);} void Add (int x,int val)//subscript for x value increases val{while (x<=m) {c[x]+=val; X+=lowbit (x); }}int sum (int x)//1~x array of subscript and {int ans=0; while (x>0) {ans+=c[x]; X-=lowbit (x); } return ans; int main () {int T; cin>>t; int kase=1; while (t--) {scanf ("%d%d%d", &n,&m,&k); for (int i=0;i<=k-1;i++) scanf ("%d%d", &q[i].x,&q[i].y); Sort (q,q+k,cmp); Memset (C,0,sizeof (c)); LL cnt=0; for (int i=0;i<=k-1;i++) {cnt+=sum (q[i].y-1); Add (q[i].y,1); } cout<< "Test case" <<kase++<< ":" <<cnt<<endl; } return 0;}
POJ 2481
Description
Farmer John ' s cows has discovered that the clover growing along the ridge of the hill (which we can think of as a One-dim Ensional number line) in he field is particularly good.
Farmer John had N cows (we number the cows from 1 to N). Each of Farmer John's N cows have a range of clover that she particularly likes (these ranges might overlap). The ranges is defined by a closed interval [s,e].
But some cows is strong and some is weak. Given Cows:cow I and Cow J, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei-si > EJ-SJ, we say that cow I was stronger than cow J.
For each cow, how many cows is stronger than her? Farmer John needs your help!
Input
The input contains multiple test cases.
For each test case, the first line was an integer n (1 <= n <= 5), and which is the number of cows. Then come N lines, the i-th of which contains both Integers:s and E (0 <= S < E <= 5) specifying the start end Location respectively of a range preferred by some cow. Locations is given as distance from the start of the ridge.
The end of the input contains a single 0.
Output
For each test case, output one line containing n space-separated integers, the i-th of which specifying the number of cows That is stronger than cow I.
Sample Input
31 20 33 40
Sample Output
1 0 0
#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <string > #include <algorithm> #define LL long long#define inf 0x3f3f3f3fusing namespace Std;int c[100010];int num[100010 ];struct node{int id; int l,r;} Q[100010];int CMP (node A,node b) {if (A.R==B.R) return a.l<b.l; return A.R>B.R;} int lowbit (int x) {return x& (-X);} void Add (int x,int val)//subscript for x value increases val{while (x<=100000)//n is the size of the array, error-prone {c[x]+=val; X+=lowbit (x); }}int sum (int x)//1~x array of subscript and {int ans=0; while (x>0) {ans+=c[x]; X-=lowbit (x); } return ans; int main () {int n; while (cin>>n) {if (n==0) return 0; for (int i=0;i<=n-1;i++) {scanf ("%d%d", &Q[I].L,&Q[I].R); Q[i].id=i; } sort (q,q+n,cmp); Memset (C,0,sizeof (c)); for (int i=0;i<=n-1;i++) {if (q[i].l==q[i-1].l&&q[i].r==q[i-1].r&&i!=0) num[q[i].id]=num[q[i-1].id]; else Num[q[i].id]=sum (q[i].l+1); Add (q[i].l+1,1); } for (int i=0;i<=n-2;i++) cout<<num[i]<< ""; cout<<num[n-1]<<endl; } return 0;}
POJ 3067 poj 2481 tree-shaped array variants + interval ordering