Parking Lot

线段树区间合并一下, 求当前要占的位置, 不包括两端点的写起来方便一点。

#include<bits/stdc++.h>
#define LL 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 ull unsigned long long using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, m, tot, pos[];
PII gg[]; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
struct info {
PII mx;
int cnt, L, R;
} a[N << ];
info operator + (const info& a, const info& b) {
info c;
c.cnt = a.cnt + b.cnt;
c.mx = max(a.mx, b.mx);
c.L = min(a.L, b.L); c.R = max(a.R, b.R);
if(a.cnt && b.cnt && a.R + < b.L) {
int p = (b.L + a.R) / ;
c.mx = max(c.mx, mk(min(b.L - p, p - a.R), -p));
}
return c;
} void build(int l, int r, int rt) {
if(l == r) {
a[rt].cnt = ;
a[rt].L = inf;
a[rt].R = -inf;
a[rt].mx = mk(, );
return;
}
int mid = l + r >> ;
build(lson); build(rson);
a[rt] = a[rt << ] + a[rt << | ];
} void update(int p, int op, int l, int r, int rt) {
if(l == r) {
if(op == ) {
a[rt].cnt = ;
a[rt].L = a[rt].R = p;
a[rt].mx = mk(, );
} else {
a[rt].cnt = ;
a[rt].L = inf;
a[rt].R = -inf;
a[rt].mx = mk(, );
}
return;
}
int mid = l + r >> ;
if(p <= mid) update(p, op, lson);
else update(p, op, rson);
a[rt] = a[rt << ] + a[rt << | ];
} int main() {
scanf("%d%d", &n, &m);
build(, n, );
while(m--) {
int op, x;
scanf("%d%d", &op, &x);
if(op == ) {
tot = ;
if(a[].mx.se) gg[tot++] = a[].mx;
if(a[].L != && a[].cnt) gg[tot++] = mk(a[].L - , -);
if(a[].R != n && a[].cnt) gg[tot++] = mk(n - a[].R, -n);
if(!tot) gg[tot++] = mk(inf, -);
sort(gg, gg + tot);
reverse(gg, gg + tot);
pos[x] = -gg[].se;
update(-gg[].se, , , n, );
printf("%d\n", -gg[].se); } else {
update(pos[x], -, , n, );
}
}
return ;
} /*
*/

Codeforces 219E Parking Lot 线段树的更多相关文章

  1. Buses and People CodeForces 160E 三维偏序+线段树

    Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...

  2. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  3. [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)

    [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...

  4. [Codeforces 1199D]Welfare State(线段树)

    [Codeforces 1199D]Welfare State(线段树) 题面 给出一个长度为n的序列,有q次操作,操作有2种 1.单点修改,把\(a_x\)修改成y 2.区间修改,把序列中值< ...

  5. [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)

    [Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...

  6. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  7. Codeforces 482B Interesting Array(线段树)

    题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...

  8. codeforces 383C Propagating tree 线段树

    http://codeforces.com/problemset/problem/383/C 题目就是说,  给一棵树,将一个节点的值+val, 那么它的子节点都会-val, 子节点的子节点+val. ...

  9. CodeForces 228D. Zigzag(线段树暴力)

    D. Zigzag time limit per test 3 seconds memory limit per test 256 megabytes input standard input out ...

随机推荐

  1. C/C++中容易造成内存溢出的函数

    1.strcpy() strcpy()函数将源字符串复制到缓冲区.没有指定要复制字符的具体数目.复制字符的数目直接取决于源字符串中的数目.如果源字符串碰巧来自用户输入,且没有专门限制其大小,则有可能会 ...

  2. WINDOWS资源编译器出错信息

    ACCELERATORS语句的type域应包含ASCⅡ值或VIRTKEY值.        BEGIN expected in table        BEGIN关键字应紧跟在ACCELERATOR ...

  3. Laravel 怎么使用资源控制器delete方法

    ### 在视图上,想删除某个数据,而控制器是使用了resources controller的 那么在删除数据的时候,还是有些需要注意的地方 ### 视图上: <a href="java ...

  4. MySQL分组查询,前3条数据

    SELECT tpn.news_id, tpn.title, tpn.news_group_id, tpn.send_date FROM t_pm_news tpn WHERE 3 > ( SE ...

  5. .NET NPOI操作Excel 让单元格的内容换行

    HSSFWorkbook workbook = new HSSFWorkbook(); // 工作簿 ISheet sheet = workbook.CreateSheet("会员列表&qu ...

  6. 【进阶3-4期】深度解析bind原理、使用场景及模拟实现(转)

    这是我在公众号(高级前端进阶)看到的文章,现在做笔记  https://github.com/yygmind/blog/issues/23 bind() bind() 方法会创建一个新函数,当这个新函 ...

  7. (五)STL算法

    .算法 1.算法通过迭代器来操作容器中的数据: 2.算法为模板函数: 二.迭代器与算法 1.根据移动能力,将迭代器分成了五类 2.使用萃取,输出各个容器中,迭代器的类别 3.其中istream, os ...

  8. 获取表单内的所有元素的值 表单格式化插件jquery.serializeJSON

    简单描述:一个form表单里有十几个input或者select,要获取到他们的值,我的做法一直都是$("#id").val();这样做本来没什么说的,但是如果有很多呢,就很麻烦,看 ...

  9. mybatis的批量删除操作

    需求描述:将符合某条件的几条记录删除 解决思路:对于多个id,可以使用sql关键词 in ,只要满足数据库的id在你的id集合或者list中,就删除,从而实现批量删除.循环delete方法是在是low ...

  10. 【python】升级pip后报错解决pkg_resources.DistributionNotFound: The 'pip==7.1.0' distribution was not found and is required by the application

    原本使用pip版本为7.1.0,后升级至9.0.1 之后使用pip list提示pkg_resources.DistributionNotFound: The 'pip==7.1.0' distrib ...