HDU 3874 Necklace (tree-like array)

Source: Internet
Author: User
Tags integer numbers

Problem Descriptionmery has a beautiful necklace. The necklace is a made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value is the same, so if the and more balls has the same beautiful value, we just Coun T it once. We define the beautiful value of some interval [x, y] as F (x, y). F (x, y) is calculated as the sum of the beautiful value from the Xth ball to the Yth ball and the same value are only Counte D ONCE. For example, if the necklace are 1 1 1 2 3 1, we have f (1,3) =1, F (2,4) =3, F (2,6) =6.

Now Mery thinks the necklace is too long. She plans to take some continuous part of the necklace to build a new one. She wants to know each of the beautiful value of M continuous parts of the necklace. She'll give you M intervals [l,r] (1<=l<=r<=n) and your must tell her F (l,r) of them.
Inputthe First line is T (t<=10), representing the number of test cases.
For each case, the first line is a number n,1 <=n <=50000, indicating the number of the magic balls. The second line contains n non-negative integer numbers not greater 1000000, representing the beautiful value of the N Bal Ls. The third line has a number M, 1 <=m <=200000, meaning the nunber of the queries. Each of the next M lines contains L and R, the query.
Outputfor each query, output a line contains an integer number, representing the result of the query.
Sample Input
261 2 3 4 3 531 23 52 661 1 1 2 3 531 12 43 5

Sample Output
3714136

Test instructions: give you a bunch of numbers that let you ask for the and in the interval [l,r], asking for repetition of numbers only once.

Tree-like array approach: you will certainly think of offline, but after the offline sort of how to arrange, here is a thought:

Because to go to the heavy, we consider to ask the right interval from small to large sort, for the inquiry, we remove the front

A duplicate value that retains only the current.

#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <string > #include <iostream> #include <queue> #include <cmath> #include <map> #include <stack>  #include <bitset>using namespace std; #define REPF (I, A, b) for (int i = A; I <= B; + + i) #define REP (i, n ) for (int i = 0; i < n; + + i) #define CLEAR (A, X) memset (A, x, sizeof a) typedef long long Ll;const int max n=50000+100;const int maxm=200000+100;    LL C[MAXN], ans[maxm];int pre[maxn],hash[1000000+10];struct node{int l,r; int id;} Q[maxm];int num[maxn];int t,n,m;int lowbit (int x) {return x& (-X);}        void Update (int x,ll W) {while (X&LT;MAXN) {c[x]+=w;    X+=lowbit (x);    }}LL query (int x) {LL s=0;        while (x>0) {s+=c[x];    X-=lowbit (x); } return s;} int CMP (node L1,node L2) {return L1.R&LT;L2.R;}    int main () {int x, y;    scanf ("%d", &t); while (t--) {CLEAR (hash,-1);       CLEAR (c,0);        scanf ("%d", &n);            REPF (i,1,n) {scanf ("%i64d", &num[i]);            Pre[i]=hash[num[i]];            Hash[num[i]]=i;        Update (i,num[i]);        } scanf ("%d", &m);            REP (i,m) {scanf ("%d%d", &x,&y);            Q[i].l=x;q[i].r=y;        Q[i].id=i;        } sort (q,q+m,cmp);        int rr=0; REP (i,m) {for (int j=rr+1;j<=q[i].r;j++) {if (pre[j]!=-1)//horizontal slot, preserving only the current position of num[j            ], the essence of update (PRE[J],-NUM[J]);            } RR=Q[I].R;        Ans[q[i].id]=query (Q[I].R)-query (Q[I].L-1);    } REP (I,m) printf ("%i64d\n", Ans[i]); } return 0;}


HDU 3874 Necklace (tree-like array)

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.