[Tree array] HDU 4000

Source: Internet
Author: User

1 ~ N sequence, and how many groups of x <z <Y

Method: Tree array, first obtain the number of xyz + xzy, very simple, calculate the number larger than AI, the number smaller than Ai, this is achieved using a tree array, at the beginning, I thought it was done backwards, I .e., subscript n ~ 1. If the sequence is not necessary, you can find a number P smaller than that of AI, and calculate it on the draft paper, calculate the number larger than AI (n-I-a [I] + p) {calculate this subprocess: The number smaller than AI is P, it is reasonable to say that there is a small number of ai-1 than Ai, then there must be a ai-1-p smaller than AI in the right side of the AI, occupying a ai-1-p position, then the right side of AI is left N-(I + 1) space, so the right side is greater than the number of ai n-(I + 1)-(ai-1-p) = (n-I-a [I] + p). Note that my subscript starts from 0}. After finding The subscripts that are larger than AI and smaller than Ai, you can calculate them.

#include <map>#include <set>#include <list>#include <queue>#include <deque>#include <stack>#include <string>#include <time.h>#include <cstdio>#include <math.h>#include <iomanip>#include <cstdlib>#include <limits.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;#define LL long long#define PI acos(-1.0)#define FRE freopen("a.txt","r",stdin)#define MAX INT_MAX#define MIN INT_MIN#define eps 1e-10#define MOD 100000007#define N  100005LL c[N];int a[N];int lowbit(int t){    return t&(-t);}LL sum(int x){    LL tot=0;    while(x){        tot+=c[x];        x-=lowbit(x);    }    return tot;}void add(int x){    while(x<N){        c[x]+=1;        x+=lowbit(x);    }}int main(){FRE;    int t;    scanf("%d",&t);    int ca;    for(ca=1;ca<=t;ca++){        //memset(c,0,sizeof(c));        int n;        scanf("%d",&n);        int i,j;        for(i=0;i<=n;i++)c[i]=0;        for(i=0;i<n;i++)scanf("%d",&a[i]);        LL s1=0,s2=0;        for(i=0;i<n;i++){            LL p=sum(a[i]);//the number less than a[i]            s2+=(n-i-a[i]+p)*p;//x<y<z            s1+=(n-i-a[i]+p)*(n-i-a[i]+p-1)/2;            add(a[i]);        }        printf("Case #%d: %I64d\n",ca,(s1-s2)%MOD);    }    return 0;}
Related Article

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.