Hoj 2275 number sequence (tree-like array)

Source: Internet
Author: User

Test instructions: Given a sequence of n elements, make AI, AJ, AK, make AI < AJ > AK and I < J < K three numbers as a group. Find out how many groups there are.
idea: You can use a tree-like array, each time you enter an AI, you can query to the previous input than it is smaller or larger than the number of groups, after the input is not known, so you can open the number of records down in reverse order and then build once canThe range of the other number is taken to 0, so each number is to be added once, and the number of groups is Longlong, and the Hoj server system supports LLD, not i64d. WA a few times
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream>using namespace STD; #define M 35000#define N 50050#define ll Long long#define lowbit (x) (x&-x) int tree[m];int num[n];ll tmp[n];int sum (int RT)    {int s=0;        while (rt>0) {S+=TREE[RT];    Rt-=lowbit (RT); } return s;}        void update (int rt) {while (rt<=m-1) {tree[rt]++;    Rt+=lowbit (RT);    }}int Main () {int n;        while (~SCANF ("%d", &n)) {memset (tree,0,sizeof (tree));            for (int i=1;i<=n;i++) {scanf ("%d", &num[i]);        num[i]++;        } ll Ans=0;            for (int i=1;i<=n;i++) {tmp[i]= sum (num[i]-1);        Update (num[i]);        } memset (Tree,0,sizeof (tree));            for (int i=n;i>=1;i--) {ans+=tmp[i]* (ll) sum (num[i]-1);        Update (num[i]);    } printf ("%lld\n", ans); } return 0;}


Hoj 2275 Number sequence (tree-like 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.