Logarithmic tree array for i<j<k && A[i]>a[j]>a[k] in three-dimensional reverse order codeforces 61E Enemy is weak

Source: Internet
Author: User

Http://codeforces.com/problemset/problem/61/E

E. Enemy is weaktime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

The Romans had attacked again. This time they was much more than the Persians but Shapur was ready to defeat them. He says: "A lion is never afraid of a hundred sheep".

Nevertheless Shapur have to find weaknesses in the Roman army to defeat them. So he gives the army a weakness number.

In Shapur's opinion the weakness of an army are equal to the number of triplets i,? J,? k such that i? < J. < k and ai; a J.? a k where ax Is the power of man standing at positionx. The Roman Army has a special trait-powers of all the people in it is distinct.

Help Shapur find out how weak the Romans is.

Input

The first line of input contains a single numberN(3?≤? n? ≤?106 )-the number of men in Roman army. Next Line containsNDifferent positive integers ai (1?≤? i? ≤? n,? 1?≤? a i? ≤?109 )-powers of men in the Roman army.

Output

A single integer number, the weakness of the Roman army.

%lld specificator to read or write 64-bit integers in C + +. It's preffered to use cout (also).

Sample Test (s) input
33 2 1
Output
1
Input
32 3 1
Output
0
Input
410 8 3 1
Output
4
Input
41 5 4 3
Output
1

Test instructions is the logarithm of i<j<k && A[i]>a[j]>a[k]

If there's only 2 tuples, that's the way to find the reverse number.

Triples, use a tree-like array x to indicate how many of the numbers I have in front of me are larger than the number I have.

Then each time the Y array is summed and the number of >a[i] in x is stored in Y

Even if it's 4 dollars, you can ask for it.

#include <algorithm> #include <cctype> #include <cassert> #include <cstdio> #include < cstring> #include <climits> #include <vector> #include <iostream>using namespace std; #define LL Long longinline Void Rd (int &ret) {char C;do {c = GetChar ();} while (C < ' 0 ' | | c > ' 9 '), ret = C-' 0 '; while (c=g Etchar ()) >= ' 0 ' && C <= ' 9 ') ret = ret * + (C-' 0 ');} #define N 1000005#define EPS 1e-8#define inf 1000000ll n;struct node{ll c[n];inline ll Lowbit (ll x) {return x&-x;} void Init () {memset (c, 0, sizeof c);} ll sum (ll x) {ll ans = 0;while (x<=n+10) ans + = c[x], x+=lowbit (x); return ans;} void Change (ll x, ll y) {while (x) c[x] +=y, x-=lowbit (x);}} X, Y;int haifei[1000000], Panting[1000000];int main () {ll I, J;while (cin>>n) {ll ans = 0;for (i = 0; i < n; i++) Rd (H Aifei[i]), panting[i] = Haifei[i];x.init (); Y.init (); Sort (Haifei, haifei+n); for (i = 0; i < n; i++) {ll b = (Lower_bound (Haifei, Haifei+n, Panting[i])-Haifei) +1;l L Siz= Y.sum (b); ans + = Siz;y.change (b, x.sum (b)); X.change (b, 1);} Cout<<ans<<endl;} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Logarithmic tree array for i<j<k && A[i]>a[j]>a[k] in three-dimensional reverse order codeforces 61E Enemy is weak

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.