hdu5086——Revenge of Segment Tree
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
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.
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
2
1
2
3
1 2 3
2
20HintFor 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.
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的更多相关文章
- HDU5086——Revenge of Segment Tree(BestCoder Round #16)
Revenge of Segment Tree Problem DescriptionIn computer science, a segment tree is a tree data struct ...
- HDU5086:Revenge of Segment Tree(规律题)
http://acm.hdu.edu.cn/showproblem.php?pid=5086 #include <iostream> #include <stdio.h> #i ...
- hdu 5086 Revenge of Segment Tree(BestCoder Round #16)
Revenge of Segment Tree Time Limit: 4000/20 ...
- [ACM] HDU 5086 Revenge of Segment Tree(全部连续区间的和)
Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...
- HUD 5086 Revenge of Segment Tree(递推)
http://acm.hdu.edu.cn/showproblem.php?pid=5086 题目大意: 给定一个序列,求这个序列的子序列的和,再求所有子序列总和,这些子序列是连续的.去题目给的第二组 ...
- BestCoder#16 A-Revenge of Segment Tree
Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- [LintCode] Segment Tree Build 建立线段树
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
随机推荐
- Qt如何读取ico文件中的image(使用QImageReader和QIcon)
ico文件是一个容器,内部可以装载许多个image,我们可以通过QIcon的pixmap方法来获取需要的image QPixmap pixmap ( const QSize & size, M ...
- php unset 数组陷阱
我们删除一个array, unset($arr); 想删除某个元素 unsert($arr[i]) 一个陷阱是: unset() 函数允许删除数组中的某个键.但要注意数组将不会重建索引.如果需要删除后 ...
- 【iOS】用Layer创建一个三维模型以及拖动
关于CALayer的介绍以及基本属性,在这篇博客中有交代:CoreAnimation —— CALayer 这篇博客讲述简单的通过对layer的transform属性的设置一个CATransform3 ...
- VSTO不能创建OFFICE 文档项目的原因
正用的好好的,突然vsto不能用了.我是安装的vs2015 社区版本,本身是不带vsto的,当初不知道怎吗安装上的,昨天突然不能用了.症状是创建excel workbook 类型的项目是失败(创建ad ...
- Ubuntu 14.04下安装GitLab指南
摘要 GitLab 是一个用于仓库管理系统的开源项目.使用Git作为代码管理工具,并在此基础上搭建起来的web服务. 在GitLab的官方网站上面对Ubuntu的支持也是很好的,有比较详尽的安装指南. ...
- xend调用xenstore的出错揭秘
近期发现几例问题,均是xend里面报了同一个错误 File "/usr/lib64/python2.4/site-packages/xen/xend/xenstore/xstransact. ...
- 关于UIText换行
话不多说,直接上代码 --代码是lua的,c++也一样 local text = ccui.Text:create("text can line wrap text can line wra ...
- android 巧用资源文件(不断积累)
1.shape的使用 <shape xmlns:android="http://schemas.android.com/apk/res/android" > <s ...
- js打印
js打印,其实是打印当前页面的内容,是调用 系统的js方法,来弹出 打印设置窗口,用法很简单. window.print()就行,有的考虑到 浏览器兼容性问题,会用到document.execComm ...
- 手把手教你安装QT集成开发环境(操作系统为ubuntu10.04)
在安装QT集成开发工具包之前需要先安装build-essential和libncurses5-dev这两个开发工具和库,libncurses5-dev库是一个在Linux/Unix下广泛应用的图形函数 ...