HDU-3874 Necklace (segment tree + offline processing)

Source: Internet
Author: User
Tags integer numbers


Welcome to the Bestcoder-every Saturday night (with rice!) )
NecklaceTime limit:15000/5000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 4003 Accepted Submission (s): 1330


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
3714136for this problem, you can see my blog http://blog.csdn.net/qq_18661257/article/details/47419441 provides offline processing tutorial, basically can understand the mechanism of offline processing, Then we have to understand the topic, can not appear the number of duplicates, so the right-most value offline, the previous value of one by one can be deleted to get the correct answer, of course, we also need to note that the value range is a long long, I was in this view for nearly one hours, query () The return value of the function should also be a long long, the place where the father is here, others, we see the code basically can understand the
#include <cstdio> #include <cstring> #include <algorithm> #include <vector>using namespace std; typedef long long LL; #define Lson RT << 1, L, Mid#define Rson RT << 1|1, Mid + 1, r#define root 1, 1, nconst I NT MAXN = 5e4 + 5;const int maxm = 2e5 + 5;const int maxs = 1e6 + 5;int N, M, T, PRE[MAXS];    LL ANS[MAXM],SUM[MAXN << 2];struct Node {int L, r, id;    BOOL operator < (const node & object) Const {return R < OBJECT.R; }} node[maxm];void pushup (int rt) {Sum[rt] = Sum[rt << 1] + sum[rt << 1|1];}        void build (int rt,int l,int R) {if (L = = r) {scanf ("%i64d", &sum[rt]);    Return    } int mid = (L + r) >> 1;    Build (Lson);    Build (Rson); Pushup (RT);}        void update (int p,int RT, int l, int r) {if (L = = r) {Sum[rt] = 0;    Return    } int mid = (L + r) >> 1;    if (P <= mid) Update (P, Lson);    else Update (P, Rson); Pushup (RT);} LL query (int L, int R, int RT,int l, int r) {if (L <= l && R <= R) {return SUM[RT];    } int mid = (L + r) >> 1;    LL ret = 0;    if (L <= mid) ret + = query (l, R, Lson);    if (R > Mid) ret + = query (L, R, Rson); return ret;}    int main () {//freopen ("D://imput.txt", "R", stdin);    scanf ("%d", &t);        while (T--) {scanf ("%d", &n);        Build (root);        scanf ("%d", &m);            for (int i = 1; I <= M; i + +) {scanf ("%d%d", &AMP;NODE[I].L, &AMP;NODE[I].R);        Node[i].id = i;        } memset (Pre,-1, sizeof (pre));        Sort (node + 1, node + M + 1);            for (int i = 1,J = 1; I <= N; i++) {int tmp = query (I, I, root);            if (tmp! = 0 && pre[tmp]! =-1) {//If there is a duplicate number in front of it, delete his update (pre[tmp],root);            } Pre[tmp] = i; while (J <= M && NODE[J].R = = i) {//If the value on the right is equal to the current value, then you can refer to the tutorial in my blog ans[node[j].id] = query (Node [J].l, NODE[J].R, root);            j + +;        }} for (int i = 1; I <= M; i + +) {printf ("%i64d\n", Ans[i]); }} return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU-3874 Necklace (segment tree + offline processing)

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.