[ACM] HDU 5086 revenge of segment tree (sum of all consecutive intervals)

Source: Internet
Author: User

Revenge Of segment tree

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 Input
21231 2 3
 
Sample output
220HintFor 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 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


Solution:

Returns the sum of all consecutive intervals for a series of N numbers .. The simplest question is that IQ is anxious. I didn't expect it ....

For the current number of I (I> = 1), we only need to know how many intervals contain a [I]. The answer is I * (N-I + 1 ), I Represents the number before the number of I, including itself, (n-I + 1) represents the number after the number of I, including itself, and then multiply, it indicates that the front and back labels are paired with each other.


The number of numbers is incorrect. Numbers and numbers are exactly the same. For example, numbers 1, 4, 2, 4, and 5 are the same. The key is the number, not the specific number.

Code:

#include <iostream>#include <stdio.h>using namespace std;#define ll long longconst ll mod=1000000007;ll n;int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%I64d",&n);        ll x;        ll ans=0;        for(ll i=1;i<=n;i++)        {            scanf("%I64d",&x);            ans=(ans+i*(n-i+1)%mod*x%mod)%mod;        }        printf("%I64d\n",ans);    }    return 0;}

Note that i64 is output

[ACM] HDU 5086 revenge of segment tree (sum of all consecutive intervals)

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.