HDU 5358 (2015 Multi-school Joint Training tournament 1006) first one (interval merge + constant optimization)

Source: Internet
Author: User

HDU 5358

Test instructions

Beg ∑ i = 1 n ? ∑? J=i ? N ??(?Lo g? 2 ??S(I,J)?+1)?(I+J).

Ideas:

S (i,j) < 10^10 < 2^34, so the value of log2 (S) +1 is only possible between 1~35. Due to the slow function of log, we can compute the interval of the same log value by S (i,n), and use the pos[i][j] preprocessing to record the right boundary of each interval where I is the starting point and log (s) value is j to optimize the complexity of the NLOGN.

Mainly write it is more difficult to some, some details more tangled, a certain way of thinking to clarify and then write.

PS. This card constant is not human, you must remember to preprocess the interval mapping, otherwise n (logn) ^2 also have to kneel.


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)) #define RISE (I,A,B) for (int i = A; I < = b; i++) using namespace Std;const double eps (1e-8); typedef long LONG lint;const int MAXN = 100000 + 5;lint A[maxn];lint pos[ma Xn][34];lint two[34] = { 2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,167 77216,33554432,67108864,134217728,268435456,536870912,1073741824,2147483648,4294967296,8589934592,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 <, i++) {Lint sum = a[1];int right = 1;for (int j = 1; J <= N; j + +) {if (J! = 1) Sum-= A[j-1];w Hile (Sum < two[i] && right <= n) sum + = A[++right];p os[j][i] = right;//start with J, Log2 (sum+1 Lint ans = 0;for (int i = 1; I <= n; i++) {Lint L = i;lint r;for (int j = 0; J < G; J + +) {r = Pos[i][j];ans + = (1+j ) * (((i) * (r-l) + (R+L-1) * (R-L)/2);//Add the same log value interval (i+j) up L = r;}} cout << ans << endl;} return 0;}





Copyright notice: Bo Master said authorized all reproduced:)

HDU 5358 (2015 Multi-school Joint Training tournament 1006) first one (interval merge + constant optimization)

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.