4653

思路:

  线段树,指针滑动;

代码:

#include <bits/stdc++.h>
using namespace std;
#define maxn 1000005
#define maxm 200005
#define maxn_ maxn<<2
#define INF 0x7fffffff
struct TreeNodeType {
int l,r,dis,mid,flag;
};
struct TreeNodeType tree[maxn_];
struct QueryType {
int l,r,len;
bool operator <(const QueryType pos)const
{
return pos.len>len;
}
};
struct QueryType qu[maxn];
int n,m,bi[maxn_],cnt,size,ans=INF;
inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'')Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
}
void build(int now,int l,int r)
{
tree[now].l=l,tree[now].r=r;
if(l==r) return;tree[now].mid=l+r>>;
build(now<<,l,tree[now].mid);
build(now<<|,tree[now].mid+,r);
}
inline void pushdata(int now)
{
tree[now<<].dis+=tree[now].flag;
tree[now<<].flag+=tree[now].flag;
tree[now<<|].dis+=tree[now].flag;
tree[now<<|].flag+=tree[now].flag;
tree[now].flag=;
}
void updata(int now,int l,int r,int x)
{
if(tree[now].l>=l&&tree[now].r<=r)
{
tree[now].dis+=x,tree[now].flag+=x;
return;
}
if(tree[now].flag!=) pushdata(now);
if(l<=tree[now].mid) updata(now<<,l,r,x);
if(r>tree[now].mid) updata(now<<|,l,r,x);
tree[now].dis=max(tree[now<<].dis,tree[now<<|].dis);
}
int query(int now,int l,int r)
{
if(tree[now].l>=l&&tree[now].r<=r) return tree[now].dis;
if(tree[now].flag!=) pushdata(now);
int res=;
if(l<=tree[now].mid) res=max(res,query(now<<,l,r));
if(r>tree[now].mid) res=max(res,query(now<<|,l,r));
return res;
}
int main()
{
in(n),in(m);
for(int i=;i<=n;i++)
{
in(qu[i].l),in(qu[i].r);
qu[i].len=qu[i].r-qu[i].l+;
bi[++cnt]=qu[i].l,bi[++cnt]=qu[i].r;
}
sort(qu+,qu+n+);
sort(bi+,bi+cnt+);
size=unique(bi+,bi+cnt+)-bi-;
int now=,pos;build(,,size);
for(int i=;i<=n;i++)
{
qu[i].l=lower_bound(bi+,bi+size+,qu[i].l)-bi;
qu[i].r=lower_bound(bi+,bi+size+,qu[i].r)-bi;
updata(,qu[i].l,qu[i].r,);
while(tree[].dis>=m)
{
now++,pos=query(,qu[now].l,qu[now].r);
if(pos>=m) ans=min(qu[i].len-qu[now].len,ans);
updata(,qu[now].l,qu[now].r,-);
}
}
printf("%d\n",ans==INF?-:ans);
return ;
}

AC日记——NOI2016区间 bzoj 4653的更多相关文章

  1. AC日记——[Hnoi2017]影魔 bzoj 4826

    4826 思路: 主席树矩阵加减+单调栈预处理: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 200005 ...

  2. AC日记——[LNOI2014]LCA bzoj 3626

    3626 思路: 离线操作+树剖: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #defin ...

  3. AC日记——[ZJOI2012]网络 bzoj 2816

    2816 思路: 多个LCT: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 10005 #define l ...

  4. AC日记——[SCOI2009]游戏 bzoj 1025

    [SCOI2009]游戏 思路: 和为n的几个数最小公倍数有多少种. dp即可: 代码: #include <bits/stdc++.h> using namespace std; #de ...

  5. AC日记——[HNOI2014]世界树 bzoj 3572

    3572 思路: 虚树+乱搞: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 300005 #define ...

  6. AC日记——Rmq Problem bzoj 3339

    3339 思路: 恶心: 代码: #include <cstdio> #include <cstring> #include <iostream> #include ...

  7. AC日记——[HNOI2008]越狱 bzoj 1008

    1008 思路: 越狱情况=总情况-不越狱情况: 代码: #include <cstdio> #include <cstring> #include <iostream& ...

  8. AC日记——[FJOI2007]轮状病毒 bzoj 1002

    1002 思路: 打表找规律: dp[i]=dp[i-1]*3-dp[i-2]+2; 套个高精就a了: 代码: #include <cstdio> #include <cstring ...

  9. AC日记——[Ahoi2013]作业 bzoj 3236

    3236 思路: 莫队+树状数组维护: 代码: #include <cmath> #include <cstdio> #include <cstring> #inc ...

随机推荐

  1. Linux用户、用户组权限管理详解 --- 02

    2,用户.用户组管理操作详解: 2.1 adduser 添加用户: adduser [-u uid][-g group][-d home][-s shell] -u:直接给出userID        ...

  2. POJ_2112_Optimal Milking 这里有超级快的网络流板子

    Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among ...

  3. supervisor安装、配置和运行

    supervisor是python写的进程管理工具,supervisor能够批量对进程执行启动,停止,重启等操作,有效提高了运维效率.注意supervisor只能管理前台进程,supervisor会自 ...

  4. 题解【luoguP1351 NOIp提高组2014 联合权值】

    题目链接 题意:给定一个无根树,每个点有一个权值.若两个点 \(i,j\) 之间距离为\(2\),则有联合权值 \(w_i \times w_j\).求所有的联合权值的和与最大值 分析: 暴力求,每个 ...

  5. Leetcode 295. 数据流的中位数

    1.题目要求 中位数是有序列表中间的数.如果列表长度是偶数,中位数则是中间两个数的平均值. 例如, [2,3,4] 的中位数是 3 [2,3] 的中位数是 (2 + 3) / 2 = 2.5 设计一个 ...

  6. POJ 2391 二分+最大流

    Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19066   Accepted: 4 ...

  7. Intellij IDEA 使用jrebel运行spring-boot并实现自动编译进行热部署

    在使用jrebel运行spring-boot的时候,会发现一个很棘手的问题,就是项目不能自动编译,不能自动编译就不能实现热部署.(使用jar包方式运行的时候) 那么我们就要解决自动编译的问题,首先: ...

  8. python实现堆栈、队列

    一.利用python列表实现堆栈和队列 堆栈: 堆栈是一个后进先出的数据结构,其工作方式就像生活中常见到的直梯,先进去的人肯定是最后出. 我们可以设置一个类,用列表来存放栈中的元素的信息,利用列表的a ...

  9. SSH ERROR: Too many Authentication Failures

    来自: How to recover from "Too many Authentication Failures for user root" 其中一种可以解决的方式 eval ...

  10. Spring总结以及在面试中的一些问题(山东数漫江湖)

    1.谈谈你对spring IOC和DI的理解,它们有什么区别? IoC Inverse of Control 反转控制的概念,就是将原本在程序中手动创建UserService对象的控制权,交由Spri ...