Topic link: Physical Education Lessons
general idea
given a length of nnn 's01 010 1 sequence, there are two operations: put the interval[ l , r ] [l, r][l,r ] all become1 11 , or put the interval[ l , r ] [l, r][l,r ] all become0 00.
After each operation, 1 1 in the output sequence1 number.
Problem solving ideas
➡️Weakened version recommendation⬅️
➡️Codori tree practice problem solving⬅️
segment tree
We consider that if nn is relatively small (at1 0 6 10^6106 ), we can directly use the line segment tree to violently maintain the interval information.
But the nn of this questionn is large (there are1 0 9 10^9109 ), we cannot maintain it with a static open-point segment tree by means of heap storage. But considering the operationmmm is relatively small, only3 ⋅ 1 0 5 3 10^53⋅105 , so the number of points involved is at most2 m 2m2 m . We canmaintainthe line segment tree with dynamic opening points
But we also have a more space-saving way - discretize all the points used . But after this discretization, the original discontinuous points will become continuous , and we need to insert some additional weighted points , so that the points remain discontinuous.
For example: If the endpoints used now are 3 and 7, they become 1 and 2 after discretization, but 3 and 7 are not next to each other (there are 4, 5, 6 in the middle). But after discretization, 1 and 2 are next to each other (the three points 4, 5, 6 are wrongly discarded).
Therefore, our correct approach is to insert a point 4 with a weight between the two points 3 and 7, and the weight is 3 (representing the three points 4, 5, and 6).
For the convenience of description, we may consider all points as a point pair ( x , y ) (x, y)(x,y ) , wherexxx is the position of the point,yyy is the point weight of this point, which means how many points this point represents in total.
After discretization of 3 and 7 as above, it should be ( 1 , 1 ) , ( 2 , 3 ) , ( 3 , 1 ) (1, 1), \ (2, 3), \ (3, 1)(1,1), (2,3), (3,1).
We found that by this discretization, at most double the number of additional points will be generated .
Therefore, the number of points maintained in the segment tree is at most 4 m 4 m4 m . And because the line segment tree needs to open4 44 times the space, so the space we need is16m 16m16m.
Consider how to maintain the sequence with a segment tree :
Because each time an interval is changed to 0 00 or1 11 , which is equivalent to an interval modification. We can save space byprefixes and arraysoutside the tree
For query the whole 01 010 1 sequence of1 1The number of 1 is the root node queryof the line segment tree.
AC code
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 1; i <= (n); ++i)
using namespace std;
typedef long long ll;
const int N = 3E5 + 10;
vector<int> v(1, -0x3f3f3f3f);
int find(int x) {
return lower_bound(v.begin(), v.end(), x) - v.begin(); }
int s[N * 4];
struct query {
int tp, l, r; }; vector<query> area;
struct node {
int l, r;
int val;
int lazy;
}t[N << 4];
void pushdown(node& op, int lazy) {
op.val = lazy * (s[op.r] - s[op.l - 1]);
op.lazy = lazy;
}
void pushdown(int x) {
if (t[x].lazy == -1) return;
pushdown(t[x << 1], t[x].lazy), pushdown(t[x << 1 | 1], t[x].lazy);
t[x].lazy = -1;
}
void pushup(int x) {
t[x].val = t[x << 1].val + t[x << 1 | 1].val; }
void build(int l, int r, int x = 1) {
t[x] = {
l, r, s[r] - s[l - 1], -1 };
if (l == r) return;
int mid = l + r >> 1;
build(l, mid, x << 1), build(mid + 1, r, x << 1 | 1);
}
void modify(int l, int r, int c, int x = 1) {
if (l <= t[x].l and r >= t[x].r) {
pushdown(t[x], c);
return;
}
pushdown(x);
int mid = t[x].l + t[x].r >> 1;
if (l <= mid) modify(l, r, c, x << 1);
if (r > mid) modify(l, r, c, x << 1 | 1);
pushup(x);
}
int main()
{
int n, m; cin >> n >> m;
rep(i, m) {
int l, r, tp; scanf("%d %d %d", &l, &r, &tp);
area.push_back({
tp, l, r });
v.push_back(l), v.push_back(r);
}
v.push_back(1), v.push_back(n); // 本题需要离散化整个[1, n]区间
sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());
int last = 0;
int len = v.size();
rep(i, len - 1) {
int l = last + 1, r = v[i] - 1;
if (l <= r) v.push_back(l);
last = v[i];
}
sort(v.begin(), v.end());
len = v.size(); // [1, len - 1]
for (int i = 1; i < len - 1; ++i) s[i] = v[i + 1] - v[i];
s[len - 1] = 1; // 右端点n
rep(i, len - 1) s[i] += s[i - 1];
assert(s[len - 1] == n);
build(1, len - 1);
for (auto& [tp, l, r] : area) {
l = find(l), r = find(r);
modify(l, r, tp - 1);
printf("%d\n", t[1].val);
}
return 0;
}