题目链接:

Wool

Time Limit: 8000/4000 MS (Java/Others)   

 Memory Limit: 262144/262144 K (Java/Others)

Problem Description
At dawn, Venus sets a second task for Psyche.

She is to cross a river and fetch golden wool from violent sheep who graze on the other side.

The sheep are wild and tameless, so Psyche keeps on throwing sticks to keep them away.

There are n sticks on the ground, the length of the i-th stick is ai.

If the new stick she throws forms a triangle with any two sticks on the ground, the sheep will be irritated and attack her.

Psyche wants to throw a new stick whose length is within the interval [L,R]. Help her calculate the number of valid sticks she can throw next time.

 
Input
The first line of input contains an integer T (1≤T≤10), which denotes the number of test cases.

For each test case, the first line of input contains single integer n,L,R (2≤n≤10^5,1≤L≤R≤10^18).

The second line contains n integers, the i-th integer denotes ai (1≤ai≤10^18).

 
Output
For each test case, print the number of ways to throw a stick.
 
Sample Input
2
2 1 3
1 1
4 3 10
1 1 2 4
 
Sample Output
2
5
 
题意:
 
已经由n跟长为ai的线段,现在给定区间[l,r],问这个区间里还有多少个数与那一堆数不会形成三角形;
 
思路:
 
把a数组排序,i<j;选a[i]和a[j],(a[i]-a[j],a[i]+a[j]);这是不能出现的数的区间,可知(a[i]-a[i-1],a[i]+a[i-1])是选a[i]这个数会出现的最大范围的区间;
所以就形成了n-1个区间[a[i]-a[i-1]+1,a[i]+a[i-1]-1],把这些区间扫一遍和并后求出覆盖了[l,r]中的多少个点,然后用[l,r]的点数减去就是答案了;
 
AC代码:
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=998244353;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e5+10;
const int maxn=1e3+10;
const double eps=1e-6; int n;
LL l,r,a[N];
struct node
{
LL l,r;
}po[N];
int cmp(node x,node y)
{
if(x.l==y.l)return x.r<y.r;
return x.l<y.l;
}
int vis[N];
int main()
{
int t;
read(t);
while(t--)
{
mst(vis,0);
read(n);read(l);read(r);
For(i,0,n-1)read(a[i]);
sort(a,a+n);
For(i,1,n-1)
{
po[i].r=a[i]+a[i-1]-1;
po[i].l=a[i]-a[i-1]+1;
}
sort(po+1,po+n,cmp);
LL len=0;
For(i,1,n-1)
{
if(i==n-1)continue;
if(po[i].r<po[i+1].l)continue;
else
{
po[i+1].l=po[i].l;
po[i+1].r=max(po[i].r,po[i+1].r);
vis[i]=1;
}
}
For(i,1,n)
{
if(!vis[i])
{
if(po[i].l>r||po[i].r<l||po[i].l>po[i].r)continue;
LL L=max(po[i].l,l),R=min(po[i].r,r);
len=len+(R-L+1);
}
}
cout<<r-l+1-len<<"\n";
} return 0;
}

  

hdu-5720 Wool(区间并+扫描线)的更多相关文章

  1. hdu 5720 Wool

    hdu 5720 问题描述 黎明时,Venus为Psyche定下了第二个任务.她要渡过河,收集对岸绵羊身上的金羊毛. 那些绵羊狂野不驯,所以Psyche一直往地上丢树枝来把它们吓走.地上现在有n n ...

  2. hdu 5720(贪心+区间合并)

    Wool Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Subm ...

  3. HDU 5726 GCD 区间GCD=k的个数

    GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  4. hdu 5720 BestCoder 2nd Anniversary Wool 推理+一维区间的并

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5720 题意:有n(n <= 105)个数 ,每个数小于等于 1018:问在给定的[L,R]区间中 ...

  5. HDU 1542 - Atlantis - [线段树+扫描线]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  6. hdu 3642 Get The Treasury(扫描线)

    pid=3642" style="">题目链接:hdu 3642 Get The Treasury 题目大意:三维坐标系,给定若干的长方体,问说有多少位置被覆盖3次 ...

  7. cf1108e 线段树区间更新+扫描线

    /* 有点像扫描线 思路:从左到右枚举每个点,枚举到点i时,把所有以i为起点的区间的影响删去 再加上以i-1为结尾的区间的影响 */ #include<bits/stdc++.h> usi ...

  8. HDU 6127 Hard challenge(扫描线)

    http://acm.hdu.edu.cn/showproblem.php?pid=6127 题意: 有n个点,每个点有一个$(x,y)$坐标和一个权值,任意两点之间都有连线,并且连线的权值为两个顶点 ...

  9. hdu 1556(线段树之扫描线)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 Color the ball Time Limit: 9000/3000 MS (Java/Ot ...

随机推荐

  1. 自定义Navigation按钮及Title

    导航栏自带的按钮,时常不能满足要求,所以深深需要进行各种定制. 写一个UINavigationItem的category // UINavigationItem+CB_ChangeButton.h 
 ...

  2. 2017-10-29-morning-清北模拟赛

    T1 遭遇 #include <algorithm> #include <cstdio> #include <cmath> inline void read(int ...

  3. GRYZY #13. 拼不出的数

    拼不出的数 lost.in/.out/.cpp [问题描述] 3 个元素的集合 {5, 1, 2} 的所有子集的和分别是 0, 1, 2, 3, 5, 6, 7, 8.发 现最小的不能由该集合子集拼出 ...

  4. [Bzoj3205][Apio2013]机器人(斯坦纳树)(bfs)

    3205: [Apio2013]机器人 Time Limit: 15 Sec  Memory Limit: 128 MBSubmit: 977  Solved: 230[Submit][Status] ...

  5. Go -- 并发编程的两种限速方法

    引子 golang提供了goroutine快速实现并发编程,在实际环境中,如果goroutine中的代码要消耗大量资源时(CPU.内存.带宽等),我们就需要对程序限速,以防止goroutine将资源耗 ...

  6. openfalcon的安装和使用

    蛮复杂的样子 根据官方文档指导,一步一步走起:https://book.open-falcon.org/zh_0_2/quick_install/prepare.html 单机安装的过程:单击安装会把 ...

  7. Android PullToRefresh 下拉刷新,上拉很多其它,支持ScrollView,ListView,可方便拓展GridView,WebView等

    在写着东西之前.从网上找到非常多这方面的源代码,可是基本没有找到惬意的.包含在GitHub上的比較有名的Android-PullToRefresh-master.思来想去还是自己写吧.当然当中借鉴了一 ...

  8. C++经典面试题解析

    1. // BlankTest.cpp : 定义控制台应用程序的入口点. //题目:将一个文件中的一组整数排序后输出到另一个文件中 #include "stdafx.h" #inc ...

  9. 通过Java反射做实体查询

    我们在使用hibernate的时候,查询的时候都会和实体中的一些字段相结合去查询,当然字段少了,还算是比较简单,当字段多了,就不那么容易了,所以就自己写了个方法,根据实体中的字段信息去查询,废话不多说 ...

  10. async & await 的前世今生(Updated)----代码demo

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...