• 【bzoj1878】[SDOI2009]HH的项链


    *题目描述:
    HH有一串由各种漂亮的贝壳组成的项链。HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一段贝壳,思考它们所表达的含义。HH不断地收集新的贝壳,因此, 他的项链变得越来越长。有一天,他突然提出了一个问题:某一段贝壳中,包含了多少种不同 的贝壳?这个问题很难回答。。。因为项链实在是太长了。于是,他只好求助睿智的你,来解 决这个问题。
    *输入:
    第一行:一个整数N,表示项链的长度。 第二行:N个整数,表示依次表示项链中贝壳的编号(编号为0到1000000之间的整数)。 第三行:一个整数M,表示HH询问的个数。 接下来M行:每行两个整数,L和R(1 ≤ L ≤ R ≤ N),表示询问的区间。
    *输出:
    M行,每行一个整数,依次表示询问对应的答案。
    *样例输入:
    6
    1 2 3 4 3 5
    3
    1 2
    3 5
    2 6
    *样例输出:
    2
    2
    4
    *提示:
    对于20%的数据,N ≤ 100,M ≤ 1000;
    对于40%的数据,N ≤ 3000,M ≤ 200000;
    对于100%的数据,N ≤ 50000,M ≤ 200000。
    *题解:
    有点像2120数颜色,只不过不需要修改。于是就用主席树维护。
    *代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    
    #ifdef WIN32
        #define LL "%I64d"
    #else
        #define LL "%lld"
    #endif
    
    #ifdef CT
        #define debug(...) printf(__VA_ARGS__)
        #define setfile() 
    #else
        #define debug(...)
        #define filename ""
        #define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
    #endif
    
    #define R register
    #define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
    #define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
    #define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
    #define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
    #define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
    char B[1 << 15], *S = B, *T = B;
    inline int FastIn()
    {
        R char ch; R int cnt = 0; R bool minus = 0;
        while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
        ch == '-' ? minus = 1 : cnt = ch - '0';
        while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
        return minus ? -cnt : cnt;
    }
    #define maxn 50010
    #define maxtot 1000010
    int a[maxn], hash[maxn], tot, newn, last[maxn], prev[maxn], n;
    int root[maxn], size[maxtot], ch[maxtot][2];
    inline void Insert(R int last, R int val)
    {
        R int l = 0, r = n;
        while (l < r)
        {
            size[++tot] = size[last] + 1;
            R int mid = l + r >> 1;
            if (val > mid)
            {
                l = mid + 1;
                ch[tot][0] = ch[last][0];
                ch[tot][1] = tot + 1;
                last = ch[last][1];
            }
            else
            {
                r = mid;
                ch[tot][0] = tot + 1;
                ch[tot][1] = ch[last][1];
                last = ch[last][0];
            }
        }
        size[++tot] = size[last] + 1;
    }
    inline int Query(R int a, R int b, R int val)
    {
        R int ans = 0, fa = root[a - 1], fb = root[b], l = 0, r = n;
        while (l < r)
        {
            R int mid = l + r >> 1;
            if (mid < val)
            {
                ans += size[ch[fb][0]] - size[ch[fa][0]];
                fb = ch[fb][1];
                fa = ch[fa][1];
                l = mid + 1;
            }
            else
            {
                fb = ch[fb][0];
                fa = ch[fa][0];
                r = mid;
            }
        }
        ans += size[fb] - size[fa];
        return ans;
    }
    int main()
    {
    //  setfile();
        n = FastIn();
        for (R int i = 1; i <= n; ++i) a[i] = hash[i] = FastIn();
        std::sort(hash + 1, hash + n + 1);
        newn = std::unique(hash + 1, hash + n + 1) - hash - 1;
        for (R int i = 1; i <= n; ++i)
            a[i] = std::lower_bound(hash + 1, hash + n + 1, a[i]) - hash;
        for (R int i = 1; i <= n; ++i)
        {
            prev[i] = last[a[i]];
            last[a[i]] = i;
            root[i] = tot + 1;
            Insert(root[i - 1], prev[i]);
        }
        for (R int q = FastIn(); q; --q)
        {
            R int l = FastIn(), r = FastIn();
            printf("%d
    ", Query(l, r, l - 1) );
        }
        return 0;
    }
    /*
    6
    1 2 3 4 3 5
    3
    1 2 
    3 5
    2 6
    */
  • 相关阅读:
    八大排序
    链表的合并
    记录B站yxc的背包九讲相关代码
    C++中多态实现
    YOLOV4所用到的一些tricks
    C++中的string 和 stringstream 的知识
    博客园中插入视频
    博客园中插入网页
    面试前必须要知道的【可重入锁 自旋锁】
    面试前必须要知道的【乐观锁 悲观锁】
  • 原文地址:https://www.cnblogs.com/cocottt/p/5550950.html
Copyright © 2020-2023  润新知