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. HDU_1398_母函数

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  2. Linux 之根目录介绍

    1. /bin binary二进制 存放系统许多可执行程序文件 执行的相关指令,例如ls pwd whoami,后台的支持文件目录 2. /sbin super binary超级的二进制 存放系统许多 ...

  3. webpack学习(六)—webpack+react+es6(第2篇)

    接上篇        webpack学习(五)—webpack+react+es6(第1篇) 本文做个简单的图片加文字的页面.其中,配置文件跟上篇一致.项目结构: index.html <!DO ...

  4. 模拟人的手指在UI上滑动时3D模型跟随着移动(Unity)

    问题: 怎么让当手指滑动的同时对应的模型发生旋转 解决办法: 1:通过控制摄像机或者模型来实现效果 2:通过获取鼠标移动时X轴Y轴的偏移量来确定模型的旋转角度 3:为了不让人感觉到突兀,建议使用Mat ...

  5. 接口测试与Postman

    阅读目录 1.接口测试简介 1.1 什么是接口测试  1.2 接口测试的必要性 1.3 接口测试流程 1.4 接口文档 1.5 接口测试用例设计 1.6 接口测试用例模板 2.Postman 2.1 ...

  6. 蒟蒻的长链剖分学习笔记(例题:HOTEL加强版、重建计划)

    长链剖分学习笔记 说到树的链剖,大多数人都会首先想到重链剖分.的确,目前重链剖分在OI中有更加多样化的应用,但它大多时候是替代不了长链剖分的. 重链剖分是把size最大的儿子当成重儿子,顾名思义长链剖 ...

  7. 写代码怎能不会这些Linux命令?

    转自:https://zhuanlan.zhihu.com/p/28674639?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=to ...

  8. 《奋斗吧!菜鸟》 第八次作业:Alpha冲刺 Scrum meeting 1

    项目 内容 这个作业属于哪个课程 任课教师链接 作业要求 https://www.cnblogs.com/nwnu-daizh/p/11012922.html 团队名称 奋斗吧!菜鸟 作业学习目标 A ...

  9. 2-SAT·hihoCoder音乐节

    2-SAT·hihoCoder音乐节 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 hihoCoder音乐节由hihoCoder赞助商大力主办,邀请了众多嘉宾和知名乐队 ...

  10. Music in Car

    Music in Car time limit per test 1 second memory limit per test 256 megabytes Sasha reaches the work ...