zoukankan      html  css  js  c++  java
  • SPOJ DQUERY

    DQUERY - D-query

    Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) (1 ≤ i ≤ j ≤ n). For each d-query (i, j), you have to return the number of distinct elements in the subsequence ai, ai+1, ..., aj.

    Input

    • Line 1: n (1 ≤ n ≤ 30000).
    • Line 2: n numbers a1, a2, ..., an (1 ≤ ai ≤ 106).
    • Line 3: q (1 ≤ q ≤ 200000), the number of d-queries.
    • In the next q lines, each line contains 2 numbers i, j representing a d-query (1 ≤ i ≤ j ≤ n).

    Output

    • For each d-query (i, j), print the number of distinct elements in the subsequence ai, ai+1, ..., aj in a single line.

    Example

    Input
    5
    1 1 2 1 3
    3
    1 5
    2 4
    3 5
    
    Output
    3
    2
    3 
    分析:离线树状数组,在线主席树;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, rt<<1
    #define Rson mid+1, R, rt<<1|1
    const int maxn=1e6+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,pos[maxn],a[maxn],b[maxn],ans[maxn];
    vector<pii >query[maxn];
    void add(int x,int y)
    {
        for(int i=x;i<=n;i+=(i&(-i)))
            a[i]+=y;
    }
    int get(int x)
    {
        int ret=0;
        for(int i=x;i;i-=(i&(-i)))
            ret+=a[i];
        return ret;
    }
    int main()
    {
        int i,j;
        scanf("%d",&n);
        rep(i,1,n)scanf("%d",&b[i]);
        scanf("%d",&m);
        rep(i,1,m)
        {
            int c,d;
            scanf("%d%d",&c,&d);
            query[d].pb(mp(c,i));
        }
        rep(i,1,n)
        {
            if(pos[b[i]])add(pos[b[i]],-1);
            pos[b[i]]=i;
            add(pos[b[i]],1);
            for(pii x:query[i])ans[x.se]=get(i)-get(x.fi-1);
        }
        rep(i,1,m)printf("%d
    ",ans[i]);
        //system("Pause");
        return 0;
    }
    主席树:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    const int maxn=1e6+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,sz,pos[maxn],s[maxn],ls[maxn],rs[maxn],a[maxn],root[maxn];
    void insert(int l,int r,int x,int &y,int v,int p)
    {
        y=++sz;
        s[y]=s[x]+p;
        if(l==r)return;
        ls[y]=ls[x],rs[y]=rs[x];
        int mid=l+r>>1;
        if(v<=mid)insert(l,mid,ls[x],ls[y],v,p);
        else insert(mid+1,r,rs[x],rs[y],v,p);
    }
    int query(int l,int r,int L,int R,int rt)
    {
        if(l==L&&r==R)return s[rt];
        int mid=L+R>>1;
        if(r<=mid)return query(l,r,Lson);
        else if(l>mid)return query(l,r,Rson);
        else return query(l,mid,Lson)+query(mid+1,r,Rson);
    }
    int main()
    {
        int i,j;
        scanf("%d",&n);
        rep(i,1,n)a[i]=read();
        rep(i,1,n)
        {
            insert(1,n,root[i-1],root[i],i,1);
            if(pos[a[i]])insert(1,n,root[i],root[i],pos[a[i]],-1);
            pos[a[i]]=i;
        }
        scanf("%d",&m);
        rep(i,1,m)
        {
            int c,d;
            c=read(),d=read();
            printf("%d
    ",query(c,d,1,n,root[d]));
        }
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    最有价值的专家--微软官方网站
    CC++初学者编程教程(16) 搭建Xcode cocos2dx开发环境--尹成老师博客
    微软最具价值的专家之毛星云博客
    Winsock IO模型之IOCP模型
    IOCP模型与网络编程
    关于proc索引的创建
    查找表存在于那些proc中
    Sql 时间做条件
    Sql 无指定条件,防并发update
    sql 根据客户需用 增减查询字段
  • 原文地址:https://www.cnblogs.com/dyzll/p/5920839.html
Copyright © 2011-2022 走看看