Revenge of Segment Tree

                                                         Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

                                                                                   Total Submission(s): 420    Accepted Submission(s): 180

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
  1. 2
  2. 1
  3. 2
  4. 3
  5. 1 2 3
 
Sample Output
  1. 2
  2. 20
  3. Hint
  4. 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.
  5. 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.
  6. And one more little helpful hint, be careful about the overflow of int.
  7.  
 

求一段序列的全部连续子序列的和。
对于序列中的第i个,下标从0開始。在第ai个前有i+1个数(包含它自己),在ai个后有n-i个(包含它自己),所以ai
共出现(i+1)*(n-i)次。



官方题解:

  1. 考虑每一个数出如今多少个子序列之中。如果第i个数为Ai。区间为[L,R]。
  2.  
  3. 那么包括Ai的区间满足LiRi。累加(L+1)∗(NR)∗A[i]就能够了。
  1. 代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. using namespace std;
  6. long long mod=1000000000+7;
  7. int main()
  8. {
  9.     int t;
  10.     scanf("%d",&t);
  11.     while(t--)
  12.     {
  13.         long long n;
  14.         long long ans=0;
  15.         long long a;
  16.         scanf("%I64d",&n);
  17.         for(int i=0;i<n;i++)
  18.         {
  19.             scanf("%I64d",&a);
  20.             ans=(ans+(((a*(i+1)%mod)*(n-i))%mod))%mod;
  21.         }
  22.         printf("%I64d\n",ans);
  23.     }
  24.     return 0;
  25. }
  26.  


hdu 5086 Revenge of Segment Tree(BestCoder Round #16)的更多相关文章

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

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

  2. [ACM] HDU 5086 Revenge of Segment Tree(全部连续区间的和)

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

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

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

  4. HDU5087——Revenge of LIS II(BestCoder Round #16)

    Revenge of LIS II Problem DescriptionIn computer science, the longest increasing subsequence problem ...

  5. hdu5086——Revenge of Segment Tree

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

  6. BestCoder Round #16

    BestCoder Round #16 题目链接 这场挫掉了,3挂2,都是非常sb的错误 23333 QAQ A:每一个数字.左边个数乘上右边个数,就是能够组成的区间个数,然后乘的过程注意取模不然会爆 ...

  7. hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

    传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131 ...

  8. HDU 5228 ZCC loves straight flush( BestCoder Round #41)

    题目链接:pid=5228">ZCC loves straight flush pid=5228">题面: pid=5228"> ZCC loves s ...

  9. HDU 5432 Rikka with Tree (BestCoder Round #53 (div.2))

    http://acm.hdu.edu.cn/showproblem.php?pid=5423 题目大意:给你一个树 判断这棵树是否是独特的 一颗树是独特的条件:不存在一颗和它本身不同但相似的树 两颗树 ...

随机推荐

  1. Linux LVM 总结

    LVM全称是Logical Volume Manager. 它主要是实现硬盘容量的动态扩展. 一般用于存储数据量无法预估的场景, 例如Linux的根目录用lvm存储. LVM创建过程一般是这样的 1. ...

  2. C#防止内存泄露的方法

    一般程序员()都会这样认为:用C#这样的语言编程的一个好处就是无需再考虑内存的分配和释放.你只需创建对象,然后通过一种叫做垃圾收集的机制来处理这 些对象,也就是说:当它们不再被应用程序需要的时候来自动 ...

  3. 基于CSS3鼠标滑过放大突出效果

    还记得之前分享过一款CSS3图片悬停放大特效,效果非常不错.今天我们要再来分享一款类似的CSS鼠标滑过放大突出效果,只不过之前那个是图片,这次是色块,其实掌握了其CSS原理,任何网页元素都可以实现这种 ...

  4. css3 data-attribute属性打造漂亮的按钮

    之前介绍了几款css3实现的按钮,今天为网友来款比较新鲜的,用css3的data-attribute属性开发按钮,当鼠标经过显示按钮的详细信息.而且实现过程很简单,几行代码就搞定.大家试一试吧.如下图 ...

  5. 集成 IBM Business Process Manager V8 与企业内容管理系统

    连接:http://www.ibm.com/developerworks/cn/bpm/bpmjournal/1212_ramos/1212_ramos.html 集成 IBM Business Pr ...

  6. php多进程编程相关资料(以备参考)

    进程与线程的区别 要了解二者的区别与联系,首先得对进程与线程有一个宏观上的了解. 进程,是并发执行的程序在执行过程中分配和管理资源的基本单位,是一个动态概念,竟争计算机系统资源的基本单位.每一个进程都 ...

  7. CentOS6.3升级Python到2.7.3版本

    http://www.zhangchun.org/the-centos6-3-upgrade-python-to-2-7-3-version/ 查看python的版本 1 python  -V  2 ...

  8. Unity Shader学习资料

    文本教程 http://www.cnblogs.com/polobymulberry/p/4314147.html 视频教程 http://www.sikiedu.com/my/course/37 冯 ...

  9. map-reduce 优化

    map阶段优化 参数:io.sort.mb(default 100) 当map task开始运算,并产生中间数据时,其产生的中间结果并非直接就简单的写入磁盘. 而是会利用到了内存buffer来进行已经 ...

  10. 22 Best Sites To Download Free Sprites

    http://unity3diy.blogspot.com/2014/11/Free-Sprites-Download-For-YourGames.html ————————————————————— ...