博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
线段树区间合并(模板)
阅读量:5082 次
发布时间:2019-06-13

本文共 2353 字,大约阅读时间需要 7 分钟。

poj3667

#include
#include
#define lid id << 1#define rid id << 1 | 1using namespace std;const int mx = 50010;struct tree{ int l, r; int ls, rs, ms; int lazy;}tree[mx<<2];void build(int l, int r, int id){ tree[id].l = l; tree[id].r = r; tree[id].ls = tree[id].rs = tree[id].ms = r-l+1; tree[id].lazy = -1; if (l == r) return; int mid = (l+r) >> 1; build(l, mid, lid); build(mid+1, r, rid);}void pushdown(int id){ if (tree[id].lazy != -1){ tree[lid].lazy = tree[rid].lazy = tree[id].lazy; tree[lid].ls = tree[lid].rs = tree[lid].ms = tree[id].lazy ? 0 : tree[lid].r-tree[lid].l+1; tree[rid].ls = tree[rid].rs = tree[rid].ms = tree[id].lazy ? 0 : tree[rid].r-tree[rid].l+1; tree[id].lazy = -1; }}void pushup(int id){ tree[id].ls = tree[lid].ls; tree[id].rs = tree[rid].rs; int mid = (tree[id].l + tree[id].r) >> 1; if (tree[id].ls == mid-tree[id].l+1) tree[id].ls += tree[rid].ls; if (tree[id].rs == tree[id].r-mid) tree[id].rs += tree[lid].rs; tree[id].ms = max(max(tree[lid].ms, tree[rid].ms), tree[lid].rs+tree[rid].ls);}void upd(int l, int r, int id, bool x){ if (tree[id].l == l && tree[id].r == r){ tree[id].lazy = x; tree[id].ls = tree[id].rs = tree[id].ms = x ? 0 : r-l+1; return; } pushdown(id); int mid = (tree[id].l + tree[id].r) >> 1; if (r <= mid) upd(l, r, lid, x); else if (mid < l) upd(l, r, rid, x); else { upd(l, mid, lid, x); upd(mid+1, r, rid, x); } pushup(id);}int query(int l, int r, int id, int x){ if (l == r) return l; pushdown(id); int mid = (l+r) >> 1; if (tree[lid].ms >= x) return query(l, mid, lid, x); else if (tree[lid].rs + tree[rid].ls >= x) return mid-tree[lid].rs+1; return query(mid+1, r, rid, x);}int main(){ int n, m; scanf("%d%d", &n, &m); build(1, n, 1); while (m--){ int op, a, b; scanf("%d%d", &op, &a); if (op == 1){ if (tree[1].ms < a) printf("0\n"); else { b = query(1, n, 1, a); printf("%d\n", b); upd(b, b+a-1, 1, 1); } } else { scanf("%d", &b); upd(a, a+b-1, 1, 0); } } return 0;}
View Code

 

转载于:https://www.cnblogs.com/zhgyki/p/10349291.html

你可能感兴趣的文章
Java程序IP v6与IP v4的设置
查看>>
RUP(Rational Unified Process),统一软件开发过程
查看>>
数据库链路创建方法
查看>>
Enterprise Library - Data Access Application Block 6.0.1304
查看>>
重构代码 —— 函数即变量(Replace temp with Query)
查看>>
Bootstrap栅格学习
查看>>
程序员的数学
查看>>
聚合与组合
查看>>
jQuery如何获得select选中的值?input单选radio选中的值
查看>>
设计模式 之 享元模式
查看>>
如何理解汉诺塔
查看>>
洛谷 P2089 烤鸡【DFS递归/10重枚举】
查看>>
15 FFT及其框图实现
查看>>
Linux基本操作
查看>>
osg ifc ifccolumn
查看>>
C++ STL partial_sort
查看>>
3.0.35 platform 设备资源和数据
查看>>
centos redis 安装过程,解决办法
查看>>
IOS小技巧整理
查看>>
WebDriverExtensionsByC#
查看>>