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 ...
随机推荐
- postgresql pgagent 的安装及使用
pgagent 作为postgresql的一个任务调度代理,在postgresql 9.0 以前 是附带在pgadmin 包下面的,只是默认不安装,9.0之后作为了一个单独是的安装包.所以要使用pga ...
- 有向图的强联通分量 Tarjan算法模板
//白书 321页 #include<iostream> #include<cstdio> #include<cstring> #include<vector ...
- 轮廓问题/Outline Problem
--------------------------------------------------- //已发布改进后的轮廓问题算法:http://www.cnblogs.com/andyzeng/ ...
- UVA 1640 The Counting Problem
https://vjudge.net/problem/UVA-1640 题意:统计区间[l,r]中0——9的出现次数 数位DP 注意删除前导0 #include<cmath> #inclu ...
- Golang向Templates 插入对象的值
Go对象可以插入到template中,然后把对象的值表现在template中,你可以一层层的分解这个对象,去找他的子字段,当前对象用'.'来表示,所以当当前对象是一个string的时候,你可以用{{. ...
- UOJ#179. 线性规划[模板]
传送门 http://uoj.ac/problem/179 震惊,博主竟然还不会线性规划! 单纯形实在学不会啊……背个板子当黑盒用…… 学(chao)了NanoApe dalao的板子 #includ ...
- 【BZOJ】1609: [Usaco2008 Feb]Eating Together麻烦的聚餐
[算法]动态规划 [题解]DP有个特点(递推的特点),就是记录所有可能状态然后按顺序转移. 最优化问题中DP往往占据重要地位. f[i][j]表示前i头奶牛,第i头改为号码j的最小改动数字,这样每头奶 ...
- 【51NOD-0】1049 最大子段和
[算法]DP [题解]开long long…… #include<cstdio> #include<algorithm> #include<cstring> usi ...
- Python 用ctypes观察Python对象的内存结构 -- (转)
!!!强烈推荐的好文章!!! 对象的两个基本属性 Python所有对象结构体中的头两个字段都是相同的: refcnt:对象的引用次数,若引用次数为0则表示此对象可以被垃圾回收了. typeid:指向描 ...
- bootstrap-select属性
# 参考:https://blog.csdn.net/zxl_langya/article/details/79247307 # bootstrap-select属性: <select mult ...