CC BY 4.0 (除特别声明或转载文章外)
如果这篇博客帮助到你,可以请我喝一杯咖啡~
题目链接 珂朵莉树是一个可以维护区间 x 次方和查询的高效数据结构,原理十分暴力。
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
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
struct ChthollyTree : map<int, ll>
{
iterator split(int pos)
{
iterator it = lower_bound(pos);
if (it->first == pos)
return it;
iterator jt = it--;
return insert(jt, make_pair(pos, it->second));
}
} t;
int n, m;
int main()
{
scanf("%d%d", &n, &m);
t[1] = 1;
t[n + 1] = 0;
for (int op, l, r, x, y; m--;)
{
scanf("%d%d%d", &op, &l, &r);
ChthollyTree::iterator b = t.split(l), e = t.split(r + 1), it;
if (op == 0)
t.erase(b, e), t[l] = 0;
else if (op == 1)
{
int res = 0;
it = b;
do
{
int pos = it->first, val = it->second;
++it;
if (val)
res += it->first - pos;
} while (it != e);
t.erase(b, e), t[l] = 0;
scanf("%d%d", &x, &y);
if (res)
{
b = t.split(x), e = t.split(y + 1);
if (res >= y - x + 1)
t.erase(b, e), t[x] = 1;
else
{
it = b;
do
{
int pos = it->first, val = it->second;
++it;
if (!val)
{
t[pos] = 1;
if (res >= it->first - pos)
res -= it->first - pos;
else
res = t[pos + res] = 0;
}
} while (res && it != e);
}
}
}
else
{
int res = 0, now = 0;
it = b;
do
{
int pos = it->first, val = it->second;
++it;
if (!val)
res += it->first - pos;
else
now = max(now, res), res = 0;
} while (it != e);
printf("%d\n", max(res, now));
}
}
}