hdu-5720 Wool (interval and + scan line)

Source: Internet
Author: User

Topic Links:

Wool

Time limit:8000/4000 MS (java/others)

Memory limit:262144/262144 K (java/others)


problem DescriptionAt 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 is wild and tameless, so psyche keeps on throwing sticks to keep them away.

There isNSticks on the ground, the length of theI-th Stick isai .

If the new stick she throws forms a triangle with any of the sticks on the ground, the sheep would 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.

InputThe first line of input contains an integerT (1≤T≤) , which denotes the number of test cases.

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

The second line containsNIntegers, theI-th integer denotesai (1≤ai≤10^).

OutputFor each test case, print the number of ways to throw a stick.

Sample Input22 1 31 14 3 101 1 2 4

Sample Output25 Test instructions: Already by the line of N-length AI, now given the interval [l,r], ask how many numbers in this interval and that pile number will not form a triangle; Idea: Sorting a array,i<j; a[i] and A[j], (A[i]-a[j],a[i]+a[j]); This is the interval of the number that cannot be seen (A[i]-a[i-1],a[i]+a[i-1]) is the maximum range that the number will appear; So it formed a n-1 interval [a[i]-a[i-1]+1,a[i]+a[i-1]-1], sweep these intervals and then find out how many points covered [l,r], and then use [L,r] points minus is the answer; AC Code:
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<map>using namespacestd;#definefor (i,j,n) for (int i=j;i<=n;i++)#defineMST (SS,B) memset (ss,b,sizeof (ss));typedefLong Longll;template<classT>voidRead (t&num) {    CharCH;BOOLf=false;  for(Ch=getchar (); ch<'0'|| Ch>'9'; f= ch=='-', ch=GetChar ());  for(num=0; ch>='0'&&ch<='9'; num=num*Ten+ch-'0', ch=GetChar ()); F&& (num=-num);}intstk[ -], Tp;template<classT> Inlinevoidprint (T p) {if(!p) {Puts ("0");return; }  while(p) stk[++ TP] = p%Ten, p/=Ten;  while(TP) Putchar (stk[tp--] +'0'); Putchar ('\ n');}ConstLL mod=998244353;Const DoublePi=acos (-1.0);Const intinf=1e9;Const intn=1e5+Ten;Const intmaxn=1e3+Ten;Const Doubleeps=1e-6;intN; LL L,r,a[n];structnode{LL L,r;} Po[n];intCMP (node X,node y) {if(X.L==Y.L)returnx.r<Y.R; returnx.l<Y.L;}intVis[n];intMain () {intT;        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&GT;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 (interval and + scan line)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.