Revenge Of segment tree
Time Limit: 4000/2000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 845 accepted submission (s): 302
Problem descriptionin computer science, a segment tree is a tree data structure for storing intervals, or segments. it allows querying which of the stored segments contain a given point. it is, in principle, a static structure; that is, its content cannot be modified once the structure is built. A similar data structure is the Interval Tree.
A segment tree for a set I of N intervals uses O (n log n) Storage and can be built in O (n log n) time. segment Trees Support searching for all the intervals that contain a query point in O (log n + k), K being the number of retrieved intervals or segments.
--- Wikipedia
Today, segment tree takes revenge on you. as segment tree can answer the sum query of a interval sequence easily, your task is calculating the sum of all continuous sub-sequences of a given number sequence.
Inputthe first line contains a single integer T, indicating the number of test cases.
Each test case begins with an integer N, indicating the length of the sequence. Then n integer AI follows, indicating the sequence.
[Technical Specification]
1. 1 <= T <= 10
2. 1 <= n <= 447 000
3. 0 <= AI <= 1 000 000 000
Outputfor each test case, output the answer MOD 1 000 000 007.
Sample input21231 2 3
Sample output220
HintFor the second test case, all continuous sub-sequences are [1], [2], [3], [1, 2], [2, 3] and [1, 2, 3]. so the sum of the sub-sequences is 1 + 2 + 3 + 3 + 5 + 6 = 20. huge input, faster I/O method is recommended. and as N is rather big, too straightforward algorithm (for example, O (N ^ 2) will lead time limit exceeded. and one more little helpful hint, be careful about the overflow of Int.
Sourcebestcoder round #16
# Include <cstdio> # include <iostream> # define mod 1000000007 using namespace STD ;__ int64 a [500000] ;__ int64 sum; int main () {__ int64 I, j, N; int t; scanf ("% d", & T); While (t --) {scanf ("% i64d", & N); sum = 0; for (I = 1; I <= N; I ++) {scanf ("% i64d", & A [I]); sum + = A [I] * (I * (N-I + 1) % mod ); // note that the length is too long here, so the remainder sum % = MOD;} printf ("% i64d \ n", sum);} return 0;} must be obtained internally ;}
HDU 5086 revenge of segment tree