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

The 8th UESTC Programming Contest Final
 
解题报告
题目意思很简单,找出给出这些整数中有多少队满足
Si+Ei≥Sj and Sj+Ej≥Si
第一想法是暴力,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 + ;
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 = ;
scanf("%d",&n);
for(int i = ; i < n ; ++ i)
scanf("%d%d",&A[i].s,&A[i].e);
sort(A,A+n);
s.clear();
s.insert(A[].s);
set<int>::iterator it = s.begin();
int cot = ;
for(int i = ; i < n ; ++ i)
{
int temp = A[i].s + A[i].e;
while( s.size() > && *it > temp)
{
s.erase(it--);
cot--;
}
ans += cot++;
s.insert(A[i].s);
if (A[i].s >= *it)
it++;
}
cout << ans << endl;
}
return ;
}

UESTC_Tournament CDOJ 124的更多相关文章

  1. cdoj 1489 老司机采花

    地址:http://acm.uestc.edu.cn/#/problem/show/1489 题目: 老司机采花 Time Limit: 3000/1000MS (Java/Others)     M ...

  2. NYOJ题目124中位数

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAssAAAJUCAIAAABsWvwaAAAgAElEQVR4nO3dPXLjuraG4TsJ5xqIYw

  3. Error -27780: [GENERAL_MSG_CAT_SSL_ERROR]connect to host "124.202.213.70" failed: [10054] Connection reset by peer [MsgId: MERR-27780]

    解决方案一: 备注: 此方案如果请求响应时间太长,勾选"WinInet replay instead of Sockets(Windows only)"将会导致如下错误:

  4. TypeError: argument to reversed() must be a sequence ERROR basehttp 124 "GET /admin/ HTTP/1.1" 500 114103 Performing system checks...

    Error Msg TypeError: argument to reversed() must be a sequence ERROR basehttp 124 "GET /admin/ ...

  5. leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)

    124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...

  6. SGU 124. Broken line 射线法 eps的精准运用,计算几何 难度:3

    124. Broken line time limit per test: 0.25 sec. memory limit per test: 4096 KB There is a closed bro ...

  7. 编写高质量代码改善C#程序的157个建议——建议124:考虑在命名空间中使用复数

    建议124:考虑在命名空间中使用复数 如果有一组功能相近的类型被分到了同一个命名空间想,可以考虑为命名空间使用复数. 最典型的例子有,在FCL中,我们需要把所有的非泛型集合类集中在一起存放,所以就有了 ...

  8. Loj #124. 除数函数求和

    链接:https://loj.ac/problem/124 就是筛一下积性函数. #include<bits/stdc++.h> #define ll long long #define ...

  9. CDOJ 1324 卿学姐与公主(分块)

    CDOJ 1324 卿学姐与公主(分块) 传送门: UESTC Online Judgehttp://acm.uestc.edu.cn/#/problem/show/1324 某日,百无聊赖的卿学姐打 ...

随机推荐

  1. C++默认构造函数的一点说明

    大多数C++书籍都说在我们没有自己定义构造函数的时候,编译器会自动生成默认构造函数.其实这句话我一直也是 深信不疑.但是最近看了一些资料让我有了一点新的认识. 其实我觉得大多数C++书籍之所以这样描述 ...

  2. Jndi and c3p0 in Tomcat

    Tomcat 中Jndi是使用Tomcat自带的连接池抛弃Tomcat自带的连接池.使用c3p0 . 环境:Tomcat 5.5.20下面配置只适合Tomcat 5.5.X 下面来看Jndi 与 c3 ...

  3. hdu 5288 OO’s Sequence(计数)

    Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the number o ...

  4. Jpeg(模拟)

    Jpeg Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status  ...

  5. jQuery Pagination Ajax分页插件中文详解(摘)

    jQuery Pagination Ajax分页插件中文详解 by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxin ...

  6. COCOS2D-X 不反复随机数

    srand(time(NULL)); int a[5]; for(int i=0;i<5;i++) { a[i]=CCRANDOM_0_1()*5; } srand放在循环外面

  7. vim的正则表达式(二)应用实例

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  8. Qt快速入门系列教程目录

    Qt快速入门系列教程目录

  9. EXPDP和IMPDP简单测试

    一.EXPDP和IMPDP使用说明  Oracle Database 10g引入了最新的数据泵(Data Dump)技术,数据泵导出导入(EXPDP和IMPDP)的作用 1)实现逻辑备份和逻辑恢复.  ...

  10. java public protect default private

    (1)对于public修饰符,它具有最大的访问权限,可以访问任何一个在CLASSPATH下的类.接口.异常等.它往往用于对外的情况,也就是对象或类对外的一种接口的形式. (2)对于protected修 ...