Revenge of Segment Tree

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

Total Submission(s): 383    Accepted Submission(s): 163

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
 
Recommend
heyang   |   We have carefully selected several similar problems for you:  

pid=5089" target="_blank">5089 

pid=5088" target="_blank">5088 5085 5084 5082 

 



显然枚举全部区间是不可能的,我们得找找规律什么的,能够发现,设全部数的和是sum, S1(区间长度为1)的是sum,S2 = 2 * sum - (a1 + an)

S3 = 3 * sum - (2 * a1 + a2 + 2 *an + a1)

再枚举几个就能够找到规律



所以,总的和里。从左往右看 a1出现了(n-1)*n/2次,a2是(n - 2)*(n - 1)/2次........................

从右往左看,an出现了(n-1)*n/2次,an-1是(n - 2)*(n - 1)/2次........................



所以在O(n)的时间里就完毕了计算。注意用__int64以及取模

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; __int64 a[447100];
__int64 b[447100];
const __int64 mod = 1000000007; int main()
{
int t, n;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
__int64 ans = 0, x;
__int64 sum = 0;
for (int i = 1; i <= n; i++)
{
scanf("%I64d", &x);
b[i] = x;
a[i] = (__int64)(n - i) * (1 + n - i) / 2 % mod;
sum += x;
sum %= mod;
}
for (int i = 1; i <= n; i++)
{
a[i] = (__int64)a[i] * b[i] % mod;
}
for (int i = n; i >= 1; i--)
{
a[i] += (__int64)(i - 1) * i / 2 % mod * b[i] % mod;
}
ans = (__int64) n * (n + 1) / 2 % mod * sum % mod;
for (int i = 1; i <= n; i++)
{
ans -= a[i];
ans %= mod;
if (ans < 0)
{
ans += mod;
}
ans %= mod;
}
printf("%I64d\n", ans);
}
return 0;
}

hdu5086——Revenge of Segment Tree的更多相关文章

  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. HDU5086:Revenge of Segment Tree(规律题)

    http://acm.hdu.edu.cn/showproblem.php?pid=5086 #include <iostream> #include <stdio.h> #i ...

  3. hdu 5086 Revenge of Segment Tree(BestCoder Round #16)

    Revenge of Segment Tree                                                          Time Limit: 4000/20 ...

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

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

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

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

  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. [LintCode] Segment Tree Build II 建立线段树之二

    The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...

  8. [LintCode] Segment Tree Build 建立线段树

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  9. Segment Tree Modify

    For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...

随机推荐

  1. html向servlet传乱码解决办法

    html 设置为utf-8格式 <meta http-equiv="content-type" content="text/html;charset=UTF-8&q ...

  2. 基于visual Studio2013解决C语言竞赛题之0520相邻元素

          题目

  3. 快的打车 技术部 在 杭州 招聘 #年前面试 年后入职#架构师 - 内推网(neitui.me)

    快的打车 技术部 在 杭州 招聘 #年前面试 年后入职#架构师 - 内推网(neitui.me) 陈丹 (cd**@kuaidadi.com) 01-18 发布了内推 #年前面试 年后入职#架构师 • ...

  4. jfinal集成spring cxf做webservice服务

    链接地址:http://zhengshuo.iteye.com/blog/2154047 废话不说,直接上代码 新增cxf的plugin CXFPlugin package com.jfinal.pl ...

  5. iOS开发网络数据之AFNetworking使用1

    链接地址:http://blog.csdn.net/daiyelang/article/details/38421341 如何选择AFNetworking版本 官网下载2.5版本:http://afn ...

  6. mac下的应用程序发布 及 打包(Python写的脚本,可打包第三方库)

    其实这个问题在网上能搜到大把的解决方案.大家的统一答案都是 otool -L yourapp.app/Contents/MacOS/yourapp 根据输出信息在运行 install_name_too ...

  7. Qt国际化(Q_DECLARE_TR_FUNCTIONS() 宏给非Qt类添加翻译支持,以前没见过QTextEncoder和QTextDecoder和QLibraryInfo::location()和QEvent::LanguageChange)

    Internationalization with Qt 应用程序的国际化就是使得程序能在国际间可用而不仅仅是在本国可用的过程. Relevant Qt Classes andAPIs 以下的类支持Q ...

  8. cocos2d-x游戏开发系列教程-中国象棋03-主界面

    前情回顾 上个博客说道我们象棋程序进入了欢迎界面,在欢迎界面下等待一秒进入主界面 进入主界面的关键代码如下: CCScene* pScene = CCMainMenu::scene();  创建sce ...

  9. Swift - .plist文件数据的读取和存储

    每次在Xcode中新建一个iOS项目后,都会自己产生一个.plist文件,里面记录项目的一些配置信息.我们也可以自己创建.plist文件来进行数据的存储和读取. .plist文件其实就是一个XML格式 ...

  10. MFC:重绘Button,定制CButton,自画CPngButton,求赐教(各种bug包括性能bug)谢谢谢谢

    [1.]CPngButton.h(资源是最后图片) #pragma once #include<atlimage.h> #define PNUM 19 #define PLAYTIME 1 ...