Poj 3270/HDU 2838-cow sorting

Source: Internet
Author: User

 

Question address: http://poj.org/problem? Id = 3270

 

Note that some of the requirements_ Int64, The other part is ignored (think it is impossibleInt). ResultWaNowNLong unknown reason.

 

This is a classic question of reverse order. Beginners. There is one more State than the basic number of reverse orders. That is, you need to write one more query.

 

 

# Include <iostream> # include <cstdio> using namespace STD ;__ int64 tree [100100] ;__ int64 num [100100]; int n ;__ int64 s [100100]; __int64 query (_ int64 Val) {__ int64 sum = 0; while (Val> 0) {sum + = tree [Val]; Val-= Val &-val ;} return sum; }__ int64 query2 (_ int64 Val) {__ int64 sum = 0; while (Val> 0) {sum + = num [Val]; val-= Val &-val;} return sum;} void Update (INT Val) {__ int64 temp = val; while (Val <= N) {tree [Val] + = temp; num [Val] ++; Val + = Val &-V Al ;}} int main () {int I ;__ int64 sum = 0; while (~ Scanf ("% d", & N) {sum = 0; for (I = 1; I <= N; I ++) tree [I] = num [I] = 0; for (I = 1; I <= N; I ++) scanf ("% i64d ", & S [I]); for (I = N; I> = 1; I --) {sum + = query (s [I]-1) + query2 (s [I]-1) * s [I]; // printf ("% d \ n", s [I]); update (s [I]); // cout <"Tree" <":"; for (Int J = 1; j <= N; j ++) printf ("% 3i64d", tree [J]); cout <Endl; // cout <"num" <":"; for (Int J = 1; j <= N; j ++) printf ("% 3d", num [J]); cout <Endl ;}printf ("% i64d \ n ", sum);} 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.