Minimal Segment Cover

嗯嗯嗯。。 打倍增的时候两个for写反了查了半天bug。。。

我们先把没用的线段去掉, 然后对每个线段都找到和它接触的r最大的作为下一个, 然后打倍增就好了。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);using namespace std;const int N = 5e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-8;
const double PI = acos(-1);template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}int n, m, c[N];
int prefix[N];
int nex[N][20];struct Line {int l, r;bool operator < (const Line &rhs) const {if(l == rhs.l) return r > rhs.r;return l < rhs.l;}
} a[N];int main() {scanf("%d%d", &n, &m);for(int i = 1; i <= n; i++) scanf("%d%d", &a[i].l, &a[i].r), a[i].r--;sort(a + 1, a + 1 + n);int nn = n; n = 1;for(int i = 2; i <= nn; i++)if(a[i].r > a[n].r) a[++n] = a[i];for(int i = 1; i <= n; i++) {c[a[i].l]++;c[a[i].r + 1]--;}if(!c[0]) prefix[0] = 1;for(int i = 1; i <= 500000; i++) {c[i] += c[i - 1];if(!c[i]) prefix[i] = 1;prefix[i] += prefix[i - 1];}for(int i = 1, j = 2; i <= n; i++) {j = max(i + 1, j);while(j <= n && a[j].l <= a[i].r + 1) j++;nex[i][0] = j - 1 == i ? 0 : j - 1;}for(int j = 1; j < 20; j++)for(int i = 1; i <= n; i++)nex[i][j] = nex[nex[i][j - 1]][j - 1];while(m--) {int x, y; scanf("%d%d", &x, &y);y--;int cnt = prefix[y];if(x) cnt -= prefix[x - 1];if(cnt) {puts("-1");} else {int p = lower_bound(a + 1, a + 1 + n, Line{x + 1, inf}) - a - 1;if(a[p].r >= y) {puts("1");} else {int ans = 0;for(int i = 19; i >= 0; i--) {if(nex[p][i] && a[nex[p][i]].r < y) {p = nex[p][i], ans += (1 << i);}}printf("%d\n", ans + 2);}}}return 0;
}

转载于:https://www.cnblogs.com/CJLHY/p/10982809.html

Codeforces 1175E Minimal Segment Cover 倍增相关推荐

  1. CodeForces - 1579G Minimal Coverage(dp)

    题目链接:点击查看 题目大意:给出 nnn 个长度不同的木棍.设第 i−1i-1i−1 次放置木棍后的终点为 xxx,那么第 iii 个木棍有且仅有两种放置方法: 放到 [x+1,x+a[i]][x+ ...

  2. CodeForces - 1535E Gold Transfer(树上倍增+交互)

    题目链接:点击查看 题目大意:给出一棵初始时只有一个点的树,每个点都有两个值:ai,cia_i,c_iai​,ci​,分别代表黄金的个数和单价.需要执行 mmm 次操作,每次操作分为两种类型: 1pi ...

  3. Codeforces 825E - Minimal Labels

    825E - Minimal Labels 题意 给出 m 条有向边,组成有向无环图,输出一个 1 到 n 组成的排列,每个数只能出现一次,表示每个点的标号.如果有边 \((u, v)\) 那么 \( ...

  4. CodeForces - 208E Blood Cousins(树上倍增+二分/树上启发式合并)

    题目链接:点击查看 题目大意:给出n棵树,再给出m个询问,每次询问给出两个整数u和k,先假设u在k层之上的祖先是p,问与u在同一层深度,并且公共祖先都是p的节点有多少个 题目分析:因为先要求出u在第k ...

  5. Codeforces 827D Best Edge Weight 倍增 + 并查集 || 倍增 + 压倍增标记 (看题解)

    Best Edge Weight 我们先找出一棵最小生成树, 对于非树边来说, 答案就是两点路径上的最大值 - 1, 这个直接倍增就能处理. 对于树边来说, 就是非树边的路径经过这条边的最小值 - 1 ...

  6. Codeforces 1278 D.Segment Tree(排序+set)

    题目链接:https://codeforces.com/contest/1278/problem/D 题目大意: 给定一堆线段[li,ri],每个线段的端点都不一样,如果两个线段相交,那么他们必须是有 ...

  7. CodeForces 797C Minimal string

    栈. 先处理一下后缀最小值. 对于每一个字符,如果不是后缀最小值,将栈顶小于当前后缀最小值的都弹出,然后压入当前字符. 如果是后缀最小值,将栈顶小于当前后缀最小值的都弹出,再输出该字符. #inclu ...

  8. Codeforces 797C Minimal string【贪心】

    题意: 给出了字符串s的内容,字符串t,u初始默认为空,允许做两种操作: 1.把s字符串第一个字符转移到t字符串最后. 2.把t字符串最后一个字符转移到u字符串最后. 最后要求s.t字符串都为空,问u ...

  9. CodeForces - 976F Minimal k-covering

    Description 给你一张左边 \(n_1\) 个点,右边 \(n_2\) 个点, \(m\) 条边的二分图.对于每一个 \(0\le k\le minDeg\) ,求选取哪些边可以使每个点的度 ...

最新文章

  1. 深入理解Oracle RAC 12c 笔记
  2. SIM PIN Lock
  3. python2中为什么在进行类定义时最好要加object
  4. Leetcode 25 K个一组翻转链表 (每日一题 20210719)
  5. Win7共享文件夹简单?这个共享问题可以难倒90%的人
  6. One Order CLOSING date修改后的执行原理
  7. JavaScript中OOP——面向对象中的继承/闭包
  8. 使用haskell来解线性方程
  9. webmin升级php,Centos linux下webmin安装及配置
  10. [转载] 多元线性回归 及其Python实现
  11. unity 找到的对象是动态加载,且两个不同的父物体上都有要的那个对象并有相同的名子
  12. 群晖系统硬盘损毁的修复
  13. 自定义形状按钮的实现
  14. 大量带BPM的跑步歌曲/跑步音乐下载
  15. vue怎么制作甘特图——dhtmlx-gantt
  16. 洛谷P1789 【Mc生存】插火把
  17. IDEA使用教程(三) 功能面板
  18. icon-font 图标字体的使用
  19. iOS修复字符串中 有\n 不换行的问题
  20. JavaScript 数据结构与算法(二)哈希表

热门文章

  1. Java 技术篇-使用poi开源jar包实现读取excel实例演示,poi-3.17.jar获取
  2. 正点原子串口函数分析
  3. CTFshow php特性 web126
  4. 多彩投网站动态爬取[python+selenium]
  5. Soj题目分类 python代码)
  6. sklearn中的fit_transform和transform以及什么时候使用
  7. 12.1简介Object类
  8. 地图画指定区域_聊城各地区公司变更跨区域迁移的流程及需要准备的材料有哪些...
  9. clickhouse mysql引擎_玩数据的,有必要搞搞这个大数据框架——ClickHouse
  10. 解决socket粘包的两种low版模式 os.popen()和struct模块