Solution

线段树维护 sum 表示区间内预约个数, L 表示区间最左边的预约, R 表示区间最右边的预约。

$pushup$ 就是这样 :

 void up(int nd) {
sum[nd] = sum[lson] + sum[rson] - (R[lson] == L[rson] && R[lson]);
L[nd] = L[lson]; R[nd] = R[rson];
}

每次查询答案类似于$pushup$。

考虑把旧预约删去 :

若该次新预约 开始时间为$l$, 结束时间为$r$,是第$i$ 个预约, 则将 与该区间在右边 和 在左边相交的 旧预约的区间都用0覆盖。

然后再把新预约的区间都用 $i$覆盖。

$pushdown$ 和 $laz$ 数组也有些细节

Code

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define rd read()
using namespace std; const int N = 2e5 + ;
const int end = 1e5; struct met {
int L, R;
}in[N]; int read() {
int X = , p = ; char c = getchar();
for (; c > '' || c < ''; c = getchar())
if (c == '-') p = -;
for (; c >= '' && c <= ''; c = getchar())
X = X * + c - '';
return X * p;
} namespace SegT {
int sum[N << ], L[N << ], R[N << ], laz[N << ];
struct node {
int sum, L, R;
};
#define lson nd << 1
#define rson nd << 1 | 1
#define mid ((l + r) >> 1)
void up(int nd) {
sum[nd] = sum[lson] + sum[rson] - (R[lson] == L[rson] && R[lson]);
L[nd] = L[lson]; R[nd] = R[rson];
} void make(int nd, int x) {
sum[nd] = x ? : ; L[nd] = R[nd] = laz[nd] = x;
} void down(int nd) {
if (~laz[nd]) {
make(lson, laz[nd]); make(rson, laz[nd]);
laz[nd] = -;
}
} void modify(int cl, int cr, int c, int l, int r, int nd) {
if (cl <= l && r <= cr) {
make(nd, c); return;
}
down(nd);
if (mid >= cl)
modify(cl, cr, c, l, mid, lson);
if (mid < cr)
modify(cl, cr, c, mid + , r, rson);
up(nd);
} node query(int cl, int cr ,int l, int r, int nd) {
if (cl <= l && r <= cr) {
node tmp;
tmp.sum = sum[nd]; tmp.L = L[nd]; tmp.R = R[nd];
return tmp;
}
node tmp, ls, rs;
down(nd);
if (mid >= cl && !(mid < cr))
return query(cl, cr, l, mid, lson);
else if (!(mid >= cl) && mid < cr)
return query(cl, cr, mid + , r, rson);
ls = query(cl, cr, l, mid, lson);
rs = query(cl, cr, mid + , r, rson);
tmp.sum = ls.sum + rs.sum - (ls.R == rs.L && ls.R);
tmp.L = ls.L; tmp.R = rs.R;
return tmp;
}
}using namespace SegT; int main()
{
int n = rd;
memset(laz, -, sizeof(laz));
for (int i = ; i <= n; ++i) {
char ch = getchar();
while (ch > 'Z' || ch < 'A') ch = getchar();
if (ch == 'A') {
int l = rd, r = rd;
in[i].L = l; in[i].R = r;
node ans = query(l, r, , end, );
printf("%d\n", ans.sum);
if (ans.L) modify(in[ans.L].L, in[ans.L].R, , , end, );
if (ans.R) modify(in[ans.R].L, in[ans.R].R, , , end, );
modify(l, r, i, , end, );
}
else {
node ans = query(, end, , end, );
printf("%d\n", ans.sum);
}
}
}

Luogu2161 [SHOI2009]会场预约-线段树的更多相关文章

  1. P2161 [SHOI2009]会场预约[线段树/树状数组+二分/STL]

    题目描述 PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也就是说,前一个 ...

  2. luogu2161 [SHOI2009]会场预约

    题目大意 随着时间的推移这里有几个任务对应着一段区间.每次要将任务安到时间线上时,要把时间线上已有的与该任务对应区间有交集的区间对应的任务删去.求每次删去的区间个数,以及整个时间线上有几个任务.时间线 ...

  3. [LuoguP2161[ [SHOI2009]会场预约 (splay)

    题面 传送门:https://www.luogu.org/problemnew/show/P2161 Solution splay 的确有线段树/树状数组的做法,但我做的时候脑残没想到 我们可以考虑写 ...

  4. 2021.12.08 [SHOI2009]会场预约(平衡树游码表)

    2021.12.08 [SHOI2009]会场预约(平衡树游码表) https://www.luogu.com.cn/problem/P2161 题意: 你需要维护一个 在数轴上的线段 的集合 \(S ...

  5. 【题解】P2161[SHOI2009]会场预约(set)

    [题解][P2161 SHOI2009]会场预约 题目很像[[题解]APIO2009]会议中心 \(set\)大法好啊! 然后我们有个小\(trick\)(炒鸡帅),就是如何优雅地判断线段交? str ...

  6. P2161 [SHOI2009]会场预约 (线段树:线段树上的不重复覆盖数)

    题目描述 PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也就是说,前一个 ...

  7. BZOJ2028:[SHOI2009]会场预约(线段树版)

    浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://www.lydsy.com/JudgeOnline/prob ...

  8. P2161 [SHOI2009]会场预约

    题目描述 PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也就是说,前一个 ...

  9. BZOJ2028: [SHOI2009]会场预约(set)

    Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 425  Solved: 213[Submit][Status][Discuss] Description ...

随机推荐

  1. Redis原子计数器incr

    一.前言在一些对高并发请求有限制的系统或者功能里,比如说秒杀活动,或者一些网站返回的当前用户过多,请稍后尝试.这些都是通过对同一时刻请求数量进行了限制,一般用作对后台系统的保护,防止系统因为过大的流量 ...

  2. git库初次下载

    1.右键Git Batch Here==>输入 git config --list 确认2.再次输入ssh-keygen -t rsa -C “修改后的邮箱” 3.回车多次 找到 生成序列目录 ...

  3. Struts2学习资料

    Struts2入门示例教程 http://blog.csdn.net/wwwgeyang777/article/details/19078545     Struts2工作原理 http://blog ...

  4. zabbix_server.conf 详解

    # This is a configuration file for Zabbix server daemon # To get more information about Zabbix, visi ...

  5. STL-queue和循环队列基本操作的实现

    2018-11-13-17:53:44 1.可增长循环队列 队列是一种特殊的线性表,是一种先进先出(FIFO)的数据结构.它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入 ...

  6. 浅谈Java代理一:JDK动态代理-Proxy.newProxyInstance

    浅谈Java代理一:JDK动态代理-Proxy.newProxyInstance java.lang.reflect.Proxy:该类用于动态生成代理类,只需传入目标接口.目标接口的类加载器以及Inv ...

  7. Jedis cluster命令执行流程剖析

    Jedis cluster命令执行流程剖析 在Redis Cluster集群模式下,由于key分布在各个节点上,会造成无法直接实现mget.sInter等功能.因此,无论我们使用什么客户端来操作Red ...

  8. 844. Backspace String Compare判断删除后的结果是否相等

    [抄题]: Given two strings S and T, return if they are equal when both are typed into empty text editor ...

  9. swift - VC添加手势返回

    1.需要添加手势的界面 (1)addBackGesture() (2) 设置手势返回代理 // MARK: - 添加返回手势 extension JYRTSShopDetialConteoller:U ...

  10. Git下的.DS_Store文件

    .DS_Store 是什么 使用 Mac 的用户可能会注意到,系统经常会自动在每个目录生成一个隐藏的 .DS_Store 文件..DS_Store(英文全称 Desktop Services Stor ...