AC日记——NOI2016区间 bzoj 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的更多相关文章
- AC日记——[Hnoi2017]影魔 bzoj 4826
4826 思路: 主席树矩阵加减+单调栈预处理: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 200005 ...
- AC日记——[LNOI2014]LCA bzoj 3626
3626 思路: 离线操作+树剖: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #defin ...
- AC日记——[ZJOI2012]网络 bzoj 2816
2816 思路: 多个LCT: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 10005 #define l ...
- AC日记——[SCOI2009]游戏 bzoj 1025
[SCOI2009]游戏 思路: 和为n的几个数最小公倍数有多少种. dp即可: 代码: #include <bits/stdc++.h> using namespace std; #de ...
- AC日记——[HNOI2014]世界树 bzoj 3572
3572 思路: 虚树+乱搞: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 300005 #define ...
- AC日记——Rmq Problem bzoj 3339
3339 思路: 恶心: 代码: #include <cstdio> #include <cstring> #include <iostream> #include ...
- AC日记——[HNOI2008]越狱 bzoj 1008
1008 思路: 越狱情况=总情况-不越狱情况: 代码: #include <cstdio> #include <cstring> #include <iostream& ...
- AC日记——[FJOI2007]轮状病毒 bzoj 1002
1002 思路: 打表找规律: dp[i]=dp[i-1]*3-dp[i-2]+2; 套个高精就a了: 代码: #include <cstdio> #include <cstring ...
- AC日记——[Ahoi2013]作业 bzoj 3236
3236 思路: 莫队+树状数组维护: 代码: #include <cmath> #include <cstdio> #include <cstring> #inc ...
随机推荐
- Centos版本6的使用教程
Centos版本6的使用教程 1.打开VMware workstation 12 PRO 创建新的虚拟机. 2.使用典型类型配置. 3.选择稍后安装操作系统,可以在后面进行安装. 4.选择安装的系统 ...
- 《时间序列分析及应用:R语言》读书笔记--第二章 基本概念
本章介绍时间序列中的基本概念.特别地,介绍随机过程.均值.方差.协方差函数.平稳过程和自相关函数等概念. 2.1时间序列与随机过程 关于随机过程的定义,本科上过相关课程,用的是<应用随机过程&g ...
- mybatis分页查询需要注意的问题
一般对mybatis的分页查询的关键代码就两行: #currentPage代表当前页,pageSize代表每页的行数 PageHelper.startPage(currentPage, pageSiz ...
- 【题解】我也不是B ifrog 1112 二分 倍增
题目传送门:http://ifrog.cc/acm/problem/1112 神奇的倍增二分,长见识了,在此做个记录,分享给大家. 懒得写题解了,直接转YJQ的:http://ifrog.cc/acm ...
- HDU 4258 斜率优化dp
Covered Walkway Time Limit: 30000/10000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- Qt中内存泄露和退出崩溃的问题 delete
Qt中帮程序员做了一些内存回收的事情,但正因为这些反而让对此不熟悉的人会屡屡犯错. 收录一篇不错的文章: 在C++中学习过程中,我们都知道: delete 和 new 必须 配对使用(一 一对应):d ...
- How to configue session timeout in Hive
This article explains how to configure the following settings in Hive:hive.server2.session.check.int ...
- 【BZOJ】4530: [Bjoi2014]大融合
[题意]给定n个点的树,从无到有加边,过程中动态询问当前图某条边两端连通点数的乘积,n<=10^5. [算法]线段树合并+并查集 (||LCT(LCT维护子树信息 LCT维护子树信息(+启发式合 ...
- 基于 Docker 的 Zabbix 微服务系统
zabbix 官网提供一个镜像 [ zabbix-appliance ], 可以直接拉起一个 zabbix-server. 但是数据库无法分离出来. 本实践使用 zabbix 官方提供的 Docker ...
- Java 对象排序详解
很难想象有Java开发人员不曾使用过Collection框架.在Collection框架中,主要使用的类是来自List接口中的ArrayList,以及来自Set接口的HashSet.TreeSet,我 ...