zoukankan      html  css  js  c++  java
  • HDU3333 Turing Tree 树状数组+离线处理

    Turing Tree

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4948    Accepted Submission(s): 1746

    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
     
     
    题意:给T组数据,每组数据n个数及m个询问,每个询问[l,r]问区间不同的数的和为多少?
     
    题解:每个询问离线处理,先按r递增排,再按l递增排序,定义一个last[]用于记录数最后出现的位置,维护一个已经更新的区间[ll,rr],每次只要加入[ll,l],[rr,r]的区间,放入树状数组进行求和
    注意:加入树状数组的时候要判断last[],如果出现过就要删除原来的,保证树状数组里每个数只出现一次而且在最后一个位置
    每个询问答案是sum[rr]-sum[ll-1]
     
    #include<bits/stdc++.h>
    #define N 145000
    #define mes(x) memset(x, 0, sizeof(x));
    #define ll __int64
    const long long mod = 1e9+7;
    const int MAX = 0x7ffffff;
    using namespace std;
    ll c[N], n, a[N];
    ll ans[N];
    map<ll,int>last;
    struct node{
        int l,r,k;
    }q[N];
    int lowbit(int x){
        return x&(-x);
    }
    int cmp(node a, node b){
        if(a.r == b.r) return a.l<b.l;
        return a.r<b.r;
    }
    int cmp1(node a, node b){
        return a.k<b.k;
    }
    ll sum(int x){
        ll sum=0;
        while(x > 0){
            sum = sum+c[x];
            x -= lowbit(x);
        }
        return sum;
    }
    void add(int x,int d){
        while(x <= n){
            c[x] = c[x]+d;
            x += lowbit(x);
        }
    }
    int main()
    {
        int T, i, j, m, l, r;
        while(~scanf("%d", &T)){
            while(T--){
            scanf("%d", &n);
            for(i=1;i<=n;i++)
                scanf("%d", &a[i]);
            scanf("%d", &m);
            for(i=0;i<m;i++){
                scanf("%d%d", &q[i].l, &q[i].r);
                q[i].k = i;
            }
            sort(q, q+m,cmp);
            mes(c);
            last.clear();
            l = q[0].l;
            r = q[0].l;
            for(i=0;i<m;i++){
                    for(j=q[i].l;j<=l;j++){
                        if(!last[a[j]]){
                        last[a[j]] = j;
                        add(j, a[j]);
                        }
                    }
                    for(j=r+1;j<=q[i].r;j++){
                        if(!last[a[j]]){
                            last[a[j]] = j;
                            add(j, a[j]);//
                        }
                        else{
                            add(last[a[j]], -a[j]);
                            last[a[j]] = j;
                            add(j, a[j]);
                        }
                    }
                    ans[q[i].k] = sum(q[i].r)-sum(q[i].l-1);
                    l = min(l,q[i].l);
                    r = max(r,q[i].r);
                }
                for(i=0;i<m;i++)
                    printf("%I64d
    ", ans[i]);
            }
        }
        return 0;
    }
     
     
     
  • 相关阅读:
    注意技巧小结
    小数点转百分比
    Mysql 性能监控及调优
    Linux下快速安装Mysql及使用
    如何建立压力计算模型
    Redis 2种持久化模式的缺陷
    Redis性能调优建议
    Node.js学习入门手册
    手机APP脚本录制(LoadRunner 12)
    Linux系统内核性能调优
  • 原文地址:https://www.cnblogs.com/Noevon/p/6144021.html
Copyright © 2011-2022 走看看