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 ...
随机推荐
- 洛谷P3935 Calculating (莫比乌斯反演)
P3935 Calculating 题目描述 若xx分解质因数结果为\(x=p_1^{k_1}p_2^{k_2}\cdots p_n^{k_n},令f(x)=(k_1+1)(k_2+1)\cdots ...
- bzoj 4070 [Apio2015]雅加达的摩天楼 Dijkstra+建图
[Apio2015]雅加达的摩天楼 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 644 Solved: 238[Submit][Status][D ...
- 基于jQuery UI的调色板插件推荐colorpicker
1.下载地址 点击网页最下端Download,即可下载 2.使用方法 本插件是基于jQuery UI,所以应该先行下载jQuery UI,当然jQuery也必不可少 引入和初始化 引入js文件 & ...
- java获去json所有对象
public static void main(String args[]){ JSONObject json1=JSONObject.fromObject("{'username' : ' ...
- JSTL 入门: 表达式语言
转载自:http://www.ibm.com/developerworks/cn/java/j-jstl0211/ JavaServer Pages(JSP)是用于 J2EE 平台的标准表示层技术.J ...
- tcp/ip 学习-通过视频学习
视频下载地址:http://down.51cto.com/zt/5518/ http://www.icoolxue.com/album/show/328 每天可以拿两个番茄钟看视频,主要目的还是了解, ...
- windows下codeblocks报错undefined reference to `WSAStartup@8'|
Windows下C++Socket编程,调用WSAStartup函数报错:undefined reference to `WSAStartup@8'| 本人使用的是Codeblocks MinGW M ...
- Enterprise Architect 13 : 需求建模 自动命名并计数
如何给模型中的需求元素配置计数器以自动设置新创建元素的名称和别名: Configure -> Settings -> Auto Names and Counters 设置好后的效果图:
- J2EE保留小数问题
如果在前台页面,可以直接使用js的toFixed() 方法.number.toFixed(x) 可把 Number 四舍五入为指定小数位数的数字.参数x :必需.规定小数的位数,是 0 ~ 20 之 ...
- 【洛谷 P4116】 Qtree3 (树链剖分)
题目链接 树剖练手题,想复习下树剖. 第一次提交\(T\)成QQC 看我 ??? 看了数据范围的确挺恶心的,我的复杂度是\(O(Mlog^2N)\)的,数据范围有三段 For 1/3 of the t ...