codevs 1191 数轴染色 题解报告

来源:互联网 发布:软件测试基础知识ppt 编辑:程序博客网 时间:2024/04/28 08:11

这里写图片描述

题目描述 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

吃果果的线段树;;

#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<vector>#include<queue>#include<map>#include<set>#include<stack>#include<cstdlib>#include<string>#include<bitset>#include<iomanip>#include<deque>#define INF 1000000000#define fi first#define se second#define N 100005#define P 1000000007#define debug(x) cerr<<#x<<"="<<x<<endl#define MP(x,y) make_pair(x,y)using namespace std;int n,m;int t[1000010],lazy[1000010]; inline int get_num(){int num = 0;char c;bool flag = false;while ((c = getchar()) == ' ' || c == '\n' || c == '\r');if (c == '-') flag = true;else num = c - '0';while (isdigit(c = getchar()))num = num * 10 + c - '0';return (flag ? -1 : 1) * num;}void build(int x,int l,int r){    if(l==r)    {        t[x]=1;        return;    }     int mid=(l+r)>>1;    build(2*x,l,mid);    build(2*x+1,mid+1,r);    t[x]=t[2*x]+t[2*x+1];}void find(int x,int l,int r,int q,int w){    if(t[x]==0)    return;    if(l==q&&r==w)    {        t[x]=0;        return;    }    int mid=(l+r)>>1;    if(mid>=w)    {        find(2*x,l,mid,q,w);    }else    {        if(mid<q)        find(2*x+1,mid+1,r,q,w);        else        {        find(2*x,l,mid,q,mid);        find(2*x+1,mid+1,r,mid+1,w);        }    }    t[x]=t[2*x]+t[2*x+1];}int main(){    cin>>n>>m;    build(1,1,n);    for(int i=1;i<=m;i++)    {        int q,w;        q=get_num();        w=get_num();        find(1,1,n,q,w);        cout<<t[1]<<endl;    }}

这里写图片描述

0 0
原创粉丝点击