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×(N1)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+EiSj and Sj+EjSi. CD equals to the number of competitive matches among all N×(N1)2 matches in the tournament.

Input

One integer T (T20) on the first line indicates the number of cases. The first line of each case contains one integer N (1N10000) -- the number of competitors. Then N lines follows. The ithline contains two integer Si and Ei (1Si,Ei100000000) which are defined in the description.

Output

For each case, print the value of CD on a line.

Sample input and output

Sample InputSample 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

The 8th UESTC Programming Contest Final
 
解题报告
题目意思很简单,找出给出这些整数中有多少队满足
Si+EiSj and Sj+EjSi
第一想法是暴力,too simple...结果肯定是TLE
How to slove?
我们先对Si + Ei 按照从大到小排序,
对于排序后的序列来讲,若有i < j,若i能到pos位,则j能到达的位置肯定<=pos
因此维护一个S单调不减,每次更新答案即可.
注意到我们不是一次把某队数满足的所有其他队数加上去,而是不断累加(类似于将来的值)
 
#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;
}

 

posted on 2015-04-25 00:31  天心散人  阅读(254)  评论(0编辑  收藏  举报