HDU 5147 Sequence II (tree-like array)

Source: Internet
Author: User
Tags cas

Title Link: Hdu 5147 Sequence II

preprocessing each location as B and C can be composed of logarithms, and then enumeration B's position is computed.

#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;typedef long ll; const int MAXN = 50005;int N, ARR[MAXN], FENW[MAXN], LEF[MAXN], RIG[MAXN]; #define LOWBIT (x) ((x) & (-X)) void Add (int x, int v) {while (x <= N) {fenw[x] + = v;x + lowbit (x);}} int sum (int x) {int ret = 0;while (x) {ret + = fenw[x];x-= lowbit (x);} return ret;} int main () {int cas;scanf ("%d", &cas), while (cas--) {scanf ("%d", &n), memset (fenw, 0, sizeof (FENW)), and for (int i = 1 ; I <= N; i++) scanf ("%d", &arr[i]); ll ans = 0, tmp = 0;for (int i = 1; I <= N; i++) {lef[i] = SUM (Arr[i]); Rig[i] = n-i-AR R[i] + lef[i] + 1;add (Arr[i], 1); TMP + = Rig[i];} for (int i = 1; I <= N; i++) {tmp-= Rig[i];ans + tmp * Lef[i];} printf ("%i64d\n", ans);} return 0;}


HDU 5147 Sequence II (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.