zoukankan      html  css  js  c++  java
  • HDU #3333

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)



    Problem Description
    After inventing Turing Tree, 3xian always felt boring when solving problems about intervals, because Turing Tree could easily have the solution. As well, wily 3xian made lots of new problems about intervals. So, today, this sick thing happens again...

    Now given a sequence of N numbers A1, A2, ..., AN and a number of Queries(i, j) (1≤i≤j≤N). For each Query(i, j), you are to caculate the sum of distinct values in the subsequence Ai, Ai+1, ..., Aj.
     
    Input
    The first line is an integer T (1 ≤ T ≤ 10), indecating the number of testcases below.
    For each case, the input format will be like this:
    * Line 1: N (1 ≤ N ≤ 30,000).
    * Line 2: N integers A1, A2, ..., AN (0 ≤ Ai ≤ 1,000,000,000).
    * Line 3: Q (1 ≤ Q ≤ 100,000), the number of Queries.
    * Next Q lines: each line contains 2 integers i, j representing a Query (1 ≤ i ≤ j ≤ N).
     
    Output
    For each Query, print the sum of distinct values of the specified subsequence in one line.
     
    Sample Input
    2
    3
    1 1 4
    2
    1 2
    2 3
    5
    1 1 2 1 3
    3
    1 5
    2 4
    3 5
     
    Sample Output
    1
    5
    6
    3
    6
     
    Author
    3xian@GDUT
     
    Source
     
    Recommend
    lcy
     
    题目大意:
    查询区间[L, R]内所有不同元素的和。
     
    解法:
    在线做法我还不清楚,有一个利用树状数组的巧妙的离线做法。
    将所有查询[L, R]按右端点R从小到大排序后,依次处理。
    要点是记录一个数上一次出现的位置,这样就可以将当前的数在上个位置上的计数消除。
    这样做不会影响当前的查询,因为当这个数的当前位置离所查询的区间的右端点更近。
     
    Implementation:
    总体是个two-pointer
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    
    const int N(3e4+5), M(1e5+5);
    
    LL bit[N], ans[M];
    int pos[N], a[N], b[N], n, q;
    
    void add(int x, int v)
    {
        for(; x<=n; bit[x]+=v, x+=x&-x);
    }
    
    LL sum(int x)
    {
        LL res=0;
        for(; x; res+=bit[x], x-=x&-x);
        return res;
    }
    
    struct P
    {
        int l, r, id;
        P(int l, int r, int id):l(l),r(r),id(id){}
        P(){}
        bool operator<(const P &b)const{return r<b.r;}
    }p[M];
    
    
    int main()
    {
        int T;
        for(cin>>T; T--; )
        {
            cin>>n;
            for(int i=1; i<=n; i++) cin>>a[i], b[i-1]=a[i];
            sort(b, b+n);   //error-prone
            int *e=unique(b, b+n);
            memset(bit, 0, sizeof(bit));
            memset(pos, 0, sizeof(pos));
            cin>>q;
            for(int l, r, i=0; i<q; i++)
            {
                cin>>l>>r;
                p[i]={l, r, i};
            }
            sort(p, p+q);
            for(int i=0, j=1, k; i<q && j<=n; )
            {
                for(; j<=p[i].r; j++)
                {
                    int id=lower_bound(b, e, a[j])-b;
                    if(pos[id]) add(pos[id], -a[j]);
                    pos[id]=j, add(j, a[j]);
                }
                for(k=i; p[k].r==p[i].r; k++)
                {
                    ans[p[k].id]=sum(p[k].r)-sum(p[k].l-1);
                }
                i=k;
            }
            for(int i=0; i<q; i++) cout<<ans[i]<<endl;
        }
        return 0;
    }
     
     
  • 相关阅读:
    第一个程序01 零基础入门学习汇编语言20
    函数03 零基础入门学习C语言34
    函数03 零基础入门学习C语言34
    寄存器(内存访问)05 零基础入门学习汇编语言17
    寄存器(内存访问)06 零基础入门学习汇编语言18
    寄存器(内存访问)07 零基础入门学习汇编语言19
    寄存器(内存访问)06 零基础入门学习汇编语言18
    VC++实现恢复SSDT
    现实世界Windows Azure: 排气系统制造商 Akrapovič Revs 运用Windows Azure进行全球运营
    VC++实现全局钩子勾住消息对话框
  • 原文地址:https://www.cnblogs.com/Patt/p/5410647.html
Copyright © 2011-2022 走看看