题目描述 Description
在一条数轴上有N个点,分别是1~N。一开始所有的点都被染成黑色。接着
我们进行M次操作,第i次操作将[Li,Ri]这些点染成白色。请输出每个操作执行后
剩余黑色点的个数。
输入描述 Input Description
输入一行为N和M。下面M行每行两个数Li、Ri
输出描述 Output Description
输出M行,为每次操作后剩余黑色点的个数。
样例输入 Sample Input
10 3
3 3
5 7
2 8
样例输出 Sample Output
9
6
3
数据范围及提示 Data Size & Hint
数据限制
对30%的数据有1<=N<=2000,1<=M<=2000
对100%数据有1<=Li<=Ri<=N<=200000,1<=M<=200000
思路:把区间的值改成0,求区间和;
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#define true ture
#define false flase
using namespace std;
#define ll long long
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
struct is
{
int l,r;
int num;
int lazy;
}tree[*];
void build_tree(int l,int r,int pos)
{
tree[pos].l=l;
tree[pos].r=r;
tree[pos].lazy=-;
if(l==r)
{
tree[pos].num=;
//scanf("%lld",&tree[pos].num);
return;
}
int mid=(l+r)/;
build_tree(l,mid,pos*);
build_tree(mid+,r,pos*+);
tree[pos].num=tree[pos*].num+tree[pos*+].num;
}
void update(int l,int r,int change,int pos)
{
if(tree[pos].l==l&&tree[pos].r==r)
{
tree[pos].lazy=change;
tree[pos].num=;
return;
}
if(tree[pos].lazy==)
{
tree[pos*].num=(tree[pos*].r+-tree[pos*].l)*tree[pos].lazy;
tree[pos*+].num=(tree[pos*+].r+-tree[pos*+].l)*tree[pos].lazy;
tree[pos*].lazy=tree[pos].lazy;
tree[pos*+].lazy=tree[pos].lazy;
tree[pos].lazy=-;
}
int mid=(tree[pos].l+tree[pos].r)/;
if(r<=mid)
update(l,r,change,pos*);
else if(l>mid)
update(l,r,change,pos*+);
else
{
update(l,mid,change,pos*);
update(mid+,r,change,pos*+);
}
tree[pos].num=tree[pos*].num+tree[pos*+].num;
}
int query(int l,int r,int pos)
{
//cout<<l<<" "<<r<<" "<<pos<<endl;
if(tree[pos].l==l&&tree[pos].r==r)
return tree[pos].num;
if(tree[pos].lazy==)
{
tree[pos*].num=;
tree[pos*+].num=;
tree[pos*].lazy=;
tree[pos*+].lazy=;
tree[pos].lazy=-;
}
int mid=(tree[pos].l+tree[pos].r)/;
if(l>mid)
return query(l,r,pos*+);
else if(r<=mid)
return query(l,r,pos*);
else
return query(l,mid,pos*)+query(mid+,r,pos*+);
}
int main()
{
int x,q,i,t;
while(~scanf("%d",&x))
{
scanf("%d",&q);
build_tree(,x,);
while(q--)
{
int flag,change=,l,r;
scanf("%d%d",&l,&r);
update(l,r,change,);
printf("%d\n",query(,x,));
}
}
return ;
}