Links: http://acm.hdu.edu.cn/showproblem.php?pid=3473
Minimum SumTime
limit:16000/8000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 3427 Accepted Submission (s): 787
Problem descriptionyou is given N positive integers, denoted as x0, X1 ... xN-1. Then give some intervals [L, R]. For each interval, you need to find a number x to make as small as possible!
Inputthe first line was an integer t (t <=), indicating the number of test cases. For each test case, an integer n (1 <= n <= 100,000) comes first. Then comes N positive integers x (1 <= x <= $, 000,000) in the next line. Finally, comes an integer q (1 <= q <= 100,000), indicting there is Q queries. Each query consists of integers l, r (0 <= l <= R < N), meaning the interval you should with.
Outputfor The k-th test case, first output ' case #k: ' In a separate line. Then output Q lines, each of which is the minimum value of. Output a blank line after every test case.
Sample Input
253 6 2 2 421 40 227 720 11 1
Sample Output
Case #1:64Case #2:00
Test instructions
Ask l,r interval take any integer x, ask ∑| x-xi| The minimum value
Practice: It is clear that this x is the median. The median thought divides the tree, then divides the tree to build the time, the number of the left subtree to calculate a prefix and, then in the query, if to the right sub-tree, the interval enters the left subtree the number calculates one and. The last statistic is the number of sumlft that are smaller than the median of the interval.
O (log (n)) Find the value of the K-size # include <iostream> #include <cstdio> #include <algorithm>using namespace std;# Define N 100009#define M 19//m>log2,n; #define MID ((l+r) >>1) #define LL __int64int s[n];//s The original data after sort; int t[m ][N];//T is a tree, an int numb[m][n];//and a T array are synchronized, the current layer, the current interval, and N the number of digits before the subscript is classified to the left subtree. int n,m; ll Quzuo[m][n];ll Sum[n]; void Build (int c,int l,int R)//c is the number of layers {int lm=mid-l+1,lp=l,rp=mid+1; the beginning of the left of the//LP RP to the right of the start for (int i=l;i<=mid;i++) lm-=s[i]& Lt;s[mid]; LM represents the current left block (subscript l-mid) in which there is a number equal to S[mid], including its own int zuo,you;for (int i=l;i<=r;i++) {zuo=you=0;if (i==l) {numb[c][i]=0; Quzuo C [I]=0;} ELSE{NUMB[C][I]=NUMB[C][I-1];QUZUO[C][I]=QUZUO[C][I-1];} if (T[c][i]==s[mid]) {if (LM)//left side of how much equal to the median number of the left to go {lm--;zuo=1;} else//if equal to the left block of the median number is used up, put to the right block to you=1; }else if (T[c][i]<s[mid])//small go to left block zuo=1; else you=1; if (Zuo) {numb[c][i]++;t[c+1][lp++]=t[c][i];quzuo[c][i]+=t[c][i];} else{t[c+1][rp++]=t[c][i];}} if (l<r) build (C+1,l,mid), build (C+1,mid+1,r);} __int64 sumlft,sumrit;int Numlft,numrit;int Query (int c,int l,int r,int ql,int qr,int k)//ql and QR are the interval of the query for the left and right boundaries, L and R current. {if (l==r) return t[c][l];int s,ss;if (L==QL) s=0,ss=numb[c][qr];elses=numb[c][ql-1],ss=numb[c][qr]-numb[c][ql-1];if (K<=SS) {return Query (c+1,l,mid,l+s,l+s+ss-1,k);} Else{if (ql==l) {sumlft+=quzuo[c][qr]-0;} else{sumlft+=quzuo[c][qr]-quzuo[c][ql-1];} Return Query (C+1,MID+1,R,MID+1+QL-L-S,MID+1+QR-L-S-SS,K-SS);//SS to left subtree so SS rank in right subtree descending SS}} int main () {int tt;int cas=1; scanf ("%d", &tt), while (tt--) {scanf ("%d", &n), sum[0]=0;for (int i=1;i<=n;i++) {s[i]=i;scanf ("%d", &s[i ]); t[0][i]=s[i];sum[i]=sum[i-1]+ (ll) s[i];} Sort (s+1,s+1+n); Build (0,1,n); scanf ("%d", &m);p rintf ("Case #%d:\n", cas++) and while (m--) {int l,r;sumlft=sumrit=numlft=numrit=0;// When entering the right subtree, the value of the interval to the left subtree is added to the Sumlft scanf ("%d%d", &l,&r);//L-r in the K-large, L starting from 1 c=0, L++;r++;int ans;ans=query (0,1,n,l , R, (r-l)/2+1); numlft= (r-l)/2;numrit=r-l-numlft;sumrit=sum[r]-sum[l-1]-sumlft-(ll) ans; printf ("Zuohe%d Youhe%d Zuoshu%d Youshu%d ans%d \ n", Sumlft,sumrit,numLft,numrit,ans);p rintf ("%i64d\n", (LL) numlft* (LL) ans-sumlft) + (sumrit-(LL) numrit* (ll) ans));} Puts ("");} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Application of HDU 3473 Minimum Sum partition tree