Hihocoder 1488: Line up water (Mo Team + Tree array)

Source: Internet
Author: User

Describe

There are n children need to pick up water, among them the first child to pick up water needs AI minutes.

Because the faucet is limited, little hi needs to know if you assign a faucet to the first and second children, how to arrange their water order to minimize the amount of time they have to wait for the added water.

Little Hi will have a total of M inquiry, can you help him solve this problem?

Let's say 3 children are 2,3,4 when they pick up water. If they pick up the water in turn, the first child to wait for the water is 2, the second child is 5, the third child is 9. The sum of time is 16.

Input

The first line is a number T (t<=10), which indicates the number of data groups

For each set of data:

First row two numbers n,m (1<=n,m<=20,000)

Second row n number a1...an, indicating the time required for each child to receive water (ai<=20,000)

Next m line, two numbers L and R per line

Output

For each query, the output is an integer that represents the answer.

Sample input

14 21 2 3 41 22 4

Sample output

416

Idea: greedy know, the time is small in front. However, sorting is not possible and requires a more efficient approach, noting that ai<=2e5, which is suitable for using a tree-like array to record the number of a[i] prefixes and prefixes of a[i]. Can be offline, so with MO Team + tree-like array, MO team words, the first time to write this mathematical type of transfer, began to have a little conflict, but take out a pen strokes, formula is not difficult.

For the formula of violence, that is, the prefix of all I l<=i<=r and:
 for (i=l;i<=r;i++for (j=l;j<=i;j++)  sum+=a[j];
Now add a i=x to go in, then the new i=x, need to multiply the prefix:
 for (j=l;j<=x;j++)  sum
For the subsequent i>=x, you need to accumulate a j=x, that is, the number behind the a[x]*.
 for (i=x;i<=r;i++for (j=l;j<=i;j++)  sum
Then convert the above into a tree-shaped array of prefixes and can. Two tree-like arrays, recording the number and sum of each.

#include <cmath>#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespacestd;#definell Long LongConst intmaxn=20000; ll A[maxn+Ten],num[maxn+Ten],cnt,b,l,r,tmp;ll SUM[MAXN+Ten];struct inch{ll l;ll r;ll id; ll ans;} s[maxn+Ten];BOOLcmpinchXinchY) {if(x.l/b==y.l/b)returnx.r/b<y.r/b;returnx.l/b<y.l/B;}BOOLCMP2 (inchXinchY) {returnx.id<y.id;}voidUpdate () {cnt=0; memset (NUM,0,sizeof(num)); memset (SUM,0,sizeof(sum)); }voidAddnum (ll X,ll y) { while(X&LT;=MAXN) {num[x]+=y; x+= (-X) &x;} }voidAddsum (ll X,ll y) { while(X&LT;=MAXN) {sum[x]+=y; x+= (-X) &x;} }intQuerynum (intx) {ll res=0; while(x>0{res+=num[x]; x-= (-X) &x;}returnRes;}intQuerysum (intx) {ll res=0; while(x>0{res+=sum[x]; x-= (-X) &x;}returnRes;}intMain () {ll t,n,m,i;ll tsum; scanf ("%d",&T);  while(t--) {update (); scanf ("%lld%lld",&n,&m);  for(i=1; i<=n;i++) scanf ("%lld",&A[i]);  for(i=1; i<=m;i++) s[i].id=i,scanf ("%lld%lld", &s[i]. l,&S[i].        R); B=sqrt (n); Sort (S+1, s+m+1, CMP); L=r=1; tmp=a[1]; Addnum (a[1],1); Addsum (a[1],a[1]);  for(i=1; i<=m;i++){             while(l<S[i]. L) {Tsum=querysum (A[l]); TMP-=Tsum; TMP-= (Querynum (MAXN)-querynum (A[l])) *A[l]; Addnum (A[l],-1); Addsum (A[l],-A[l]); L++; }             while(l>S[i]. L) {L--; Addnum (A[l],1);                            Addsum (A[l],a[l]); Tsum=querysum (A[l]); TMP+=Tsum; TMP+ = (Querynum (MAXN)-querynum (A[l])) *A[l]; }             while(r<S[i]. R) {R++; Addnum (A[r],1);                Addsum (A[r],a[r]); Tsum=querysum (A[r]); TMP+=Tsum; TMP+ = (Querynum (MAXN)-querynum (A[r])) *A[r]; }             while(r>S[i]. R) {Tsum=querysum (A[r]); TMP-=Tsum; TMP-= (Querynum (MAXN)-querynum (A[r])) *A[r]; Addnum (A[r],-1); Addsum (A[r],-A[r]); R--; } S[i].ans=tmp; } sort (S+1, s+m+1, CMP2);  for(i=1; i<=m;i++) printf ("%lld\n", S[i].ans); }    return 0;}

Hihocoder 1488: Line up water (Mo Team + Tree 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.