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\n", Answer[i]);
//    system ("pause");
    return 0;
}

 

posted @ 2017-06-06 08:45  ZlycerQan  阅读(185)  评论(0编辑  收藏  举报