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. js 常用的工具函数

    1 类型判断 isString (o) { //是否字符串 return Object.prototype.toString.call(o).slice(8, -1) === 'String' } i ...

  2. python操作三大主流数据库(8)python操作mongodb数据库②python使用pymongo操作mongodb的增删改查

    python操作mongodb数据库②python使用pymongo操作mongodb的增删改查 文档http://api.mongodb.com/python/current/api/index.h ...

  3. OpenSIPS 1.11.1安装记录

    说明:操作系统Centos 6.5  64位 安装步骤: 1.安装依赖包 : yum -y install gcc make gdb wget yum -y install flex bison nc ...

  4. List集合三种遍历方法

    List<String> list = new ArrayList<String>();list.add("aaa");list.add("bbb ...

  5. [C]va_list可变长参数的使用

    一.概述 运用标准C的头文件stdarg.h提供的宏可以实现函数的自定义传参个数: 二.语法 1.va_list是一个可变长参数类型,在使用可变长参数的函数中可以定义1个或多个va_list类型参数, ...

  6. VUE 数据请求和响应(axios)

    1. 概述 1.1 简介 axios是一个基于Promise(本机支持ES6 Promise实现) 的HTTP库,用于浏览器和 nodejs 的 HTTP 客户端.具有以下特征: 从浏览器中创建 XM ...

  7. Go语言中的byte和rune区别、对比

    Go语言中byte和rune实质上就是uint8和int32类型.byte用来强调数据是raw data,而不是数字:而rune用来表示Unicode的code point.参考规范: uint8 t ...

  8. swift 学习- 27 -- 访问控制

    // 访问控制 可以限定其源文件 或模块中的代码对你的代码的访问级别, 这个特性可以让我们隐藏代码的一些实现细节, 并且可以为其他人可以访问和使用的代码提供接口 // 你可以明确地给某个类型 (类, ...

  9. GZip使用

    class Program { static void Main(string[] args) { //Trace.Listeners.Clear(); //Trace.Listeners.Add(n ...

  10. css固定表头,表单内容可以滑动

    <html><head>    <meta charset="utf-8">    <title>Table</title&g ...