Numeric tagging issues offline + Tree array HDU 3938

Source: Internet
Author: User

To give you an array of length n, define the Val of the interval [l,r] as the sum of all the different values in the interval. Now there are M queries, each asking for an interval, asking what is the Val of the interval.

Idea: Sort all queries according to the right endpoint. Then the violent enumeration of the right interval, and then the previous occurrence of the Val to make a mark, each time the tag is updated. Concrete and HDU 58,691 kind, only 5869 also have to preprocess, comparatively difficult

//see if it explodes. int! array will not be one dimension less! //You must be careful about the conditions of the winning.#include <bits/stdc++.h>using namespacestd;#defineLL Long Long#defineAll (a) A.begin (), A.end ()#definePB Push_back#defineMk Make_pair#defineFi first#defineSe SecondConst intMAXN =50000+5;Const intMAXM =200000+5;Const intMaxval =1000000+5; LL TREE[MAXN], A[MAXN], Ans[maxm];vector<pair<int,int> >Q[MAXN];intN, M;intPre[maxval];inlineintLowbit (intx) {returnX &-x;}voidUpdateintXintval) {     for(inti = x; I <= N; i + =lowbit (i)) Tree[i]+=Val;} LL sum (intx) {LL ans=0;  for(inti = x; i >0; I-=lowbit (i)) {ans+=Tree[i]; }    returnans;}intMain () {intT CIN >>T;  while(t--) {memset (PRE,0,sizeof(pre)); memset (Tree,0,sizeof(tree)); scanf ("%d", &N);  for(inti =1; I <= N; i++) {scanf ("%i64d", A +i);        Q[i].clear (); } scanf ("%d", &m);  for(inti =1; I <= m; i++){            intL, R; scanf"%d%d", &l,&R);        Q[R].PB (MK (L, i)); }         for(inti =1; I <= N; i++){            if(Pre[a[i]]) {update (pre[a[i]),-1*A[i]); } Pre[a[i]]=i;            Update (I, a[i]); intLen =q[i].size ();  for(intj =0; J < Len; J + +) {pair<int,int> P =Q[i][j]; Ans[p.second]= SUM (i)-SUM (P.first-1); }        }         for(inti =1; I <= m; i++) {printf ("%i64d\n", Ans[i]); }    }    return 0;}
View Code

Numeric tagging issues offline + Tree array HDU 3938

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.