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
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.
 

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



官方题解:

考虑每一个数出如今多少个子序列之中。如果第i个数为Ai。区间为[L,R]。

那么包括Ai的区间满足L⩽i⋂R⩾i。累加(L+1)∗(N−R)∗A[i]就能够了。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
long long mod=1000000000+7;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
long long n;
long long ans=0;
long long a;
scanf("%I64d",&n);
for(int i=0;i<n;i++)
{
scanf("%I64d",&a);
ans=(ans+(((a*(i+1)%mod)*(n-i))%mod))%mod;
}
printf("%I64d\n",ans);
}
return 0;
}


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. Excel工作记录表制作

    前言:我们一天中的工作可能特别忙.事情特别多,这时候你就要像CPU一样去调度.去执行任务,但总要有个地方记录你的执行情况,这就需要我们自己去记录和管理了,下面是使用excel的简单管理记录方式,下图是 ...

  2. pandas入门——loc与iloc函数

    oc与iloc函数 loc函数 import pandas as pd import numpy # 导入数据 df = pd.read_csv(filepath_or_buffer="D: ...

  3. 练习1 Just Java

    任务:做这样一个界面,选择数量,自动计算价格.超级简单.. <?xml version="1.0" encoding="utf-8"?> <a ...

  4. MAC层作用

    对于无线传感网 MAC,顾名思义,就是介质访问控制,是用来控制无线介质的访问的,由于无线传输是共享空中资源的,必然存在多个无线传感器节点对传输介质的争用,MAC层协议就是用来解决这个问题的,包括冲突的 ...

  5. iis部署webservice问题集合

    一.添加网站 具体步骤:打开控制面板,选择管理工具,打开管理工具. 打开管理工具后,打开第二个internet信息服务(iis)管理器. 打开后的界面如下: 右击网站,添加网站后,弹出“添加网站”选项 ...

  6. 腾讯云CentOS升级JDK1.8

    1.查看CentOS自带JDK是否已安装. yum list installed |grep java. 2.卸载原有JDK yum -y remove java-1.5.0-gcj.i686 3.查 ...

  7. Java并发编程:Lock和Synchronized <转>

    在上一篇文章中我们讲到了如何使用关键字synchronized来实现同步访问.本文我们继续来探讨这个问题,从Java 5之后,在java.util.concurrent.locks包下提供了另外一种方 ...

  8. Linxu

    http://www.92csz.com/study/linux/ MySql 乱码  修改 /etc/my.cnf文件 character-set-server=utf8 , 表名不区分大小写:lo ...

  9. Java编程的逻辑 (73) - 并发容器 - 写时拷贝的List和Set

    ​本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...

  10. Ansible 进阶技巧

    原文  http://www.ibm.com/developerworks/cn/linux/1608_lih_ansible/index.html?ca=drs-   简介 Ansible 是一个系 ...