zoukankan      html  css  js  c++  java
  • 《洛谷P1972 [SDOI2009]HH的项链》

    第一眼:啊,大水题,直接上莫队分块,70分,原地懵逼。

    因为出题人疯狂加强数据,莫队的复杂度已经跟不上了orz。

    考虑新的解法:首先,我们可以统计一个数出现的位置。

    我们先对询问离线,进行r的升序。

    那么可以发现,就算前面出现过当前数,也只能算作一个数,且因为我们按r升序。

    那么这个数越后面就越能影响到答案。

    所以我们用pre数组来统计这个数出现的位置,然后在树状数组上更新每个位置是否有数出现。

    同一个数只能出现在一个位置,且尽量靠后即可。用1表示该位置有数,0表示每个。

    那么对于区间[L,r]只需要在树状数组上统计sum[r] - sum[L-1]即可。

    当我们在当前位置插入这个数时,同时要把它前一个位置删去,这样才不会重复。

    // Author: levil
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    typedef pair<int,int> pii;
    const int N = 1e6+5;
    const int M = 2e5+5;
    const LL Mod = 199999;
    #define rg register
    #define pi acos(-1)
    #define INF 1e9
    #define CT0 cin.tie(0),cout.tie(0)
    #define IO ios::sync_with_stdio(false)
    #define dbg(ax) cout << "now this num is " << ax << endl;
    namespace FASTIO{
        inline LL read(){
            LL x = 0,f = 1;char c = getchar();
            while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();}
            while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();}
            return x*f;
        }
        void print(int x){
            if(x < 0){x = -x;putchar('-');}
            if(x > 9) print(x/10);
            putchar(x%10+'0');
        }
    }
    using namespace FASTIO;
    void FRE(){
    /*freopen("data1.in","r",stdin);
    freopen("data1.out","w",stdout);*/}
    
    int n,m,c[N],a[N],ans[N],pre[N];//pre记录上一次出现的位置
    struct Node{int L,r,id;}p[N];
    bool cmp(Node a,Node b)
    {
        if(a.r == b.r) return a.L < b.L;
        return a.r < b.r;
    }
    int lowbit(int x){return x&(-x);}
    void update(int x,int val)
    {
        for(rg int i = x;i < N;i += lowbit(i)) c[i] += val;
    }
    int query(int x)
    {
        int ans = 0;
        for(rg int i = x;i > 0;i -= lowbit(i)) ans += c[i];
        return ans;
    }
    int main()
    {
        n = read();
        for(rg int i = 1;i <= n;++i) a[i] = read();
        m = read();
        for(rg int i = 1;i <= m;++i) p[i].L = read(),p[i].r = read(),p[i].id = i;
        sort(p+1,p+m+1,cmp);
        int L = 1;
        for(rg int i = 1;i <= m;++i)
        {
            for(;L <= p[i].r;++L) 
            {
                int x = pre[a[L]];
                if(x != 0) update(x,-1);
                pre[a[L]] = L;
                update(L,1);
            }
            ans[p[i].id] = query(p[i].r)-query(p[i].L-1);
        }
        for(rg int i = 1;i <= m;++i) printf("%d
    ",ans[i]); 
        system("pause");
    }
    View Code
  • 相关阅读:
    Error 500curl_setopt(): CURLOPT_SSL_VERIFYHOST no longer accepts the value 1, value 2 will be used i
    解决Apache提示没有权限访问的问题
    【服务器】本地运行成功,但服务器上运行却显示错误500
    CentOS 7中PHP配置文件php.ini的放在哪个位置
    用yum快速搭建LAMP平台
    redis 在32位系统安装以及使用
    CentOS7查看和关闭防火墙
    CENTOS7下安装REDIS
    在centos和redhat上安装docker
    Annotation 注解
  • 原文地址:https://www.cnblogs.com/zwjzwj/p/13584777.html
Copyright © 2011-2022 走看看