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. B - Link/Cut Tree

    Problem description Programmer Rostislav got seriously interested in the Link/Cut Tree data structur ...

  2. nginx单机1w并发设置

    关闭keep-live,提高链接回收 keeplive_timeout 0; events{ worker_connections 1024; } more /proc/sys/net/core/so ...

  3. Objective-C—— Block

    OC Block 其实功能就类似C语言的函数指针,js中的闭包之类的.把代码块当做一个变量就行操作,有自己的变量和作用域. 简单看一下Block的语法和可能出现的问题: Block语法: block语 ...

  4. intellij 创建java web项目(maven管理的SSH)

    intellij 创建java web项目(maven管理的SSH) 环境intellij IDEA14.MAVEN.Spring.Struts2.Hibernate.Java Web.工程搭建. 1 ...

  5. c# ado.net eftity framework 返回多表查询结果

    public static IQueryable GetWeiXinTuWenList() { using (var Model = new Model.WeiXinEntities()) { var ...

  6. 安卓手机USB无法共享、上网或卡顿的解决方法

    安卓手机通过USB为电脑(Windows10)提供网络接入点时,系统程序会异常卡顿. 1)设备管理器2)点击“网络适配器”,在弹出的下拉列表中选择”Remote NDIS based Internet ...

  7. C++对象的内存模型

    1. 普通对象模型 对象是如何在内存中布局的? 成员 存放位置 访问范围 非静态数据成员 每一个对象体内 为该对象专有 静态数据成员 程序的静态存储区内,只有一份实体 为该类所有对象共享 成员函数(静 ...

  8. [转]使用Fiddler进行iOS APP的HTTP/HTTPS抓包

    Fiddler不但能截获各种浏览器发出的HTTP请求, 也可以截获各种智能手机发出的HTTP/HTTPS请求.Fiddler能捕获iOS设备发出的请求,比如IPhone, IPad, MacBook. ...

  9. 完全掌握vuex

    公司项目中大量的使用了vue,感觉对vue知识的掌握也越来越熟练了,录制视频教程也让我受益匪浅,自己成长的同时,我更希望帮助其他前端小伙伴一起成长.这篇文章我们主要讲解vuex. vuex是一个专门为 ...

  10. BZOJ [Poi2000]病毒 AC自动机_DFS_细节

    Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) # ...