Code:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 200000+5;
int n,m,k;
struct Segment_Tree{
int lazy,left,right,maxv;
}Seg[N<<2];
inline void push_down(int o,int l,int r)
{
if(Seg[o].lazy == -1 || l==r)return;
int mid = (l+r)>>1;
int lz = Seg[o].lazy, ls=o<<1, rs=(o<<1)|1;
Seg[ls].lazy = Seg[rs].lazy = lz;
Seg[ls].maxv = lz==0 ? mid-l+1: 0;
Seg[rs].maxv = lz==0 ? r-mid: 0;
Seg[ls].left = Seg[ls].right = Seg[ls].maxv;
Seg[rs].left = Seg[rs].right = Seg[rs].maxv;
Seg[o].lazy = -1;
}
inline void update_seg(int o,int l,int r)
{
int mid = (l+r)>>1, ls=o<<1, rs=(o<<1)|1;
Seg[o].maxv = max(Seg[ls].maxv, Seg[rs].maxv);
Seg[o].maxv = max(Seg[o].maxv, Seg[ls].right+Seg[rs].left);
Seg[o].left = Seg[ls].maxv == mid-l+1 ? Seg[ls].maxv+Seg[rs].left : Seg[ls].left;
Seg[o].right = Seg[rs].maxv == r-mid ? Seg[rs].maxv+Seg[ls].right: Seg[rs].right;
}
void build_Tree(int l,int r,int o){
if(l>r)return;
if(l==r)
{
Seg[o].left = Seg[o].right = Seg[o].maxv = 1;
Seg[o].lazy = -1;
return;
}
int mid = (l+r)>>1, ls=o<<1, rs=(o<<1)|1;
build_Tree(l,mid,ls);
build_Tree(mid+1,r,rs);
update_seg(o,l,r);
Seg[o].lazy = -1;
}
void update(int L,int R,int l,int r,int val,int o){
if(L>R)return;
if(L>=l&&R<=r)
{
Seg[o].lazy = val;
Seg[o].maxv = (val==0)?R-L+1: 0;
Seg[o].left = Seg[o].right = Seg[o].maxv;
return;
}
push_down(o,L,R);
int mid = (L+R)>>1,ls=o<<1, rs=(o<<1)|1;
if(l<=mid) update(L,mid,l,r,val,ls);
if(r>mid) update(mid+1,R,l,r,val,rs);
update_seg(o,L,R);
}
int query(int o,int l,int r){
int mid=(l+r)>>1, ls=o<<1, rs=(o<<1)|1, ans;
push_down(o,l,r);
if(Seg[o].left >= k)
ans = l;
else if(Seg[ls].maxv >=k )
ans = query(ls,l,mid);
else if(Seg[ls].right+Seg[rs].left >= k)
ans = mid+1-Seg[ls].right;
else
ans = query( rs ,mid+1,r);
update_seg(o,l,r);
return ans;
}
inline int solve_1(){
if(Seg[1].maxv < k)return 0;
return query(1,1,n);
}
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
scanf("%d%d",&n,&m);
build_Tree(1,n,1);
while(m--)
{
int op,x,y;
scanf("%d%d",&op,&x);
if(op==1)
{
k=x;
int ans=solve_1();
printf("%d\n",ans);
if(ans!=0)update(1,n,ans,ans+k-1,1,1);
}
if(op==2)
{
scanf("%d",&y);
update(1,n,x,x+y-1,0,1);
}
}
return 0;
}

  

洛谷P2894 [USACO08FEB]酒店Hotel_区间更新_区间查询的更多相关文章

  1. 洛谷 P2894 [USACO08FEB]酒店Hotel-线段树区间合并(判断找位置,不需要维护端点)+分治

    P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...

  2. 洛谷P2894 [USACO08FEB]酒店Hotel

    P2894 [USACO08FEB]酒店Hotel https://www.luogu.org/problem/show?pid=2894 题目描述 The cows are journeying n ...

  3. 洛谷 P2894 [USACO08FEB]酒店Hotel 解题报告

    P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...

  4. 区间连续长度的线段树——洛谷P2894 [USACO08FEB]酒店Hotel

    https://www.luogu.org/problem/P2894 #include<cstdio> #include<iostream> using namespace ...

  5. 洛谷P2894 [USACO08FEB]酒店Hotel [线段树]

    题目传送门 酒店 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and ...

  6. 洛谷P2894[USACO08FEB]酒店Hotel(线段树)

    问题描述 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N & ...

  7. 洛谷 P2894 [USACO08FEB]酒店

    题目描述 用线段树维护三个值:区间最长空位长度,从左端点可以延伸的最长空位长度,从右端点可以延伸的最长空位长度. #include<complex> #include<cstdio& ...

  8. 洛谷 P2894 [USACO08FEB]酒店Hotel

    题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...

  9. 线段树【洛谷P2894】 [USACO08FEB]酒店Hotel

    P2894 [USACO08FEB]酒店Hotel 参考样例,第一行输入n,m ,n代表有n个房间,编号为1---n,开始都为空房,m表示以下有m行操作,以下 每行先输入一个数 i ,表示一种操作: ...

随机推荐

  1. "AssertionError: View function mapping is overwriting an existing endpoint function"如何解决

    使用Flask定义URL的时候,如果出现"AssertionError: View function mapping is overwriting an existing endpoint ...

  2. 【hihocoder 1287】 数论一·Miller-Rabin质数测试

    [题目链接]:http://hihocoder.com/problemset/problem/1287 [题意] [题解] 取的底数必须是小于等于n-1的; 那12个数字能通过2^64以内的所有数字; ...

  3. mysql优化sql语句

    mysql优化sql语句   常见误区   www.2cto.com   误区1:   count(1)和count(primary_key) 优于 count(*)   很多人为了统计记录条数,就使 ...

  4. SCU - 4117 - Discover

    先上题目: D - Discover Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit St ...

  5. 手动编译java的package问题,及演示继承的基本实现

    不用IDE,而直接用命令编译JAVA包,仔细看了下,作一个记录. 以下的URL值得收藏. http://www.aiuxian.com/article/p-2115485.html http://ww ...

  6. 使用nginx+lua脚本读写redis缓存

    配置 新建spring boot项目增加redis配置 <dependency> <groupId>org.springframework.boot</groupId&g ...

  7. SPOJ 1771&&DLX精确覆盖,重复覆盖

    DLX的题,做过这题才算是会吧. 这道题转化成了精确覆盖模型来做,一开始,只是单纯的要覆盖完行列和斜线,WA. 后来醒悟了,不能这样,只要覆盖全部行或列即可.虽然如此,但某些细节地方很关键不能考虑到. ...

  8. POJ 3608

    1.计算P上y坐标值最小的顶点(称为 yminP )和Q上y坐标值最大的顶点(称为 ymaxQ). 2.为多边形在 yminP 和 ymaxQ 处构造两条切线 LP 和 LQ 使得他们对应的多边形位于 ...

  9. visual studio 开发工具SVN集成工具,测试可用,成功集成

    使用说明:https://www.xiazaiba.com/html/24864.html 下载地址:点击下载集成插件

  10. 解题报告 之 HDU5303 Delicious Apples

    解题报告 之 HDU5303 Delicious Apples Description There are n apple trees planted along a cyclic road, whi ...