zoukankan      html  css  js  c++  java
  • HDU-3333 Turing Tree 【树状数组+离线处理+离散化】

    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
     

     
    题解:
     
    根据数据范围,这题肯定是要离线处理询问的,ai范围1e9但是最多只有30000个,因此离散化映射到一个连续的区间内,最后用树状数组维护。
     
     
    代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<set>
     6 using namespace std;
     7 #define Mem(a, b) memset(a, b, sizeof(a))
     8 typedef __int64 ll;
     9 const int N = 30000 + 5;
    10 const int M = 100000 + 5;
    11 int a[N], b[N], d[N], aa[N], Next[N], p[N], n, m;
    12 ll ans[M], C[N];
    13 struct Node {
    14     int l, r, id;
    15 }q[M];
    16 set<int> s;
    17 
    18 bool cmp(Node a, Node b) {return a.l < b.l;}
    19 
    20 int lowbit(int x) {return x & -x;}
    21 
    22 void add(int x, int d) {
    23     while (x <= n) {
    24         C[x] += d; x += lowbit(x);
    25     }
    26 } 
    27 
    28 ll sum(int x) {
    29     ll ret = 0;
    30     while (x) {
    31         ret += C[x]; x -= lowbit(x);
    32     }
    33     return ret;
    34 }
    35 
    36 int main() {
    37     int T;
    38     scanf("%d", &T);
    39     while (T--) {
    40         Mem(Next, 0); Mem(p, 0); Mem(C, 0);
    41         scanf("%d", &n);
    42         int tot = 0;
    43         s.clear();
    44         for (int i = 1; i <= n; ++i) {
    45             scanf("%d", &a[i]);
    46             if (!s.count(a[i])) {
    47                 s.insert(a[i]);
    48                 d[tot++] = a[i];
    49             }
    50         }
    51         sort(d, d+tot);
    52         for (int i = 1; i <= n; ++i) {
    53             int idx = lower_bound(d, d+tot, a[i]) - d + 1;
    54             aa[i] = idx;
    55             b[idx] = a[i];
    56         }
    57         for (int i = n; i >= 1; --i) Next[i] = p[aa[i]], p[aa[i]] = i;
    58         for (int i = 1; i <= tot; ++i) 
    59             if(p[i]) add(p[i], b[i]);
    60         scanf("%d", &m);
    61         for (int i = 1; i <= m; ++i)
    62             scanf("%d%d", &q[i].l, &q[i].r), q[i].id = i;
    63         sort(q+1, q+1+m, cmp);
    64         int l = 1;
    65         for (int i = 1; i <= m; ++i) {
    66             while (l < q[i].l) {
    67                 if (Next[l]) add(Next[l], b[aa[l]]);
    68                 ++l;
    69             }
    70             ans[q[i].id] = sum(q[i].r) - sum(q[i].l-1);
    71         }
    72         for (int i = 1; i <= m; ++i) printf("%I64d
    ", ans[i]);
    73     }
    74 
    75     return 0;
    76 }
  • 相关阅读:
    LeetCode 23. Merge k Sorted Lists
    第四章 基本TCP套接字编程 第五章 TCP客户/服务器程序实例
    LeetCode 18. 4Sum
    LeetCode 16. 3Sum Closest
    Leetcode题 257. Binary Tree Paths
    Django---Form表单
    Python---面向对象(二)
    Python---面向对象(一)
    Django---Cookie && Session && 分页
    [ Day51 ]Python之路----JavaScript --DOM操作
  • 原文地址:https://www.cnblogs.com/robin1998/p/6611021.html
Copyright © 2011-2022 走看看