[ACM] HDU 5086 Revenge of Segment Tree(全部连续区间的和)
Revenge of Segment Tree
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.
解题思路:
给定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(全部连续区间的和)的更多相关文章
- hdu 5086 Revenge of Segment Tree(BestCoder Round #16)
Revenge of Segment Tree Time Limit: 4000/20 ...
- HUD 5086 Revenge of Segment Tree(递推)
http://acm.hdu.edu.cn/showproblem.php?pid=5086 题目大意: 给定一个序列,求这个序列的子序列的和,再求所有子序列总和,这些子序列是连续的.去题目给的第二组 ...
- 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
Revenge of Segment Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- HDU5086:Revenge of Segment Tree(规律题)
http://acm.hdu.edu.cn/showproblem.php?pid=5086 #include <iostream> #include <stdio.h> #i ...
- BestCoder#16 A-Revenge of Segment Tree
Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...
- hdu 5086(递推)
Revenge of Segment Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- 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 ...
- Segment Tree Beats 区间最值问题
Segment Tree Beats 区间最值问题 线段树一类特殊技巧! 引出:CF671C Ultimate Weirdness of an Array 其实是考试题,改题的时候并不会区间取最值,区 ...
随机推荐
- CentOS6 在线安装PostgreSQL10
本文主要通过实际案例介绍如何在CentOS6环境中在线安装PostgreSQL10,安装环境需具备能够使用yum在线安装功能.具体安装步骤如下, 1 下载对应版本的PGDG文件 从https://yu ...
- sleep()和wait()的区别
1 sleep()方法,我们首先要知道该方法是属于Thread类中的.而wait()方法,则是属于Object类中的. 2 Thread.sleep和Object.wait都会暂停当前的线程,对于CP ...
- W3C标准冒泡、捕获机制
(一) 捕获和冒泡如何相互影响 我们来做几个任务吧! 有一个前提,#parent为标签,#child为子标签,他们是嵌套关系支线任务1 //捕获模式 document.getElementById(' ...
- poj2376 Cleaning Shifts 区间贪心
题目大意: (不说牛了) 给出n个区间,选出个数最少的区间来覆盖区间[1,t].n,t都是给出的. 题目中默认情况是[1,x],[x+1,t]也是可以的.也就是两个相邻的区间之间可以是小区间的右端与大 ...
- Oracle存储过程给变量赋值的方法
截止到目前我发现有三种方法可以在存储过程中给变量进行赋值: 1.直接法 := 如:v_flag := 0; 2.select into 如:假设变量名为v_flag,select count( ...
- ROS:Nvidia Jetson TK1平台安装使用ROS
原文连接: http://wiki.ros.org/indigo/Installation/UbuntuARM Ubuntu ARM install of ROS Indigo There are c ...
- Pjax无刷新跳转页面实现,支持超链接与表单提交
什么是pjax? 当你点击一个站内的链接的时候,不是做页面跳转,而是只是站内页面刷新.这样的用户体验,比起整个页面都闪一下来说, 好很多. 其中有一个很重要的组成部分, 这些网站的ajax刷新是支持浏 ...
- C# 获得剪贴板内容和 richTextBox部分文本设置颜色
try { MemoryStream vMemoryStream = iData.GetData("Html Format") as MemoryStream; if (vMemo ...
- 玲珑杯#20 C 漆黑的太阳——莫队
题目:https://www.ifrog.cc/acm/problem/1155 题解:https://www.ifrog.cc/acm/solution/28 1.如何不重复计算一个值 自己想的是对 ...
- 3D模型在UI上显示的方法(Unity)
方法:使用RawImage通过Render Texter将摄像机下的物体渲染纹理记录并显示在RawImage上面 具体实现:新建一个模型(Cube),新建一个摄像机,将Clear Flags设置为So ...