Educational Codeforces Round 36 (Rated for Div. 2) E. Physical Education Lessons
大意:初始n天都是工作日,给你q个操作,分别是把l,r变成工作日或者非工作日。让你求出工作日的天数。
思路:n很大,但操作数不大,所以我们要动态开点线段数来解决这个问题,算是裸题了。
记录每个点和左右儿子节点的编号,如果是新的点就新开一个节点。其他的操作都是根普通线段树一样拉
细节见代码:
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define LL long long
#define SZ(X) X.size()
#define pii pair<int,int>
#define ALL(X) X.begin(),X.end()
using namespace std;
LL gcd(LL a, LL b) {return b ? gcd(b, a % b) : a;}
LL lcm(LL a, LL b) {return a / gcd(a, b) * b;}
LL powmod(LL a, LL b, LL MOD) {LL ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
const int N = 1e7 + 5e6;
int n, q, cnt = 1;
int ls[N], rs[N], t[N];
int s[N];
#define mid (l+r>>1)
void push_down(int o, int l, int r) {
if (!ls[o])ls[o] = ++cnt;
if (!rs[o])rs[o] = ++cnt;
t[ls[o]] = t[o];
t[rs[o]] = t[o];
if (t[o] == 2) {
s[ls[o]] = mid - l + 1;
s[rs[o]] = r - mid;
s[o] = 0;
} else if (t[o] == 1) {
s[ls[o]] = s[rs[o]] = 0;
s[o] = 0;
}
t[o] = 0;
return ;
}
void up(int &o, int l, int r, int x, int y, int Q) {
if (!o) o = ++cnt;
if (x <= l && y >= r) {
t[o] = Q;
if (Q == 2) {
s[o] = r - l + 1;
} else s[o] = 0;
return ;
}
if (t[o])push_down(o, l, r);
if (x <= mid && t[ls[o]] != Q)up(ls[o], l, mid, x, y, Q);
if (y > mid && t[rs[o]] != Q)up(rs[o], mid + 1, r, x, y, Q);
if (t[ls[o]] == t[rs[o]])t[o] = t[ls[o]];
else t[o] = 0;
s[o] = s[ls[o]] + s[rs[o]];
return ;
}
int main() {
// cout << (double)log2(1000000000) * 300000 << endl;
scanf("%d%d", &n, &q);
t[1] = 1;
int root = 1;
up(root, 1, n, 1, n, 2);
for (int i = 1; i <= q; i++) {
int l, r, p;
scanf("%d%d%d", &l, &r, &p);
if (p == 1) {
up(root, 1, n, l, r, 1);
} else {
up(root, 1, n, l, r, 2);
}
printf("%d\n", s[1] );
}
return 0;
}