zoukankan      html  css  js  c++  java
  • codevs 1191 数轴染色

                  codevs 1191 数轴染色 

    题目描述 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<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int maxn=500000+2;
    int fa[maxn],rank[maxn];
    int n,m,cnt,l,r;
    void init(int n) {
        for(int i=1; i<=n; i++) 
            fa[i]=i,rank[i]=0;
    }
    int find(int x) {
        return fa[x]==x?x:fa[x]=find(fa[x]);
    }
    void merge(int x,int y) {
        fa[find(y)]=find(x);
    }
    bool same(int x,int y) {
        return find(x)==find(y);
    }
    int main() {
        scanf("%d%d",&n,&m);
        init(n);
        while(m--) {
            scanf("%d%d",&l,&r);
            r=find(r);
            while(r>=l) {
                if(r!=l&&same(l,r)) break;
                merge(r-1,r);
                cnt++;
                r=find(r);
            }
            cout<<n-cnt<<'
    ';
        }
        return 0;
    }
    并查集
    #include<iostream>
    #include<cstring>
    #include<cstdio> 
    using namespace std;
    const int MAXN = 1000005;
    int m,n,l,r;
    struct tree {
        int l,r,sum;
        bool xc;
    } tree[MAXN << 2];
    void update(int p) {
        tree[p].sum = tree[p<<1].sum + tree[p<<1|1].sum;
    }
    void build(int l,int r,int p) {
        tree[p].l = l,tree[p].r = r;
        if(l == r) {
            tree[p].sum = 1;
            return;
        }
        int mid = (l + r) >> 1;
        build(l,mid,p<<1);
        build(mid + 1,r,p<<1|1);
        update(p);
    }
    void change(int l,int r,int p) {
        if(l <= tree[p].l && tree[p].r <= r) {
            tree[p].sum = 0;
            tree[p].xc = true;
            return;
        }
        if(tree[p].xc == true)  return;
        int mid = (tree[p].l + tree[p].r) >> 1;
        if(l <= mid)    change(l,r,p<<1);
        if(mid < r)     change(l,r,p<<1|1);
        update(p);
        return;
    }
    int ask(int l,int r,int p) {
        if(l <= tree[p].l && tree[p].r <= r)
            return tree[p].sum;
        int ans = 0,mid = (tree[p].l + tree[p].r) >> 1;
        if(l <= mid)    ans += ask(l,r,p<<1);
        if(mid < r)     ans += ask(l,r,p<<1|1);
        update(p);
        return ans;
    }
    int main() {
        memset(tree,0,sizeof(tree));
        cin>>n>>m;
        build(1,n,1);
        for(int i = 1; i <= m; i ++) {
            cin>>l>>r;
            change(l,r,1);
            printf("%d
    ",ask(1,n,1));
        }
        return 0;
    }
    线段树

    当时做的时候被Dev-C++ 5.9.2 坑了一笔,因为它自带cstdio效果所以我一个输出用了printf没加cstdio,在codevs上交了五次,CE了五次,我一度以为codevs评测机出错了,最后终于认真看了一下,原来少了个库。

     人生有四然,来是偶然,去是必然,顺其自然,理所当然。

  • 相关阅读:
    系统可靠性计算
    jira与readmine区别
    linux下批量替换文件内容
    JMeter学习(十九)JMeter测试MongoDB
    mongoVUE1.5.3 破解方法
    Junit使用GroboUtils进行多线程测试
    JMeter学习(十八)JMeter测试Java(二)
    JMeter学习(十七)JMeter测试Java
    Tomcat 和 Resin 比较,哪个更适合你?
    JMeter学习(十四)JMeter监控Tomcat性能
  • 原文地址:https://www.cnblogs.com/GTBD/p/9227880.html
Copyright © 2011-2022 走看看