HDU 1394 Minimum Inversion number "segment tree in reverse order"

Source: Internet
Author: User

Previously written in a tree-like array, then use the line tree to write a bit ~ ~ ~

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <cmath>6#include <vector>7 using namespacestd;8 #defineLP (P << 1)9 #defineRP (P << 1 | 1)Ten #defineGetmid (L,r) (L + (R-L)/2) One  A Const intMAXN =50005; - structnode{ -     intl,r,s; the}t[4*MAXN]; -  - intA[maxn],n; -  + intCalcintAintb) {returnA +b;} -  + voidPUSH_UP (intp) { AT[P].S =Calc (t[lp].s,t[rp].s); at } -  - voidBuild_tree (intPintLintR) { -T[P].L =l; -T[P].R =R; -     if(L = = r) {T[P].S =0;return;} in     intMID =Getmid (l,r); - Build_tree (lp,l,mid); toBuild_tree (rp,mid+1, R); + push_up (p); - } the  * voidUpdateintPintSintW) {//add W to s dot $     if(T[P].L = =T[P].R) {Panax NotoginsengT[p].s + =W; -         return; the     } +     intMID =Getmid (T[P].L,T[P].R); A     if(S <=mid) Update (LP,S,W); the     ElseUpdate (RP,S,W); + push_up (P);  - } $  $ intSumintPintLintR) { -     if(T[P].L = = L && T[P].R = = r)returnT[p].s; -      the         intMID =Getmid (T[P].L,T[P].R); -         if(R <= Mid)returnsum (lp,l,r);Wuyi         Else if(Mid < L)returnsum (rp,l,r); the          Else returnCalc (sum (lp,l,mid), SUM (rp,mid+1, R)); - } Wu  - intMain () { About     intT; $      while(SCANF ("%d", &n)! =EOF) { -          for(inti =1; I <= n;i++) scanf ("%d",&A[i]), a[i]; -Build_tree (1,1, n); -          A         intAns =0; +          for(inti =1; I <= n;i++){        theAns + = SUM (1, a[i]+1, n); Update (1, A[i],1); -       //printf ("ans =%d\n", ans); $         } the          the         intres =ans; the          for(inti =1; I <= n;i++){ theAns + = n2*a[i]-1; -res =min (res,ans); in         //printf ("res =%d\n", res); the         } theprintf"%d\n", res); About     } the     return 0; the}
View Code

HDU 1394 Minimum Inversion number "segment tree in reverse order"

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.