hdu--3333 (tree-like array + offline processing)

Source: Internet
Author: User

Turing Tree
Time Limit: 3000MS Memory Limit: 32768KB 64bit IO Format: %i64d &%i64u

Submit Status

Description

After inventing Turing tree, 3xian @ felt boring when solving problems about intervals, because Turing tree could eas Ily has the solution. As well, wily 3xian made lots of the 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 is to caculate the sum of distinct values in the subsequence Ai, ai+1, ..., Aj.

Input

The first line was an integer T (1≤t≤10), indecating the number of testcases below.
For each case, the input format would be as 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

Sample Output

The method is very magical, offline processing, offline after each query by the right end of the interval sorting, constantly inserted bit, if ever appeared, so that the original position of 0 is not seen, will present the position plus this number representation appears, WA several times, the sort of time did not pay attention to the wrong, the range did not pay attention to, Both the C and ans arrays should be a long long range. Code:
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include < algorithm> #include <string> #include <cmath> #include <queue> #include <vector> #include <map> #include <set> #define INF 0x3f3f3f3f#define Mem (A, B) memset (A,b,sizeof (a)) using namespace Std;const int maxd=30000+10;const int maxn=100000+5; #define Lson l,m,rt<<1#define Rson m+1,r,rt<<1 | 1typedef Long Long ll;typedef pair<int,int> PII, #define EPS 1e-10using namespace std;//---------------------    typedef long LONG Ll;int a[maxd];ll c[maxd];int op,n,l,r;struct node{int l,r;    int id;    ll _sum;        BOOL operator< (Node A) const {if (R!=A.R) return r<a.r;    Return l<a.l; }};node qq[maxn];ll ans[maxn];map<int,int> vis;int lowbit (int x) {return (-X) &x;}    ll sum (int x) {ll ret=0;        while (x>0) {ret+=c[x];    X-=lowbit (x); } return ret;} void Add (int x,int d) {while (X&Lt;=n) {c[x]+=d;    X+=lowbit (x);    }}int Main () {int kase;    Freopen ("1.txt", "R", stdin);    scanf ("%d", &kase);        while (kase--) {scanf ("%d", &n);        MEM (c,0);        Vis.clear ();        for (int i=1; i<=n; ++i) scanf ("%d", &a[i]);        scanf ("%d", &op);        for (int i=1; i<=op; ++i) scanf ("%d%d", &AMP;QQ[I].L,&AMP;QQ[I].R), qq[i].id=i;        Sort (QQ+1,QQ+1+OP);        qq[0].l=0,qq[0].r=0;            for (int i=1; i<=op; ++i) {int ll=qq[i].l,rr=qq[i].r;                    for (int j=qq[i-1].r+1; j<=qq[i].r; ++j) {if (Vis.find (A[j])!=vis.end ()) {                Add (Vis[a[j]],-a[j]);                } Add (J,a[j]);            Vis[a[j]]=j;        } ans[qq[i].id]=sum (RR)-sum (LL-1);    } for (int i=1; i<=op; ++i) printf ("%lld\n", Ans[i]); } return 0;}


hdu--3333 (tree-like array + offline processing)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.