POJ3928 and LA4329 tree Arrays

Source: Internet
Author: User

Use this question to test the effects of various practices ~

This is a question entitled "ACM2008" on the Beijing site. It is between simple and moderate. It is not enough for the penalty. Therefore, such a question must be answered.

After adding (a [I] +) is processed in this way, sum (a [I]) is used to calculate a smaller number than a [I. For more information, see the tree chart.

Tree array exceptions ~

 

The change of the tree array after add (a [I], 1) may be very complicated ..

 

Hmm !! Understand.

 

If you change to add (a [I], 1), the result is converted to sum (a [I])-1; this is equivalent to adding 1 at a [I, the following is equivalent to add (a [I] + ~~, However, the time consumption will increase by 30 + MS.

# Include <iostream> # include <stdio. h> # include <string. h> using namespace std; const int maxn = 100005; long c [maxn]; int a [20005]; long temp [maxn]; int lowbit (int x) {return x &-x;} void add (int x, int y) {while (x <= maxn) {c [x] + = y; x + = lowbit (x) ;}long long sum (int x) {long ret = 0; while (x> 0) {ret + = c [x]; x-= lowbit (x);} return ret;} int main () {int case_num; scanf ("% d", & case_nu M); while (case_num --) {memset (c, 0, sizeof (c); int n; scanf ("% d", & n ); for (int I = 1; I <= n; I ++) {scanf ("% d", & a [I]); add (a [I] +); // try an array with only 0 values and the result is similar, replace + 1 with + x [I] temp [I] = sum (a [I]); // This place minus c [x ..} Long ans = 0; for (int I = 1; I <= n; I ++) printf ("% d", sum (a [I]); printf ("\ n"); for (int I = 2; I <n; I ++) {ans + = temp [I] * (n-I-(sum (a [I])-temp [I]); // In fact the first sum (a [I])-temp [I] is not quite sure ans + = (i-1-temp [I]) * (sum (a [I]) -temp [I]); // my sum (a [I]) actually includes itself (not included)} printf ("% lld \ n", ans );} return 0 ;}

 

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.