Revenge of Segment Tree

Problem Description
In 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 the sum of all continuous sub-sequences of a given number sequence.
 
Input
The 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
 
Output
For each test case, output the answer mod 1 000 000 007.
 
Sample Input
2
1
2
3
1 2 3
 
Sample Output
2
20
Hint
For 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.
 
Source

解题思路:

给定n个数的数列,求全部连续区间的和。。最简单的一道题,智商捉急啊,没想到。。

对于当前第i个数(i>=1),我们仅仅要知道有多少个区间包含a[i]就能够了,答案是 i*(n-i+1), i代表第i个数前面有多少个数,包含它自己。(n-i+1)代表第i个数后面有多少个数,包含它自己,然后相乘,代表前面的标号和后边的标号两两配对。

如图:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3JfMTk5MzA4Mjk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

上图数列取得不恰当,标号和数正好相等。比方数列1,4,2。4。5。图也是和上图一样的。

关键的是标号,而不是详细的数。

代码:

#include <iostream>
#include <stdio.h>
using namespace std;
#define ll long long
const 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;
}

注意输出 I64

[ACM] HDU 5086 Revenge of Segment Tree(全部连续区间的和)的更多相关文章

  1. hdu 5086 Revenge of Segment Tree(BestCoder Round #16)

    Revenge of Segment Tree                                                          Time Limit: 4000/20 ...

  2. HUD 5086 Revenge of Segment Tree(递推)

    http://acm.hdu.edu.cn/showproblem.php?pid=5086 题目大意: 给定一个序列,求这个序列的子序列的和,再求所有子序列总和,这些子序列是连续的.去题目给的第二组 ...

  3. HDU5086——Revenge of Segment Tree(BestCoder Round #16)

    Revenge of Segment Tree Problem DescriptionIn computer science, a segment tree is a tree data struct ...

  4. hdu5086——Revenge of Segment Tree

    Revenge of Segment Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  5. HDU5086:Revenge of Segment Tree(规律题)

    http://acm.hdu.edu.cn/showproblem.php?pid=5086 #include <iostream> #include <stdio.h> #i ...

  6. BestCoder#16 A-Revenge of Segment Tree

    Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...

  7. hdu 5086(递推)

    Revenge of Segment Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  8. HDU 1542/POJ 1151 Atlantis (scaning line + segment tree)

    A template of discretization + scaning line + segment tree. It's easy to understand, but a little di ...

  9. Segment Tree Beats 区间最值问题

    Segment Tree Beats 区间最值问题 线段树一类特殊技巧! 引出:CF671C Ultimate Weirdness of an Array 其实是考试题,改题的时候并不会区间取最值,区 ...

随机推荐

  1. A - Beautiful Matrix

    Problem description You've got a 5 × 5 matrix, consisting of 24 zeroes and a single number one. Let' ...

  2. Unity引擎GUI之Canvas和EventSystem

    最近想写一套关于UGUI所有控件的基础使用教程系列,主要是根据本人的使用心得以及部分测试附带字面翻译来写的,所以其中可能难以避免会有不正确的地方. 好了进入主题,既然是第一篇,我觉得我有必要先介绍一下 ...

  3. php基础知识 书写格式

    PHP,是英文超文本预处理语言Hypertext Preprocessor的递归缩写.PHP 是一种 HTML 内嵌式的语言,是一种在服务器端执行的嵌入HTML文档的脚本语言. php嵌入页面的标记有 ...

  4. REST、RESTful、SOA

    1.http://www.imooc.com/article/17650 2.SOA面向服务架构

  5. 5、scala数组转换

    1.使用yield和函数式编程转换数组 2.算法案例:移除第一个负数之后的所有负数 1.使用yield和函数式编程转换数组 使用yield进行数组转换 结合if守卫,仅转换需要转换的元素 使用函数式编 ...

  6. Canopy聚类算法分析

          原文链接:http://blog.csdn.net/yclzh0522/article/details/6839643 Canopy聚类算法是可以并行运行的算法,数据并行意味着可以多线程进 ...

  7. spring实现helloWord

    第一步:添加架包 第二步:写一个简单的实列 package com.java.test; /** * @author nidegui * @create 2019-06-22 10:58 */ pub ...

  8. Python基础(二)数据类型

    (一)数字 Python3中的数字类型分为3种,分别是整型,浮点型以及复数. Python2种的数字类型分为4种,分别是整型,长整型,浮点型以及复数. 其中长整型时Python2为应对位数较大的而设置 ...

  9. wafII笔记

    wafII笔记:    组件的使用方法:        组件属性:                 属性的设置和获取通过option方法来完成 waf("#id").wafProm ...

  10. 51nod1117 聪明的木匠【贪心+优先队列】

    一位老木匠需要将一根长的木棒切成N段.每段的长度分别为L1,L2,......,LN(1 <= L1,L2,-,LN <= 1000,且均为整数)个长度单位.我们认为切割时仅在整数点处切且 ...