A sports company is planning to advertise in a tournament. It is a single round-robin tournament, that's to say competitors play with all the others once. The company thinks that the advertising impact is proportional to the so called competitiveness degree
(CD) of the tournament.
CD is calculated in the following way: We assume there're N competitors in the tournament and of course N×(N−1)2 matches in total. For each competitor we define two values S and E which stand for skill and experience. We say a match between competitor i and competitor j is competitive if and only if Si+Ei≥Sj and Sj+Ej≥Si. CD equals to the number of competitive matches among all N×(N−1)2 matches in the tournament.
Input
One integer T (T≤20) on the first line indicates the number of cases. The first line of each case contains one integer N (1≤N≤10000) -- the number of competitors. Then N lines follows. The ithline contains two integer Si and Ei (1≤Si,Ei≤100000000) which are defined in the description.
Output
For each case, print the value of CD on a line.
Sample input and output
Sample Input | Sample Output |
---|---|
3 2 1 2 4 1 2 1 2 2 2 5 1 9 5 4 3 4 2 2 6 2 |
0 1 8 |
Source
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <set> using namespace std; const int maxn = 1e4 + 5; int n; multiset<int>s; typedef struct data { int s,e; friend bool operator < (const data & x,const data & y) { return x.s + x.e > y.s + y.e; } }; data A[maxn]; int main(int argc ,char * argv[]) { int Case; scanf("%d",&Case); while(Case--) { int ans = 0; scanf("%d",&n); for(int i = 0 ; i < n ; ++ i) scanf("%d%d",&A[i].s,&A[i].e); sort(A,A+n); s.clear(); s.insert(A[0].s); set<int>::iterator it = s.begin(); int cot = 1; for(int i = 1 ; i < n ; ++ i) { int temp = A[i].s + A[i].e; while( s.size() > 0 && *it > temp) { s.erase(it--); cot--; } ans += cot++; s.insert(A[i].s); if (A[i].s >= *it) it++; } cout << ans << endl; } return 0; }