Codeforces 915E - Physical Education Lessons
动态开点只能开\(Ofast\)莽过去
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
const int N = 15000010;
int n, Q, root, cnt, L[N], R[N], seg[N], lazy[N];
void pushup(int u)
{
seg[u] = seg[L[u]] + seg[R[u]];
}
void pushdown(int u, int l, int r)
{
if (lazy[u] != -1) {
int mid = l + r >> 1;
if (!L[u]) L[u] = ++ cnt;
seg[L[u]] = lazy[u] * (mid - l + 1);
lazy[L[u]] = lazy[u];
if (!R[u]) R[u] = ++ cnt;
seg[R[u]] = lazy[u] * (r - mid);
lazy[R[u]] = lazy[u];
lazy[u] = -1;
}
}
void modify(int &x, int ll, int rr, int l, int r, int k)
{
if (!x) x = ++ cnt;
if (l <= ll && rr <= r) {
seg[x] = (rr - ll + 1) * k;
lazy[x] = k;
return;
}
pushdown(x, ll, rr);
int mid = ll + rr >> 1;
if (l <= mid) modify(L[x], ll, mid, l, r, k);
if (r > mid) modify(R[x], mid + 1, rr, l, r, k);
pushup(x);
}
int main() {
scanf("%d%d", &n, &Q);
memset(lazy, -1, sizeof lazy);
modify(root, 1, n, 1, n, 0);
while (Q -- ) {
int l, r, k;
scanf("%d%d%d", &l, &r, &k);
modify(root, 1, n, l, r, 2 - k);
printf("%d\n", n - seg[1]);
}
return 0;
}
珂朵莉树
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define IT set<node>::iterator
using namespace std;
typedef long long LL;
int n, m, sum;
struct node
{
int l, r;
mutable LL v;
node(int L, int R = -1, LL V = 0):l(L), r(R), v(V) {}
bool operator<(const node& o) const {
return l < o.l;
}
};
set<node> s;
IT split(int pos)
{
IT it = s.lower_bound(node(pos));
if (it != s.end() && it->l == pos)
return it;
-- it;
int L = it->l, R = it->r;
LL V = it->v;
s.erase(it);
s.insert(node(L, pos-1, V));
return s.insert(node(pos, R, V)).first;
}
void assign_val(int l, int r, LL val)
{
IT itr = split(r+1), itl = split(l);
IT it = itl;
for (; itl != itr; ++ itl)
sum -= itl->v * (itl->r - itl->l + 1);
s.erase(it, itr);
s.insert(node(l, r, val));
sum += val * (r - l + 1);
}
int main() {
IOS;
cin >> n >> m;
sum = n;
s.insert(node(1, n, 1));
for (int i = 1; i <= m; i ++ ) {
int l, r, op;
cin >> l >> r >> op;
if (op == 1) assign_val(l, r, 0);
else assign_val(l, r, 1);
cout << sum << endl;
}
return 0;
}