HDU 5358 (2015 Multi-school joint training competition 1006) First One (range merging + constant optimization), hdu training competition

Source: Internet
Author: User

HDU 5358 (2015 Multi-school joint training competition 1006) First One (range merging + constant optimization), hdu training competition

HDU 5358

Question:

Calculate Σ I = 1n Σ j = in (⌊ log2S (I, j) records + 1) records (I + j ).

Ideas:

S (I, j) <10 ^ 10 <2 ^ 34, so the value of log2 (S) + 1 can only be 1 ~ Between 35. Due to the slow log changes, we can use S (I, n) to separate Multiple Intervals With the same log value for calculation, the complexity of nlogn can be optimized by using pos [I] [j] preprocessing to record the right boundary of each interval where I is taken as the starting point and log (s) value is j.

It is mainly difficult to write, some details are tangled, and some ideas are clarified before writing.

Ps. The unit constant is unhuman. Remember to pre-process the interval ing. Otherwise, n (logn) ^ 2 will have to kneel down ..


Code:

/** @ Author Novicer * language: C ++/C */# include <iostream> # include <sstream> # include <fstream> # include <vector> # include <list> # include <deque> # include <queue> # include <stack> # include <map> # include <set> # include <bitset> # include <algorithm> # include <cstdio> # include <cstdlib> # include <cstring> # include <cctype> # include <cmath> # include <ctime> # include <iomanip> # define INF 2147483647 # define cls (x) memset (x, 0, sizeof (x) # de Fine rise (I, a, B) for (int I = a; I <= B; I ++) using namespace std; const double eps (1e-8 ); typedef long lint; const int maxn = 100000 + 5; lint a [maxn]; lint pos [maxn] [34]; lint two [34] = {2, 4, 8, 16, 128,256,512,102 934592,17179869184}; int main () {int t; cin> t; while (t --) {int n; cin> n; for (int I = 1; I <= n; I ++) {scanf ("% I64d", & a [I]) ;}for (int I = 0; I <34; I ++) {lint sum = a [1]; int right = 1; for (int j = 1; j <= n; j ++) {if (j! = 1) sum-= a [J-1]; while (sum <two [I] & right <= n) sum + = a [++ right]; pos [j] [I] = right; // starting from j, the right boundary of the range where the value of log2 (sum + 1) is I is right} lint ans = 0; for (int I = 1; I <= n; I ++) {lint l = I; lint r; for (int j = 0; j <34; j ++) {r = pos [I] [j]; ans + = (1 + j) * (I) * (r-l) + (r + L-1) * (r-l)/2); // accumulate (I + j) of the same log value range. l = r ;}} cout <ans <endl ;} return 0 ;}





Copyright statement: the blogger authorizes everyone to reprint everything :)

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.