codevs 1191 树轴染色 线段树区间定值,求和

时间:2023-12-17 13:50:26

codevs 1191 树轴染色

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://www.codevs.cn/problem/1191/

Description

在一条数轴上有N个点,分别是1~N。一开始所有的点都被染成黑色。接着
我们进行M次操作,第i次操作将[Li,Ri]这些点染成白色。请输出每个操作执行后
剩余黑色点的个数。

Input

输入一行为N和M。下面M行每行两个数Li、Ri

Output

输出M行,为每次操作后剩余黑色点的个数。

Sample Input

10 3
3 3
5 7
2 8

Sample Output

9
6
3

HINT

数据限制
对30%的数据有1<=N<=2000,1<=M<=2000
对100%数据有1<=Li<=Ri<=N<=200000,1<=M<=200000

题意

题解:

区间更新,把黑色当成1,白色当成0,然后搞一搞就好了

代码:

#include <stdio.h>
#include <string.h> const int MAXN = ;
int sum[MAXN<<];
int lazy[MAXN<<]; void pushup(int rt)
{
sum[rt] = sum[rt<<] + sum[rt<<|];
} void pushdown(int rt, int x)
{
if(lazy[rt] != -) {
lazy[rt<<] = lazy[rt<<|] = lazy[rt];
sum[rt<<] = (x-(x>>))*lazy[rt];///!!!
sum[rt<<|] = (x>>)*lazy[rt];///!!!
lazy[rt] = -;
}
} void creat(int l, int r, int rt)
{
lazy[rt] = -, sum[rt] = ;
if(l == r) return;
int mid = (l+r)>>;
creat(l, mid, rt<<);
creat(mid+, r, rt<<|);
pushup(rt);
} void modify(int l, int r, int x, int L, int R, int rt)
{
if(l <= L && r >= R) {
lazy[rt] = x;
sum[rt] = x*(R-L+);///!!!
return;
}
pushdown(rt, R-L+);///!!!
int mid = (L+R)>>;
if(l <= mid) modify(l, r, x, L, mid, rt<<);
if(r > mid) modify(l, r, x, mid+, R, rt<<|);
pushup(rt);
} int main()
{
int i, j, k = ;
int n, T, q;
int x, y, w;
//while(scanf("%d", &T) != EOF)
//while(T--)
//{
scanf("%d %d", &n, &q);
creat(, n, ); while(q--) {
scanf("%d %d", &x, &y);
modify(x, y, , , n, );
printf("%d\n",n-sum[]);
} //printf("Case %d: The total value of the hook is %d.\n", ++k, sum[1]);
//}
return ;
}