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 ...
随机推荐
- git安装配置和使用
## 安装git服务器 ## 安装git sudo apt-get install git ## 建立git用户 sudo adduser git ## 修改git用户 * 设置不能登录 vim /e ...
- k好数 数位dp
问题描述 如果一个自然数N的K进制表示中任意的相邻的两位都不是相邻的数字,那么我们就说这个数是K好数.求L位K进制数中K好数的数目.例如K = 4,L = 2的时候,所有K好数为11.13.20.22 ...
- 如何在Linux上安装QQ
我一直无法解决Ubuntu QQ问题,而最近我重装ubuntu之后在网络上找到与QQ相关的内容,网上有大神开发出了新版的wineQQ,解决了我们对QQ的需求.经过尝试,完成了QQ安装 如图 安装的是 ...
- [LeetCode] 5. Longest Palindromic Substring ☆☆☆☆
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- MongoDB入门(7)- SpringDataMongoDB
入门 本文介绍如何应用SpringDataMongoDB操作实体和数据库,本文只介绍最基本的例子,复杂的例子在后面的文章中介绍. SpringDataMongoDB简介 SpringDataMongo ...
- 洛谷2115 [USACO14MAR]破坏Sabotage
https://www.luogu.org/problem/show?pid=2115 题目描述 Farmer John's arch-nemesis, Farmer Paul, has decide ...
- HDU 5868 Different Circle Permutation Burnside引理+矩阵快速幂+逆元
题意:有N个座位,人可以选座位,但选的座位不能相邻,且旋转不同构的坐法有几种.如4个座位有3种做法.\( 1≤N≤1000000000 (10^9) \). 题解:首先考虑座位不相邻的选法问题,如果不 ...
- 「模板」网络最大流 FF && EK && Dinic && SAP && ISAP
话不多说上代码. Ford-Fulkerson(FF) #include <algorithm> #include <climits> #include <cstdio& ...
- 随机生成数组函数+nth-element函数
这几天做了几道随机生成数组的题,且需要用nth-elemeng函数,并且都是北航出的多校题…… 首先我们先贴一下随机生成数组函数的代码: unsigned x = A, y = B, z = C; u ...
- TOJ 1005 Hero In Maze (深搜)
描述 500年前,Jesse是我国最卓越的剑客.他英俊潇洒,而且机智过人^_^. 突然有一天,Jesse心爱的公主被魔王困在了一个巨大的迷宫中.Jesse听说这个消息已经是两天以后了,他知道公主在迷宫 ...