zoukankan      html  css  js  c++  java
  • 线段树+离散化+染色

    题目链接:https://cn.vjudge.net/contest/66989#status/17111202012/D/0/

    这个题目太坑了,一直给报超时,然后调了一下午发现多开了一个map。。。。。

    AC代码:

    #include<iostream>
    #include<string>
    #include<cstring>
    #include<iomanip>
    #include<cmath>
    #include<algorithm>
    #include<stdio.h>
    #include<map>
    using namespace std;
    # define maxn 10000000+100
    # define lson l,m,rt<<1
    # define rson m+1,r,rt<<1|1
    //# define m (l+r)>>1
    int a[maxn];
    int tot[maxn];
    int fi[maxn],se[maxn];
    int col[maxn];//0代表没有颜色,-1代表有多种颜色。
    int q1[maxn];
    map<int,int >w;
    int ans;
    void cal(int rt)
    {
        col[rt<<1]=col[rt];
        col[rt<<1|1]=col[rt];
        col[rt]=0;
    }
    void update(int L,int R,int l,int r,int rt,int num)
    {
        if(L<=l&&R>=r)
        {
            col[rt]=num;
            return ;
        }//染色的过程
        if(col[rt]>=0)//这个地方的染色始终是比线段树的整体进程快一步的,可以画图理解一下。
        {
            cal(rt);
        }
        int m=(l+r)>>1;
        if(L<=m)update(L,R,lson,num);
        if(R>m)update(L,R,rson,num);
    
        if(col[rt]>=0)
            col[rt]=-1;//如果有多个颜色出现的话,应该等到左子树和右子树都更新完毕后才能赋值为-1,
    }
    void f(int l,int r,int rt)
    {
        if(col[rt]>=0)
        {
            w[col[rt]]=1;
            return ;
        }
        int m=(l+r)>>1;
        f(lson);
        f(rson);
    }
    int main()
    {
        int T;
        // T=read();
        scanf("%d",&T);
        while(T--)
        {
            int n;
            // n=read();
            scanf("%d",&n);
            int cnt=0;
            for(int i=1; i<=n; i++)
            {
                //fi[i]=read();
                //     se[i]=read();
                scanf("%d%d",&fi[i],&se[i]);
                tot[++cnt]=fi[i];
                tot[++cnt]=se[i];
            }//离散化的过程
            sort(tot+1,tot+cnt+1);
            ans=0;
            q1[tot[1]]=++ans;
            for(int i=2; i<=cnt; i++)
            {
                if(tot[i]!=tot[i-1])q1[tot[i]]=++ans;
            }//之前超时就一直因为这个,因为在这个多开了一个map所以超时了,因为已经排好序了,直接判断和前一个是否相等就可以去重了。
            col[1]=0;
           // build(1,ans,1);
            for(int i=1; i<=n; i++)
            {
                update(q1[fi[i]],q1[se[i]],1,ans,1,i);
            }
            f(1,ans,1);
            int anss=w.size();
            printf("%d
    ",anss);
            w.clear();
        }
    
        return 0;
    }
    
  • 相关阅读:
    CCF201604试题
    CCF201512试题
    CCF201509试题
    CCF201509试题
    CCF201503试题
    CCF201503试题
    CCF201412试题
    CCF201412试题
    CCF201409试题
    CCF201409试题
  • 原文地址:https://www.cnblogs.com/letlifestop/p/10262854.html
Copyright © 2011-2022 走看看