First, the interval is sorted by length and then the discretization endpoint ("Length" here refers to the actual length of the interval before discretization)
It then simulates a queue, which enters sequentially in a sorted order until a point is covered by M. Then out of line, until all points are covered less than m times
Modify and ask the number of overlays can be implemented with a segment tree
1 //c++11 Code2 3#include <cstdio>4#include <cstring>5#include <algorithm>6 7 Const intmaxn=500005;8 Const intinf=0x7f7f7f7f;9 Ten structRange One { A intLeft,right; - intLen; - voidAssignintLintR) {left=l; right=r; len=r-l;} the }; - - structSegtree - { + structNode - { + intVal=0;//Max-val A inttag=0; at intTotal () {returnval+tag;} - }; -Node node[maxn<<2]; - intsize; - int_val; - in voidUpdateintCurintRight ) - { toNode[cur].val = Std::max (node[cur+1].total (), node[right].total ()); + } - voidAddintLintRintval) the { *_val=Val; $__add (0, l,r+1,0, size);Panax Notoginseng } - void__add (intCurintRlintRrintNL,intNR) the { + if(Rl<=nl && rr>=NR) A { thenode[cur].tag+=_val; + return; - } $ intMid= (NL+NR) >>1; $ intright=cur+ (MID-NL) <<1); - if(Rl<mid) __add (cur+1, rl,rr,nl,mid); - if(rr>mid) __add (RIGHT,RL,RR,MID,NR); the Update (cur,right); - }Wuyi intAskall () {returnnode[0].total (); } the }; - Wu Range RG[MAXN]; - Segtree segt; About $ intn,m; - intbuf[maxn<<1]; - intValnum; - A voidinput () + { thescanf"%d%d",&n,&M); - inttl,tr; $ for(intI=0; i<n;i++) the { thescanf"%d%d",&tl,&tr); thebuf[i<<1]=tl; thebuf[(i<<1)+1]=tr; - rg[i].assign (TL,TR); in } the } the About voiddiscretize () the { theStd::sort (buf,buf+ (n<<1)); theValnum=std::unique (buf,buf+ (n<<1))-buf; + for(intI=0; i<n;i++) - { theRg[i].left=std::lower_bound (Buf,buf+valnum,rg[i].left)-buf;BayiRg[i].right=std::lower_bound (Buf,buf+valnum,rg[i].right)-buf; the } the } - - intSolve () the { the intres=inf; theAuto cmpidx=[] (Constrange& A,Constrange& B)BOOL{returna.len<B.len;}; theStd::sort (rg,rg+n,cmpidx); - discretize (); theSegt.size=Valnum; the inthead=-1, tail=-1; the while(1)94 { the while((++head) <n && Segt.askall () <M) theSegt.add (Rg[head].left,rg[head].right,1); the if(Segt.askall () <m) Break;98 Else--head; About while(Segt.askall () = =M) - {101++tail;102Res=std::min (res,rg[head].len-Rg[tail].len);103Segt.add (rg[tail].left,rg[tail].right,-1);104 } the }106 return(Res==inf)?-1: res;107 }108 109 intMain () the {111 input (); theprintf"%d\n", Solve ());113 return 0; the}
UOJ222 NOI2016 interval segment tree +fifo queue