HDU 5419--victor and Toys —————— "segment Tree | differential prefix and"

Source: Internet
Author: User

Victor and Toys

Time limit:2000/1000 MS (java/others) Memory limit:262144/131072 K (java/others)
Total submission (s): 654 Accepted Submission (s): 219


Problem Descriptionvictor hasNToys, numbered from1ToN. The beauty of theI-th Toy isWi .

Victor has a sense of math and he generatesmintervals, theI-th interval is[li,RI] . He randomly picks3Numbersi,J,K(1≤i<J<k≤m) , and selects all of the toys whose number is no less thanMax(li,lJ,lk) And no larger than min(rI,RJ,RK) . Now he wants to know the expected sum of beauty of the selected toys, can I help him?

Inputthe first line of the input contains an integerT, denoting the number of test cases.

In every test case, there is and integersNandmIn the first line, denoting the number of the toys and intervals.

The second line containsNIntegers, theI-th integerWi Denotes that the beauty of theI-th toy.

Then there ismLines, theI-th line contains and integerslI andrI .

1≤T≤ .

1≤n,m≤50000 .

1≤wi≤5 .

1≤li≤ri≤n.

Outputyour program should printTLines:theI-th of these denotes the answer of theI-th case.

If the answer is an integer, just print a single interger, otherwise print an irreducible fraction like p/ q .

Sample Input13 41 1 52 31 33 31 1

Sample OUTPUT5/4

Sourcebestcoder Round #52 (Div.2)    topic description:  Problem Solving ideas 1: (differential prefix and) preprocessing out S[i] array, which indicates how many intervals each toy is within. E=sigma (XI*PI). Here Xi is the interesting value, pi is C (s[i],3)/C (m,3). So the key to this problem is to deal with it s[i]. At the same time attention posture graceful, don't explode long long. As for the difference prefix and, in fact, it is a clever array to deal with the offline interval problem, for M interval, in the interval left end of the Li Place +1, in the interval right end of the RI Place-1. At the end of the prefix and processing, the complexity of n can get the number of the first toy in the range.   
#include <bits/stdc++.h>using namespace std;typedef __int64 int;const INT maxn=55000;int a[maxn],s[maxn];int cal (    INT nn) {if (nn<3) return 0; Return (nn-2) * (nn-1) *NN/6;} int GCD (int a,int b) {return b==0?a:gcd (b,a%b);}    int main () {int t,n,m,li,ri;    scanf ("%d", &t);        while (t--) {memset (s,0,sizeof (s));        scanf ("%d%d", &n,&m);        for (int i=1;i<=n;i++) scanf ("%d", &a[i]);            for (int i=1;i<=m;i++) {//Differential scanf ("%d%d", &li,&ri);        s[li]++;s[ri+1]--; } for (int i=1;i<=n;i++) {//prefix and.            The value in the S array is the number of intervals within which the first toy is.        S[I]+=S[I-1];        } INT Fm,fz;        fz=0;        for (int i=1;i<=n;i++) {fz+=cal ((int) s[i]) *a[i];            } if (m<3) {puts ("0");        Continue        } fm=cal (M);        if (fz==0) {printf ("0\n", FM);            }else {INT gcd=gcd (FZ,FM);            FZ/=GCD,FM/=GCD;   if (fm==1)             printf ("%i64d\n", FZ);        else printf ("%i64d/%i64d\n", FZ,FM); }} return 0;}

  

HDU 5419--victor and Toys —————— "segment Tree | differential prefix and"

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.