Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P2894 [USACO08FEB]Hotel G

P2894 [USACO08FEB]Hotel G

类似最大子段和的维护,只是所有权值都为 $1$ 。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 5e4 + 10;
struct Node
{
int l, r, ls, rs, s, mark;
}tr[N << 2];
int n, m;
void pushup (int x)
{
tr[x].s = max(max(tr[x << 1].s, tr[x << 1 | 1].s), tr[x << 1].rs + tr[x << 1 | 1].ls);
if (tr[x << 1].s == tr[x << 1].r - tr[x << 1].l + 1)
tr[x].ls = tr[x << 1].s + tr[x << 1 | 1].ls;
else
tr[x].ls = tr[x << 1].ls;
if (tr[x << 1 | 1].s == tr[x << 1 | 1].r - tr[x << 1 | 1].l + 1)
tr[x].rs = tr[x << 1 | 1].s + tr[x << 1].rs;
else
tr[x].rs = tr[x << 1 | 1].rs;
}
void pushdown (int x)
{
if (tr[x].l == tr[x].r)
return;
if (tr[x].mark == 1)
{
tr[x << 1].ls = tr[x << 1].rs = tr[x << 1].s = 0;
tr[x << 1 | 1].ls = tr[x << 1 | 1].rs = tr[x << 1 | 1].s = 0;
tr[x << 1].mark = tr[x << 1 | 1].mark = 1;
}
else if (tr[x].mark == 2)
{
tr[x << 1].ls = tr[x << 1].rs = tr[x << 1].s = tr[x << 1].r - tr[x << 1].l + 1;
tr[x << 1 | 1].ls = tr[x << 1 | 1].rs = tr[x << 1 | 1].s = tr[x << 1 | 1].r - tr[x << 1 | 1].l + 1;
tr[x << 1].mark = tr[x << 1 | 1].mark = 2;
}
tr[x].mark = 0;
}
void build (int x, int l, int r)
{
tr[x].l = l;
tr[x].r = r;
if (l == r)
{
tr[x].ls = tr[x].rs = tr[x].s = 1;
return;
}
int mid = l + r >> 1;
build(x << 1, l, mid);
build(x << 1 | 1, mid + 1, r);
pushup(x);
}
int query (int x, int k)
{
pushdown(x);
if (tr[x].l == tr[x].r)
return tr[x].l;
int mid = tr[x].l + tr[x].r >> 1;
if (tr[x << 1].s >= k)
return query(x << 1, k);
else if (tr[x << 1].rs + tr[x << 1 | 1].ls >= k)
return tr[x << 1].r - tr[x << 1].rs + 1;
else
return query(x << 1 | 1, k);
}
void modify (int x, int l, int r, int k)
{
if (tr[x].l >= l && tr[x].r <= r)
{
if (k == 1)
tr[x].ls = tr[x].rs = tr[x].s = 0;
else
tr[x].ls = tr[x].rs = tr[x].s = tr[x].r - tr[x].l + 1;
tr[x].mark = k;
return;
}
pushdown(x);
int mid = tr[x].l + tr[x].r >> 1;
if (l <= mid)
modify(x << 1, l, r, k);
if (r > mid)
modify(x << 1 | 1, l, r, k);
pushup(x);
}
int main ()
{
cin >> n >> m;
build(1, 1, n);
while (m--)
{
int op, a, b;
cin >> op;
if (op == 1)
{
cin >> a;
if (tr[1].s < a)
{
cout << 0 << endl;
continue;
}
int t = query(1, a);
cout << t << endl;
modify(1, t, t + a - 1, 1);
}
else
{
cin >> a >> b;
modify(1, a, a + b - 1, 2);
}
}
return 0;
}