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. windows服务安装错误 在‘安装’过程发生异常:System.ComponentModel.Win32Exception:系统正在关机

    今天安装windows服务的时候先是在本地安装测试通过,但是一到服务器就一直安装失败 在‘安装’过程发生异常:System.ComponentModel.Win32Exception:系统正在关机 然 ...

  2. Oracle 数据导入导出(imp/exp)

    环境:windows下,oracle11g 1.启动oracle服务 net start OracleDBConsoleorclnet start OracleOraDb11g_home1TNSLis ...

  3. [转]Java web 开发 获取用户ip

    如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值,那么真正的用户端的真实IP则是取X-Forwarded-For中第一个非unknown的有效IP字符串. pu ...

  4. 上传预览图片的插件jquery-fileupload

    上传预览图片的插件jquery-fileupload github地址:https://github.com/blueimp/jQuery-File-Upload 中文文档:http://www.jq ...

  5. 关于17个Cr的430采购的注意事项 430F

    430F不锈钢是在430钢上加上易切削性能的钢种.用于自动车床.螺栓和螺母等.430LX在430钢中添加Ti或Nb.降低C含量,改善了加工性能的和焊接性能.用于热水罐.供热水系统.卫生器具.家庭用耐用 ...

  6. Caffe+Kubuntu16.04_X64+CUDA 8.0配置

    前言: 经过尝试过几次Caffe,theano,MxNet之后,很长时间没有进行caffe的更新,此次在Ubuntu16.04下安装Caffe,折腾了一天时间,终于安装成功. 参考链接:Caffe+U ...

  7. Object::connect: No such slot (QT槽丢失问题)

    1.看看你的类声明中有没有Q_OBJECT,并继承public QMainWindow{ 例如: class CPlot: public QMainWindow{ Q_OBJECT 2.你声明的函数要 ...

  8. css3基础篇二

    CSS3 边框 border-radius box-shadow border-image(ie不支持) 语法 border-radius: 1-4 length|% / 1-4 length|%; ...

  9. 【sqli-labs】 less22 Cookie Injection- Error Based- Double Quotes - string (基于错误的双引号字符型Cookie注入)

    注入的过程和less 20 21一样,这次闭合cookie的使用的双引号

  10. mysql主主同步

    Mysql 主主同步方案 第一台机器主 [root@master ~]# vim /etc/my.cnf [mysqld] server-id=1 log-bin=mysql-binlog log-s ...