【HDU 4343】Interval query(倍增)
BUPT2017 wintertraining(15) #8D
题意
给你x轴上的N个线段,M次查询,每次问你[l,r]区间里最多有多少个不相交的线段。(0<N, M<=100000)
限时15000 MS
题解
如果不看限时,当作是1000MS的话= =,那么可以用倍增来做。
先按右端点排序。
可以去掉一下包含了其它区间的区间,可以优化一点点。
用 f[i][j] 表示 i 节点下 \(2^n\) 个不相交的线段下标。
预处理出 f 数组。
查询的时候,左端点用二分,然后右端点用倍增来找。
实际上不用倍增,也可以卡时间过。
代码
780ms
#include <cstdio>
#include <algorithm>
#include <cstring>
#define N 100005
using namespace std;
struct Seg{
int s,e;
bool operator < (const Seg &b) const{
return e<b.e||e==b.e&&s>b.s;
}
}seg[N];
int n,m;
int f[N][32];
int main(){
while(~scanf("%d %d", &n, &m)){
for(int i=0;i<n;++i)
scanf("%d %d", &seg[i].s, &seg[i].e);
memset(f,-1,sizeof f);
sort(seg,seg+n);
int nn=0;
for(int i=1;i<n;++i)
if(seg[nn].s<seg[i].s)
seg[++nn]=seg[i];
++nn;
for(int i=0;i<nn;++i){
f[i][0]=i+1;
while(f[i][0]<nn&&seg[f[i][0]].s<seg[i].e)++f[i][0];
if(f[i][0]==nn)f[i][0]=-1;
}
for(int j=0;j<30;++j)
for(int i=0;i<nn;++i)
if(f[i][j]!=-1)
f[i][j+1]=f[f[i][j]][j];
for(int i=1,s,e;i<=m;++i){
scanf("%d %d", &s, &e);
int l=0, r=nn-1;
while(l<r){
int m=l+r>>1;
if(seg[m].s<s)
l=m+1;
else
r=m;
}
if(seg[l].e>e||seg[l].s<s)puts("0");
else{
int ans=1;
for(int j=29;j>=0;--j){
if(f[r][j]!=-1&&seg[f[r][j]].e<=e){
r=f[r][j];
ans+=(1<<j);
}
}
printf("%d\n", ans);
}
}
}
return 0;
}
1045ms
#include <cstdio>
#include <algorithm>
#include <cstring>
#define N 100005
using namespace std;
struct Seg {
int s, e;
bool operator < (const Seg &b) const {
return e < b.e || e == b.e && s > b.s;
}
} seg[N];
int n, m;
int main() {
while(~scanf("%d %d", &n, &m)) {
for(int i = 0; i < n; ++i)
scanf("%d %d", &seg[i].s, &seg[i].e);
sort(seg, seg + n);
int nn = 0;
for(int i = 1; i < n; ++i)
if(seg[nn].s < seg[i].s)
seg[++nn] = seg[i];
++nn;
for(int i = 1, s, e; i <= m; ++i) {
scanf("%d %d", &s, &e);
int l = 0, r = nn - 1;
while(l < r) {
int m = l + r >> 1;
if(seg[m].s < s)
l = m + 1;
else
r = m;
}
if(seg[l].e > e || seg[l].s < s)puts("0");
else {
int ans = 1;
for(int i = l, j = l + 1; j < nn && seg[j].e <= e; ++j) {
if(seg[i].e <= seg[j].s) {
i = j;
++ans;
}
}
printf("%d\n", ans);
}
}
}
return 0;
}
【HDU 4343】Interval query(倍增)的更多相关文章
- HDU 4343 Interval query(贪心 + 倍增)
题目链接 2012多校5 Problem D 题意 给定$n$个区间,数字范围在$[0, 10^{9}]$之间,保证左端点严格大于右端点. 然后有$m$个询问,每个询问也为一个区间,数字范围在$[ ...
- 【HDOJ】4343 Interval query
最大不相交集合的数量.思路是dp[i][j]表示已经有i个不相交集合下一个不相交集合的最右边界.离散化后,通过贪心解. /* 4343 */ #include <iostream> #in ...
- HDU 4343 D - Interval query 二分贪心
D - Interval queryTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...
- HDU 4343 贪心
D - Interval queryTime Limit: 1.5 Sec Memory Limit: 256 MB Description This is a very simple questio ...
- 刷题总结——Interval query(hdu4343倍增+贪心)
题目: Problem Description This is a very simple question. There are N intervals in number axis, and M ...
- HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- hdu 5726 GCD 暴力倍增rmq
GCD/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5726 Description Give you a sequence ...
- HDU4343Interval query 倍增
去博客园看该题解 题意 给定n个区间[a,b),都是左闭右开,有m次询问,每次询问你最多可以从n个区间中选出多少[L,R]的子区间,使得他们互不相交. n,m<=10^5. 区间下标<=1 ...
- HDU 6107 Typesetting (倍增)
Typesetting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
随机推荐
- main函数如何调用文件外的函数
- Karen and Coffee CodeForces - 816B (差分数组+预处理前缀和)
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- 用python实现一个回文数
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...
- 【问题解决方案】从 Anaconda Prompt 或 Jupyter Notebook 终端进入Python后重新退出到命令状态
从 Anaconda Prompt 或 Jupyter Notebook 终端进入Python后重新退出到命令状态 退出Python:exit() 或者 Ctrl+z 例子一枚 默认打开的是3.7,需 ...
- ocrosoft 程序设计提高期末复习问题M 递归求猴子吃桃
http://acm.ocrosoft.com/problem.php?cid=1172&pid=12 题目描述 猴子吃桃问题.猴子第1天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个. ...
- centos 7 aufs
Docker storage drivers | Docker Documentationhttps://docs.docker.com/storage/storagedriver/select-st ...
- MySQL之慢查询日志和通用查询
MySQL中的日志包括:错误日志.二进制日志.通用查询日志.慢查询日志等等.这里主要介绍下比较常用的两个功能:通用查询日志和慢查询日志. 1.通用查询日志:记录建立的客户端连接和执行的语句. 2.慢查 ...
- python文件封装成*.exe文件(单文件和多文件)
环境:win10 64位 python3.7 单*.py文件打包Python GUI:程序打包为exe 一.安装Pyinstaller,命令pip install Pyinstaller,(大写的P ...
- java 获取下一个字母(传大写返回大写,传小写返回小写)
public static String getNextUpEn(String en){ char lastE = 'a'; char st = en.toCharArray()[0]; if(Cha ...
- CMake--变量
1.一般变量 1)CMake变量引用的方式 使用${}进行变量的引用.例如: ${PROJECT_NAME} #返回项目名称 在 IF 等语句中,是直接使用变量名而不通过${}取值. 2)cmake自 ...