zoukankan      html  css  js  c++  java
  • luogu P2056 采花

    二次联通门 : luogu P2056 采花

    /*
        luogu P2056 采花
     
        貌似是线段树的题。。。
        
        莫队水过(此题的数据弱化了, 所以莫队能过)
    
        记录一个count数组
        储存每种颜色出现的次数即可
        
    */
    #include <algorithm>
    #include <cstdio>
    #include <cmath>
    #include <cstdlib>
    
    #define Max 1000005
    
    inline void read (int &now)
    {
        now = 0;
        register char word = getchar ();
        while (word < '0' || word > '9')
            word = getchar ();
        while (word >= '0' && word <= '9')
        {
            now = now * 10 + word - '0';
            word = getchar ();
        }
    }
    
    int belong[Max];
    
    struct Query_Data 
    {
        int l, r;
    
        int Id;
    
        bool operator < (const Query_Data &now) const
        {
            if (belong[this->l] == belong[now.l])
                return this->r < now.r;;
            return belong[this->l] < belong[now.l];
        }
    };
    
    
    int number[Max];
    int count[Max];
    
    int Answer[Max];
    int Result;
    
    void Updata (int now, bool type)
    {
        if (type)
        {
            if (count[number[now]] == 1)
                Result ++;
            count[number[now]] ++;
        }
        else
        {
            if (count[number[now]] == 2)
                Result --;
            count[number[now]] --;
        }
    }
    
    Query_Data query[Max];
    
    int main (int argc, char *argv[])
    {
        int N, M, C;
        read (N);
        read (C);
        read (M);
        int K_Size = sqrt (N);
        for (int i = 1; i <= N; i ++)
        {
            belong[i] = (i + 1) / K_Size;
            read (number[i]);
        }
        for (int i = 1; i <= M; i ++)
        {
            read (query[i].l);
            read (query[i].r);
            query[i].Id = i;
        }
        std :: sort (query + 1, query + 1 + M);
        int l = 1, r = 0;
        for (int i = 1; i <= M; i ++)
        {
            while (l < query[i].l)
                Updata (l ++, false);
            while (l > query[i].l)
                Updata (-- l, true);
            while (r < query[i].r)
                Updata (++ r, true);
            while (r > query[i].r)
                Updata (r --, false);
            Answer[query[i].Id] = Result;
        }
        for (int i = 1; i <= M; i ++)
            printf ("%d
    ", Answer[i]);
    //    system ("pause");
        return 0;
    }
  • 相关阅读:
    python3:操作excel文件
    将博客搬至CSDN
    easyui给select控件绑定change事件
    Oracle 查询时间在当天的数据
    Spring Jdbc使用like模糊查询
    Intellij idea workflow 工作流插件安装
    使用jQuery出现the function undefined
    java.el.PropertyNotFoundException解决方法
    使用反射类、Class类获取指定的构造器并实例化对象
    使用mybatis查询数据,按特定顺序排序
  • 原文地址:https://www.cnblogs.com/ZlycerQan/p/6949613.html
Copyright © 2011-2022 走看看