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 ...
随机推荐
- 基于Sql Server 2008的分布式数据库的实践(五)
原文 基于Sql Server 2008的分布式数据库的实践(五) 程序设计 ------------------------------------------------------------- ...
- Hdu 1158 Employment Planning(DP)
Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1158 一道dp题,或许是我对dp的理解的还不够,看了题解才做出来,要加油了. 只能先上代码了 ...
- Android大放送干:书籍、过程、工具等各种全
完全干燥分享,本文收集Android制定必要的书籍.过程.具.新闻和杂志各种资源.它们能让你在Android开发之旅的各个阶段都受益. 入门 <Learning Android(中文版)> ...
- 如何实现android蓝牙开发 自动配对连接,并不弹出提示框
之前做一个android版的蓝牙 与血压计通讯的项目,遇到最大的难题就是自动配对. 上网查资料说是用反射createBond()和setPin(),但测试时进行配对还是会出现提示,但配对是成功了 我就 ...
- 管理tips
管理是什么? 我认为达到的目的就是高效.低成本. 成本低才能有盈余,才能活的长和舒服.高效就是无谓的消耗少,以结果为导向. 开源节流,应该包含显性的与隐性的两方面. 隐性成本: 1.会议成本;2.沟通 ...
- Linux fstab 参数详解
[root@qs-wg-db1 /]# cat /etc/fstab LABEL=/ / ext3 defaults ...
- Android Studio安装教程
Google在2013 I/O大会上发布Android Studio之后,广大Android开发者欢欣鼓舞,不过很快就有人出现问题,Android Studio无法安装,或者安装后无法启动,这篇文章就 ...
- 浙江大学PAT上机题解析之3-05. 求链式线性表的倒数第K项
给定一系列正整数,请设计一个尽可能高效的算法,查找倒数第K个位置上的数字. 输入格式说明: 输入首先给出一个正整数K,随后是若干正整数,最后以一个负整数表示结尾(该负数不算在序列内,不要处理). 输出 ...
- cocos2dx-lua捕获用户touch事件的几种方式
这里仅仅针对lua 1.为每一个关心的事件注冊回调函数 详细分为下面几种 1>单点触摸 注冊函数为 cc.Handler.EVENT_TOUCH_BEGAN = 40 cc.Handl ...
- 利用FFT 计算生成离散解析信号
通常我们用到的信号都是实值信号,但是我们可以根据这个实信号构造出一个复信号,使得这个复信号只包含正频率部分,而且这个复信号的实部正好就是我们原来的实值信号.简单的推导可知,复信号的虚部是原信号的希尔伯 ...